Build final native binaries (Experimental DSL)
Kotlin/Native targets are compiled to the *.klib
library artifacts, which can be consumed by Kotlin/Native itself as a dependency but cannot be used as a native library.
To declare final native binaries, use the new binaries format with the kotlinArtifacts
DSL. It 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.
Kotlin artifact DSL can help you to solve a common issue: when you need to access multiple Kotlin modules from your app. Since the usage of several Kotlin/Native artifacts is limited, you can export multiple Kotlin modules into a single artifact with new DSL.
Declare binaries
kotlinArtifacts {}
is the top-level block for artifact configuration in the Gradle build script. Use the following kinds of binaries to declare elements of the kotlinArtifacts {}
DSL:
Factory method | Binary kind | Available for |
---|---|---|
| All native targets, except for | |
| All native targets, except for | |
| Objective-C framework | macOS, iOS, watchOS, and tvOS targets only |
| Universal fat framework | macOS, iOS, watchOS, and tvOS targets only |
| XCFramework framework | macOS, iOS, watchOS, and tvOS targets only |
Inside the kotlinArtifacts
element, you can write the following blocks:
The simplest version requires the target
(or targets
) parameter for the selected build type. Currently, two build types are available:
DEBUG
– produces a non-optimized binary with debug informationRELEASE
– produces an optimized binary without debug information
In the modes
parameter, you can specify build types for which you want to create binaries. The default value includes both DEBUG
and RELEASE
executable binaries:
You can also declare binaries with custom names:
The argument sets a name prefix, which is the default name for the binary file. For example, for Windows the code produces the mylib.dll
file.
Configure binaries
For the binary configuration, the following common parameters are available:
Name | Description |
---|---|
| Optional linking type that defines the library type. By default, it's |
| Optional build types, |
| Optional compiler options applied to the compilation. See the list of available compiler options. |
| In addition to the current module, you can add other modules to the resulting artifact. |
| You can override the list of all modules that will be added to the resulting artifact. |
| Declares a particular target of a project. The names of available targets are listed in the Targets section. |
Libraries and frameworks
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 any other multiplatform module into a single entity and export all these modules to it.
Library
The registered Gradle task is assembleMyslibSharedLibrary
that assembles all types of registered "myslib" into a dynamic library.
Framework
The registered Gradle task is assembleMyframeFramework
that assembles all types of registered "myframe" framework.
Fat 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. This 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 registered Gradle task is assembleMyfatframeFatFramework
that assembles all types of registered "myfatframe" fat framework.
XCFrameworks
All Kotlin Multiplatform projects can use XCFrameworks as an output to gather logic for all the target platforms and architectures in a single bundle. Unlike universal (fat) frameworks, you don't need to remove all unnecessary architectures before publishing the application to the App Store.
The registered Gradle task is assembleSdkXCFramework
that assembles all types of registered "sdk" XCFrameworks.