配置编译项

Kotlin multiplatform projects use compilations for producing artifacts. Each target can have one or more compilations, for example, for production and test purposes.

For each target, default compilations include:

Compilations

If you need to compile something other than production code and unit tests, for example, integration or performance tests, you can create a custom compilation.

You can configure how artifacts are produced in:

See the list of compilation parameters and compiler options available for all or specific targets.

配置所有编译项

  1. kotlin {
  2. targets.all {
  3. compilations.all {
  4. kotlinOptions {
  5. allWarningsAsErrors = true
  6. }
  7. }
  8. }
  9. }

为一个目标配置编译项

  1. kotlin {
  2. jvm().compilations.all {
  3. kotlinOptions {
  4. sourceMap = true
  5. metaInfo = true
  6. }
  7. }
  8. }
  1. kotlin {
  2. targets.jvm.compilations.all {
  3. kotlinOptions {
  4. sourceMap = true
  5. metaInfo = true
  6. }
  7. }
  8. }

配置一个编译项

  1. kotlin {
  2. jvm().compilations.main {
  3. kotlinOptions {
  4. jvmTarget = "1.8"
  5. }
  6. }
  7. }
  1. kotlin {
  2. jvm {
  3. val main by compilations.getting {
  4. kotlinOptions {
  5. jvmTarget = "1.8"
  6. }
  7. }
  8. }
  9. }

创建自定义编译项

If you need to compile something other than production code and unit tests, for example, integration or performance tests, create a custom compilation.

For example, to create a custom compilation for integration tests of the jvm() target, add a new item to the compilations collection.

For custom compilations, you need to set up all dependencies manually. The default source set of a custom compilation does not depend on the commonMain and the commonTest source sets.

  1. kotlin {
  2. jvm() {
  3. compilations.create('integrationTest') {
  4. defaultSourceSet {
  5. dependencies {
  6. def main = compilations.main
  7. // Compile against the main compilation's compile classpath and outputs:
  8. implementation(main.compileDependencyFiles + main.output.classesDirs)
  9. implementation kotlin('test-junit')
  10. /* ... */
  11. }
  12. }
  13. // Create a test task to run the tests produced by this compilation:
  14. tasks.register('jvmIntegrationTest', Test) {
  15. // Run the tests with the classpath containing the compile dependencies (including 'main'),
  16. // runtime dependencies, and the outputs of this compilation:
  17. classpath = compileDependencyFiles + runtimeDependencyFiles + output.allOutputs
  18. // Run only the tests from this compilation's outputs:
  19. testClassesDirs = output.classesDirs
  20. }
  21. }
  22. }
  23. }
  1. kotlin {
  2. jvm() {
  3. compilations {
  4. val main by getting
  5. val integrationTest by compilations.creating {
  6. defaultSourceSet {
  7. dependencies {
  8. // Compile against the main compilation's compile classpath and outputs:
  9. implementation(main.compileDependencyFiles + main.output.classesDirs)
  10. implementation(kotlin("test-junit"))
  11. /* ... */
  12. }
  13. }
  14. // Create a test task to run the tests produced by this compilation:
  15. tasks.register<Test>("integrationTest") {
  16. // Run the tests with the classpath containing the compile dependencies (including 'main'),
  17. // runtime dependencies, and the outputs of this compilation:
  18. classpath = compileDependencyFiles + runtimeDependencyFiles + output.allOutputs
  19. // Run only the tests from this compilation's outputs:
  20. testClassesDirs = output.classesDirs
  21. }
  22. }
  23. }
  24. }
  25. }

You also need to create a custom compilation in other cases, for example, if you want to combine compilations for different JVM versions in your final artifact, or you have already set up source sets in Gradle and want to migrate to a multiplatform project.

Include Java sources in JVM compilations

By default, the JVM target ignores Java sources and compiles only Kotlin source files.

To include Java sources in the compilations of the JVM target, explicitly enable the Java language support for the target:

The Java source files are placed in the child directories of the Kotlin source roots. For example, the paths are:

Java source files

The common source sets cannot include Java sources.

Due to current limitations, the Kotlin plugin replaces some tasks configured by the Java plugin:

  • The target’s JAR task instead of jar (for example, jvmJar).
  • The target’s test task instead of test (for example, jvmTest).
  • The resources are processed by the equivalent tasks of the compilations instead of *ProcessResources tasks.

The publication of this target is handled by the Kotlin plugin and doesn’t require steps that are specific for the Java plugin.

配置与原生语言的互操作

Kotlin provides interoperability with native languages and DSL to configure this for a specific compilation.

Native languageSupported platformsComments
CAll platforms, except for WebAssembly 
Objective-CApple platforms (macOS, iOS, watchOS, tvOS) 
Swift via Objective-CApple platforms (macOS, iOS, watchOS, tvOS)Kotlin can use only Swift declarations marked with the @objc attribute.

A compilation can interact with several native libraries. Configure interoperability in the cinterops block of the compilation with available parameters.

  1. kotlin {
  2. linuxX64 { // Replace with a target you need.
  3. compilations.main {
  4. cinterops {
  5. myInterop {
  6. // Def-file describing the native API.
  7. // The default path is src/nativeInterop/cinterop/<interop-name>.def
  8. defFile project.file("def-file.def")
  9. // Package to place the Kotlin API generated.
  10. packageName 'org.sample'
  11. // Options to be passed to compiler by cinterop tool.
  12. compilerOpts '-Ipath/to/headers'
  13. // Directories for header search (an eqivalent of the -I<path> compiler option).
  14. includeDirs.allHeaders("path1", "path2")
  15. // Additional directories to search headers listed in the 'headerFilter' def-file option.
  16. // -headerFilterAdditionalSearchPrefix command line option equivalent.
  17. includeDirs.headerFilterOnly("path1", "path2")
  18. // A shortcut for includeDirs.allHeaders.
  19. includeDirs("include/directory", "another/directory")
  20. }
  21. anotherInterop { /* ... */ }
  22. }
  23. }
  24. }
  25. }
  1. kotlin {
  2. linuxX64 { // Replace with a target you need.
  3. compilations.getByName("main") {
  4. val myInterop by cinterops.creating {
  5. // Def-file describing the native API.
  6. // The default path is src/nativeInterop/cinterop/<interop-name>.def
  7. defFile(project.file("def-file.def"))
  8. // Package to place the Kotlin API generated.
  9. packageName("org.sample")
  10. // Options to be passed to compiler by cinterop tool.
  11. compilerOpts("-Ipath/to/headers")
  12. // Directories to look for headers.
  13. includeDirs.apply {
  14. // Directories for header search (an equivalent of the -I<path> compiler option).
  15. allHeaders("path1", "path2")
  16. // Additional directories to search headers listed in the 'headerFilter' def-file option.
  17. // -headerFilterAdditionalSearchPrefix command line option equivalent.
  18. headerFilterOnly("path1", "path2")
  19. }
  20. // A shortcut for includeDirs.allHeaders.
  21. includeDirs("include/directory", "another/directory")
  22. }
  23. val anotherInterop by cinterops.creating { /* ... */ }
  24. }
  25. }
  26. }

Android 编译项

The compilations created for an Android target by default are tied to Android build variants: for each build variant, a Kotlin compilation is created under the same name.

Then, for each Android source set compiled for each of the variants, a Kotlin source set is created under that source set name prepended by the target name, like the Kotlin source set androidDebug for an Android source set debug and the Kotlin target named android. These Kotlin source sets are added to the variants’ compilations accordingly.

The default source set commonMain is added to each production (application or library) variant’s compilation. The commonTest source set is similarly added to the compilations of unit test and instrumented test variants.

Annotation processing with kapt is also supported, but due to current limitations it requires that the Android target is created before the kapt dependencies are configured, which needs to be done in a top-level dependencies block rather than within Kotlin source set dependencies.

  1. kotlin {
  2. android { /* ... */ }
  3. }
  4. dependencies {
  5. kapt("com.my.annotation:processor:1.0.0")
  6. }

源集层次结构的编译项

Kotlin can build a source set hierarchy with the dependsOn relation.

Source set hierarchy

If the source set jvmMain depends on a source set commonMain then:

  • Whenever jvmMain is compiled for a certain target, commonMain takes part in that compilation as well and is also compiled into the same target binary form, such as JVM class files.
  • Sources of jvmMain ‘see’ the declarations of commonMain, including internal declarations, and also see the dependencies of commonMain, even those specified as implementation dependencies.
  • jvmMain can contain platform-specific implementations for the expected declarations of commonMain.
  • The resources of commonMain are always processed and copied along with the resources of jvmMain.
  • The language settings of jvmMain and commonMain should be consistent.

Language settings are checked for consistency in the following ways:

  • jvmMain should set a languageVersion that is greater than or equal to that of commonMain.
  • jvmMain should enable all unstable language features that commonMain enables (there’s no such requirement for bugfix features).
  • jvmMain should use all experimental annotations that commonMain uses.
  • apiVersion, bugfix language features, and progressiveMode can be set arbitrarily.