使用 Gradle

    使用 应用 Kotlin Gradle 插件。

    Kotlin Gradle 插件 1.4.20 适用于 Gradle 5.4 及更高版本。The plugin requires Gradle 6.0 or later.

    1. plugins {
    2. kotlin("<……>") version "1.4.20"
    3. }

    需要将其中的占位符 <……> 替换为可在后续部分中找到的插件名之一。

    Targeting multiple platforms

    Projects targeting multiple platforms, called , require the kotlin-multiplatform plugin. Learn more about the plugin.

    1. plugins {
    2. id 'org.jetbrains.kotlin.multiplatform' version '1.4.20'
    3. }
    1. plugins {
    2. kotlin("multiplatform") version "1.4.20"
    3. }

    面向 JVM

    如需面向 JVM 平台,请应用 Kotlin JVM 插件。

    1. plugins {
    2. id "org.jetbrains.kotlin.jvm" version "1.4.20"
    3. }
    1. plugins {
    2. kotlin("jvm") version "1.4.20"
    3. }

    在这个代码块中的 version 应该是字面值,并且不能在其他构建脚本中应用。

    或者使用旧版 apply plugin 方式:

    1. apply plugin: 'kotlin'

    不建议在 Gradle Kotlin DSL 中以 apply 的方式应用 Kotlin 插件 – 。

    Kotlin 源代码可以与同一个文件夹或不同文件夹中的 Java 源代码一同存放。默认约定是使用不同的文件夹:

    1. project
    2. - src
    3. - main (root)
    4. - kotlin
    5. - java

    如果不使用默认约定,那么应该更新相应的 sourceSets 属性:

    1. sourceSets {
    2. main.kotlin.srcDirs += 'src/main/myKotlin'
    3. main.java.srcDirs += 'src/main/myJava'
    4. }

    面向 JavaScript

    When targeting only JavaScript, use the kotlin-js plugin. Learn more

    1. plugins {
    2. id 'org.jetbrains.kotlin.js' version '1.4.20'
    3. }
    1. plugins {
    2. kotlin("js") version "1.4.20"
    3. }

    Kotlin and Java sources

    这个插件只适用于 Kotlin 文件,因此建议将 Kotlin 和 Java 文件分开(当同一项目包含 Java 文件时)。 If you don’t store them separately , specify the source folder in the sourceSets block:

    1. kotlin {
    2. sourceSets {
    3. }
    4. }
    1. kotlin {
    2. sourceSets["main"].apply {
    3. kotlin.srcDir("src/main/myKotlin")
    4. }
    5. }

    It’s recommended that you use Android Studio for creating Android applications. .

    Configuring dependencies

    To add a dependency on a library, set the dependency of the required type (for example, implementation) in the dependencies block of the source sets DSL.

    1. kotlin {
    2. sourceSets {
    3. commonMain {
    4. dependencies {
    5. implementation 'com.example:my-library:1.0'
    6. }
    7. }
    8. }
    9. }
    1. kotlin {
    2. sourceSets {
    3. val commonMain by getting {
    4. dependencies {
    5. implementation("com.example:my-library:1.0")
    6. }
    7. }
    8. }
    9. }

    Alternatively, you can .

    Dependency types

    A dependency on a standard library (stdlib) in each source set is added automatically. The version of the standard library is the same as the version of the Kotlin Gradle plugin.

    For platform-specific source sets, the corresponding platform-specific variant of the library is used, while a common standard library is added to the rest. The Kotlin Gradle plugin will select the appropriate JVM standard library depending on the kotlinOptions.jvmTarget compiler option of your Gradle build script.

    If you declare a standard library dependency explicitly (for example, if you need a different version), the Kotlin Gradle plugin won’t override it or add a second standard library.

    If you do not need a standard library at all, you can add the opt-out flag to the gradle.properties:

    1. kotlin.stdlib.default.dependency=false

    Set dependencies on test libraries

    The is available for testing different Kotlin projects.

    Add the corresponding dependencies on test libraries:

    • For commonTest, add the kotlin-test-common and kotlin-test-annotations-common dependencies.
    • For JVM targets, use kotlin-test-junit or kotlin-test-testng for the corresponding asserter implementation and annotations mapping.
    • For Kotlin/JS targets, add kotlin-test-js as a test dependency.

    Kotlin/Native targets do not require additional test dependencies, and the kotlin.test API implementations are built-in.

    1. kotlin{
    2. sourceSets {
    3. commonTest {
    4. dependencies {
    5. implementation kotlin('test-common')
    6. implementation kotlin('test-annotations-common')
    7. }
    8. }
    9. jvmTest {
    10. dependencies {
    11. implementation kotlin('test-junit')
    12. }
    13. }
    14. jsTest {
    15. dependencies {
    16. implementation kotlin('test-js')
    17. }
    18. }
    19. }
    20. }

    You can use shorthand for a dependency on a Kotlin module, for example, kotlin(“test”) for “org.jetbrains.kotlin:kotlin-test”.

    Set a dependency on a kotlinx library

    If you use a kotlinx library and need a platform-specific dependency, you can use platform-specific variants of libraries with suffixes such as -jvm or -js, for example, kotlinx-coroutines-core-jvm. You can also use the library base artifact name instead – kotlinx-coroutines-core.

    1. kotlin {
    2. sourceSets {
    3. jvmMain {
    4. dependencies {
    5. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.2'
    6. }
    7. }
    8. }
    1. kotlin {
    2. sourceSets {
    3. val jvmMain by getting {
    4. dependencies {
    5. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.2")
    6. }
    7. }
    8. }
    9. }

    If you use a multiplatform library and need to depend on the shared code, set the dependency only once in the shared source set. Use the library base artifact name, such as kotlinx-coroutines-core or ktor-client-core.

    1. kotlin {
    2. sourceSets {
    3. commonMain {
    4. dependencies {
    5. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
    6. }
    7. }
    8. }
    9. }
    1. kotlin {
    2. sourceSets {
    3. val commonMain by getting {
    4. dependencies {
    5. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    6. }
    7. }
    8. }
    9. }

    Set dependencies at the top level

    Alternatively, you can specify the dependencies at the top level with the configuration names following the pattern <sourceSetName><DependencyType>. This is helpful for some Gradle built-in dependencies, like gradleApi(), localGroovy(), or , which are not available in the source sets dependency DSL.

    1. dependencies {
    2. commonMainImplementation 'com.example:my-library:1.0'
    3. }
    1. dependencies {
    2. "commonMainImplementation"("com.example:my-library:1.0")
    3. }

    注解处理

    Kotlin 通过 Kotlin 注解处理工具 kapt 支持注解处理。

    Kotlin Gradle 插件支持支持增量编译。增量编译会跟踪多次构建之间源文件的变更,因此只会编译这些变更所影响的文件。

    Kotlin/JVM 与 Kotlin/JS 项目均支持增量编译 and is enabled by default since Kotlin 1.1.1。

    • Add the following line to the gradle.properties or local.properties file:
      • kotlin.incremental=false 用于 Kotlin/JVM
      • kotlin.incremental.js=false 用于 Kotlin/JS 项目。
    • As the command line parameter, use -Pkotlin.incremental=false 或者 -Pkotlin.incremental.js=false

      请注意,这样用法中,该参数必须添加到后续每个子构建,并且任何具有禁用增量编译的构建都会使增量缓存失效。

    请注意,任何情况下首次构建都不会是增量的。

    Gradle 构建缓存支持

    Kotlin 插件支持 。

    如需禁用所有 Kotlin 任务的缓存,请将系统属性标志 kotlin.caching.enabled 设置为 false (运行构建带上参数 -Dkotlin.caching.enabled=false)。

    如果使用 kapt,请注意默认情况下不会缓存注解处理任务。不过,可以。

    编译器选项

    要指定附加的编译选项,请使用 Kotlin 编译任务的 kotlinOptions 属性。

    当面向 JVM 时,对于生产代码这些任务称为 compileKotlin 而对于测试代码称为 compileTestKotlin。对于自定义源文件集(source set)这些任务称呼取决于 compile<Name>Kotlin 模式。

    Android 项目中的任务名称包含构建变体 名称,并遵循 compile<BuildVariant>Kotlin 的模式,例如 compileDebugKotlincompileReleaseUnitTestKotlin

    当面向 JavaScript 时,这些任务分别称为 compileKotlinJscompileTestKotlinJs,以及对于自定义源文件集称为 compile<Name>KotlinJs

    要配置单个任务,请使用其名称。示例:

    1. compileKotlin {
    2. kotlinOptions.suppressWarnings = true
    3. }
    4. //或者
    5. compileKotlin {
    6. kotlinOptions {
    7. suppressWarnings = true
    8. }
    9. }
    1. import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    2. // ……
    3. val compileKotlin: KotlinCompile by tasks
    4. compileKotlin.kotlinOptions.suppressWarnings = true

    请注意,对于 Gradle Kotlin DSL,首先从项目的 tasks 中获取任务。

    相应地,为 JS 与 Common 目标使用类型 Kotlin2JsCompileKotlinCompileCommon

    也可以在项目中配置所有 Kotlin 编译任务:

    1. tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    2. kotlinOptions { /*……*/ }
    3. }

    对于 Gradle 任务的完整选项列表如下:

    JVM 与 JS 的公共属性

    JVM 特有的属性

    生成文档

    要生成 Kotlin 项目的文档,请使用 ; 相关配置说明请参见 Dokka README 。Dokka 支持混合语言项目,并且可以生成多种格式的输出 ,包括标准 JavaDoc。

    使用 Gradle Kotlin DSL

    使用 时,请使用 plugins { …… } 块应用 Kotlin 插件。 如果使用 apply { plugin(……) } 来应用的话,可能会遇到未解析的到由 Gradle Kotlin DSL 所生成扩展的引用问题。为了解决这个问题,可以注释掉出错的用法,运行 Gradle 任务 kotlinDslAccessorsSnapshot, 然后解除该用法注释并重新运行构建或者重新将项目导入到 IDE 中。