Edit Page

使用 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.

You can also enable incremental compilation to make your builds faster.

插件和版本

使用 kotlin-gradle-plugin 编译Kotlin的源代码和模块.

要用的 Kotlin 版本通常是通过 kotlin.version属性来定义:

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:

里程碑 版本
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

应用于JVM

为了在JVM中应用, Kotlin插件需要配置如下

apply plugin: "kotlin"

Kotlin源文件和Java源文件可以在同一个文件夹中存在, 也可以在不同文件夹中. 默认采用的是不同的文件夹:

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

如果不想使用默认选项,你需要更新对应的 sourceSets 属性

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

应用于 JavaScript

当应用于 JavaScript 的时候, 需要设置一个不同的插件:

apply plugin: "kotlin2js"

该插件仅作用于Kotlin文件,因此推荐使用这个插件来区分Kotlin和Java文件 (这种情况仅仅是同一工程中包含Java源文件的时候). 如果 不使用默认选项,又为了应用于 JVM,我们需要指定源文件夹使用 sourceSets

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

如果你想创建一个可重用的库, 使用 kotlinOptions.metaInfo 来生成额外的二进制形式的JS文件. 这个文件应该和编译结果一起分发.

compileKotlin2Js {
	kotlinOptions.metaInfo = true
}

应用于 Android

Android的 Gradle模型和传统的Gradle有些不同, 因此如果我们想要通过Kotlin来创建一个Android应用, 应该使用 kotlin-android 插件来代替 kotlin:

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

Android Studio

如果你使用的是Android Studio, 下面的一些属性需要添加到文件中:

android {
  ...

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

上述属性可以使kotlin目录在Android Studio中作为源码根目录存在, 所以当项目模型加载到IDE可以被正确识别. Alternatively, you can put Kotlin classes in the Java source directory, typically located in src/main/java.

配置依赖

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"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

Annotation processing

The Kotlin plugin supports annotation processors like Dagger or DBFlow. In order for them to work with Kotlin classes, add the respective dependencies using the kapt configuration in your dependencies block:

dependencies {
  kapt 'groupId:artifactId:version'
}

If you previously used the android-apt plugin, remove it from your build.gradle file and replace usages of the apt configuration with kapt. If your project contains Java classes, kapt will also take care of them. If you use annotation processors for your androidTest or test sources, the respective kapt configurations are named kaptAndroidTest and kaptTest.

Some annotation processing libraries require you to reference generated classes from within your code. For this to work, you’ll need to add an additional flag to enable the generation of stubs to your build file:

kapt {
    generateStubs = true
}

Note, that generation of stubs slows down your build somewhat, which is why it’s disabled by default. If generated classes are referenced only in a few places in your code, you can alternatively revert to using a helper class written in Java which can be seamlessly called from your Kotlin code.

For more information on kapt refer to the official blogpost.

Incremental compilation

Kotlin 1.0.2 introduced new experimental incremental compilation mode in Gradle. Incremental compilation tracks changes of source files between builds so only files affected by these changes would be compiled.

There are several ways to enable it:

  1. add kotlin.incremental=true line either to a gradle.properties or a local.properties file;

  2. add -Pkotlin.incremental=true to gradle command line parameters. Note that in this case the parameter should be added to each subsequent build (any build without this parameter invalidates incremental caches).

After incremental compilation is enabled, you should see the following warning message in your build log: Using experimental kotlin incremental compilation

Note, that the first build won’t be incremental.

OSGi

OSGi 支持查看 Kotlin OSGi page.

例子

Kotlin Repository 包含的例子: