Edit Page

Using Gradle

In order to build Kotlin with Gradle you should set up the kotlin-gradle plugin, apply it to your project and add kotlin-stdlib dependencies. Those actions may also be performed automatically in IntelliJ IDEA by invoking the Tools Kotlin Configure Kotlin in Project action.

Plugin and Versions

The kotlin-gradle-plugin compiles Kotlin sources and modules.

The version of Kotlin to use is usually defined as the kotlin_version property:

buildscript {
   ext.kotlin_version = '<version to use>'

   repositories {
     mavenCentral()
   }

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

The correspondence between Kotlin releases and versions is displayed below:

Milestone Version
1.0.5-2 1.0.5-2
1.0.5 1.0.5
1.0.4 1.0.4
1.0.3 1.0.3
1.0.2 hotfix update 1.0.2-1
1.0.2 1.0.2
1.0.1 hotfix update 2 1.0.1-2
1.0.1 hotfix update 1.0.1-1
1.0.1 1.0.1
1.0 GA 1.0.0
Release Candidate 1.0.0-rc-1036
Beta 4 1.0.0-beta-4589
Beta 3 1.0.0-beta-3595
Beta 2 1.0.0-beta-2423
Beta 1.0.0-beta-1103
Beta Candidate 1.0.0-beta-1038
M14 0.14.449
M13 0.13.1514
M12.1 0.12.613
M12 0.12.200
M11.1 0.11.91.1
M11 0.11.91
M10.1 0.10.195
M10 0.10.4
M9 0.9.66
M8 0.8.11
M7 0.7.270
M6.2 0.6.1673
M6.1 0.6.602
M6 0.6.69
M5.3 0.5.998

Targeting the JVM

To target the JVM, the Kotlin plugin needs to be applied

apply plugin: "kotlin"

Kotlin sources can be mixed with Java sources in the same folder, or in different folders. The default convention is using different folders:

project
    - src
        - main (root)
            - kotlin
            - java

The corresponding sourceSets property should be updated if not using the default convention

sourceSets {
    main.kotlin.srcDirs += 'src/main/myKotlin'
    main.java.srcDirs += 'src/main/myJava'
}

Targeting JavaScript

When targeting JavaScript, a different plugin should be applied:

apply plugin: "kotlin2js"

This plugin only works for Kotlin files so it is recommended to keep Kotlin and Java files separate (if it's the case that the same project contains Java files). As with targeting the JVM, if not using the default convention, we need to specify the source folder using sourceSets

sourceSets {
    main.kotlin.srcDirs += 'src/main/myKotlin'
}

If you want to create a re-usable library, use kotlinOptions.metaInfo to generate additional JS file with binary descriptors. This file should be distributed together with the result of translation.

compileKotlin2Js {
	kotlinOptions.metaInfo = true
}

Targeting Android

Android's Gradle model is a little different from ordinary Gradle, so if we want to build an Android project written in Kotlin, we need kotlin-android plugin instead of kotlin:

buildscript {
    ...
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

Android Studio

If using Android Studio, the following needs to be added under android:

android {
  ...

  sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
  }
}

This lets Android Studio know that the kotlin directory is a source root, so when the project model is loaded into the IDE it will be properly recognized.

Configuring Dependencies

In addition to the kotlin-gradle-plugin dependency shown above, you need to add a dependency on the Kotlin standard library:

buildscript {
   ext.kotlin_version = '<version to use>'
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

apply plugin: "kotlin" // or apply plugin: "kotlin2js" if targeting JavaScript

repositories {
  mavenCentral()
}

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

If your project uses Kotlin reflection or testing facilities, you need to add the corresponding dependencies as well:

compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"

OSGi

For OSGi support see the Kotlin OSGi page.

Examples

The Kotlin Repository contains examples: