Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup kotlin project with gradle

Tags:

gradle

kotlin

I'm new to kotlin and gradle and tried to set up my very first project:

build.gradle

buildscript {
   ext.kotlin_version = '1.0.1-1'

   repositories {
     mavenCentral()
     jcenter()
   }

   dependencies {
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
}

apply plugin: "kotlin"

src\main\kotlin\main.kt

package hello

fun main(args: Array<String>) {
   println("Hello World!")
}

And I get the error message "src\main\kotlin\main.kt: (4, 4): Unresolved reference: println".

The build.gradle file I copied from http://kotlinlang.org/docs/reference/using-gradle.html

I'd expect that the standard libraries are included automatically - or do I need to add something here?

I'm using gradle 2.12, JDK 1.8. (in case this matters)

like image 783
Peter T. Avatar asked Mar 25 '16 19:03

Peter T.


1 Answers

The reference is missing the kotlin-stdlib dependency. It is not added automatically.

kotlin-gradle-plugin buildscript dependency is only Gradle plugin for Kotlin builds, and it doesn't add any dependencies to your project code. In order to use the standard library, one should add it as a dependency.

Append the following to your build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

(jcenter() is needed again, these repositories are different from those in buildscript)

like image 134
hotkey Avatar answered Sep 28 '22 11:09

hotkey