构建最终原生二进制文件

By default, a Kotlin/Native target is compiled down to a *.klib library artifact, which can be consumed by Kotlin/Native itself as a dependency but cannot be executed or used as a native library.

To declare final native binaries such as executables or shared libraries, use the binaries property of a native target. This property represents a collection of native binaries built for this target in addition to the default *.klib artifact and provides a set of methods for declaring and configuring them.

The kotlin-multiplatform plugin doesn’t create any production binaries by default. The only binary available by default is a debug test executable that lets you run unit tests from the test compilation.

声明二进制文件

Use the following factory methods to declare elements of the binaries collection.

Factory methodBinary kindAvailable for
executableProduct executableAll native targets
testTest executableAll native targets
sharedLibShared native libraryAll native targets, except for WebAssembly
staticLibStatic native libraryAll native targets, except for WebAssembly
frameworkObjective-C frameworkmacOS, iOS, watchOS, and tvOS targets only

The simplest version doesn’t require any additional parameters and creates one binary for each build type. Currently there two build types available:

  • DEBUG – produces a non-optimized binary with debug information
  • RELEASE – produces an optimized binary without debug information

The following snippet creates two executable binaries: debug and release.

  1. kotlin {
  2. linuxX64 { // Use your target instead.
  3. binaries {
  4. executable {
  5. // Binary configuration.
  6. }
  7. }
  8. }
  9. }

You can drop the lambda if there is no need for additional configuration:

  1. binaries {
  2. executable()
  3. }

You can specify for which build types to create binaries. In the following example, only the debug executable is created.

  1. binaries {
  2. executable([DEBUG]) {
  3. // Binary configuration.
  4. }
  5. }
  1. binaries {
  2. executable(listOf(DEBUG)) {
  3. // Binary configuration.
  4. }
  5. }

You can also declare binaries with custom names.

  1. binaries {
  2. executable('foo', [DEBUG]) {
  3. // Binary configuration.
  4. }
  5. // It's possible to drop the list of build types (in which case, all the available build types will be used).
  6. executable('bar') {
  7. // Binary configuration.
  8. }
  9. }
  1. binaries {
  2. executable("foo", listOf(DEBUG)) {
  3. // Binary configuration.
  4. }
  5. // It's possible to drop the list of build types (in which case, all the available build types will be used).
  6. executable("bar") {
  7. // Binary configuration.
  8. }
  9. }

The first argument sets a name prefix, which is the default name for the binary file. For example, for Windows the code produces the files foo.exe and bar.exe. You can also use the name prefix to access the binary in the build script.

访问二进制文件

You can access binaries to configure them or get their properties (for example, the path to an output file).

You can get a binary by its unique name. This name is based on the name prefix (if it is specified), build type, and binary kind following the pattern: <optional-name-prefix><build-type><binary-kind>, for example, releaseFramework or testDebugExecutable.

Static and shared libraries have the suffixes static and shared respectively, for example, fooDebugStatic or barReleaseShared.

  1. // Fails if there is no such binary.
  2. binaries['fooDebugExecutable']
  3. binaries.fooDebugExecutable
  4. binaries.getByName('fooDebugExecutable')
  5. // Returns null if there is no such binary.
  6. binaries.findByName('fooDebugExecutable')
  1. // Fails if there is no such binary.
  2. binaries["fooDebugExecutable"]
  3. binaries.getByName("fooDebugExecutable")
  4. // Returns null if there is no such binary.
  5. binaries.findByName("fooDebugExecutable")

Alternatively, you can access a binary by its name prefix and build type using typed getters.

  1. // Fails if there is no such binary.
  2. binaries.getExecutable('foo', DEBUG)
  3. binaries.getExecutable(DEBUG) // Skip the first argument if the name prefix isn't set.
  4. binaries.getExecutable('bar', 'DEBUG') // You also can use a string for build type.
  5. // Similar getters are available for other binary kinds:
  6. // getFramework, getStaticLib and getSharedLib.
  7. // Returns null if there is no such binary.
  8. binaries.findExecutable('foo', DEBUG)
  9. // Similar getters are available for other binary kinds:
  10. // findFramework, findStaticLib and findSharedLib.
  1. // Fails if there is no such binary.
  2. binaries.getExecutable("foo", DEBUG)
  3. binaries.getExecutable(DEBUG) // Skip the first argument if the name prefix isn't set.
  4. binaries.getExecutable("bar", "DEBUG") // You also can use a string for build type.
  5. // Similar getters are available for other binary kinds:
  6. // getFramework, getStaticLib and getSharedLib.
  7. // Returns null if there is no such binary.
  8. binaries.findExecutable("foo", DEBUG)
  9. // Similar getters are available for other binary kinds:
  10. // findFramework, findStaticLib and findSharedLib.

将依赖项导出到二进制文件

When building an Objective-C framework or a native library (shared or static), you may need to pack not just the classes of the current project, but also the classes of its dependencies. Specify which dependencies to export to a binary using the export method.

  1. kotlin {
  2. sourceSets {
  3. macosMain.dependencies {
  4. // Will be exported.
  5. api project(':dependency')
  6. api 'org.example:exported-library:1.0'
  7. // Will not be exported.
  8. api 'org.example:not-exported-library:1.0'
  9. }
  10. }
  11. macosX64("macos").binaries {
  12. framework {
  13. export project(':dependency')
  14. export 'org.example:exported-library:1.0'
  15. }
  16. sharedLib {
  17. // It's possible to export different sets of dependencies to different binaries.
  18. export project(':dependency')
  19. }
  20. }
  21. }
  1. kotlin {
  2. sourceSets {
  3. macosMain.dependencies {
  4. // Will be exported.
  5. api(project(":dependency"))
  6. api("org.example:exported-library:1.0")
  7. // Will not be exported.
  8. api("org.example:not-exported-library:1.0")
  9. }
  10. }
  11. macosX64("macos").binaries {
  12. framework {
  13. export(project(":dependency"))
  14. export("org.example:exported-library:1.0")
  15. }
  16. sharedLib {
  17. // It's possible to export different sets of dependencies to different binaries.
  18. export(project(':dependency'))
  19. }
  20. }
  21. }

You can export only api dependencies of the corresponding source set.
You can export maven dependencies, but due to current limitations of Gradle metadata, such a dependency should be either a platform dependency (for example, kotlinx-coroutines-core-native_debug_macos_x64 instead of kotlinx-coroutines-core-native) or be exported transitively.

By default, export works non-transitively. This means that if you export the library foo depending on the library bar, only methods of foo are added to the output framework.

You can change this behavior using the transitiveExport flag. If set to true, the declarations of the library bar are exported as well.

  1. binaries {
  2. framework {
  3. export project(':dependency')
  4. // Export transitively.
  5. transitiveExport = true
  6. }
  7. }
  1. binaries {
  2. framework {
  3. export(project(":dependency"))
  4. // Export transitively.
  5. transitiveExport = true
  6. }
  7. }

For example, assume that you write several modules in Kotlin and then want to access them from Swift. Since usage of several Kotlin/Native frameworks in one Swift application is limited, you can create a single umbrella framework and export all these modules to it.

构建 universal frameworks

By default, an Objective-C framework produced by Kotlin/Native supports only one platform. However, you can merge such frameworks into a single universal (fat) binary using the lipo tool. This operation especially makes sense for 32-bit and 64-bit iOS frameworks. In this case, you can use the resulting universal framework on both 32-bit and 64-bit devices.

The fat framework must have the same base name as the initial frameworks.

  1. import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
  2. kotlin {
  3. // Create and configure the targets.
  4. targets {
  5. iosArm32("ios32")
  6. iosArm64("ios64")
  7. configure([ios32, ios64]) {
  8. binaries.framework {
  9. baseName = "my_framework"
  10. }
  11. }
  12. }
  13. // Create a task building a fat framework.
  14. tasks.register("debugFatFramework", FatFrameworkTask) {
  15. // The fat framework must have the same base name as the initial frameworks.
  16. baseName = "my_framework"
  17. // The default destination directory is '<build directory>/fat-framework'.
  18. destinationDir = file("$buildDir/fat-framework/debug")
  19. // Specify the frameworks to be merged.
  20. from(
  21. targets.ios32.binaries.getFramework("DEBUG"),
  22. targets.ios64.binaries.getFramework("DEBUG")
  23. )
  24. }
  25. }
  1. import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
  2. kotlin {
  3. // Create and configure the targets.
  4. val ios32 = iosArm32("ios32")
  5. val ios64 = iosArm64("ios64")
  6. configure(listOf(ios32, ios64)) {
  7. binaries.framework {
  8. baseName = "my_framework"
  9. }
  10. }
  11. // Create a task to build a fat framework.
  12. tasks.register<FatFrameworkTask>("debugFatFramework") {
  13. // The fat framework must have the same base name as the initial frameworks.
  14. baseName = "my_framework"
  15. // The default destination directory is '<build directory>/fat-framework'.
  16. destinationDir = buildDir.resolve("fat-framework/debug")
  17. // Specify the frameworks to be merged.
  18. from(
  19. ios32.binaries.getFramework("DEBUG"),
  20. ios64.binaries.getFramework("DEBUG")
  21. )
  22. }
  23. }