平台间共享代码

    If you need to access platform-specific APIs from the shared code, use the Kotlin mechanism of .

    If you have business logic that is common for all platforms, you don’t need to write the same code for each platform – just share it in the common source set.

    All platform-specific source sets depend on the common source set by default. You don’t need to specify any relations manually for default source sets, such as jvmMain, macosX64Main, and others.

    If you need to access platform-specific APIs from the shared code, use the Kotlin mechanism of expected and actual declarations.

    对相似平台共享代码

    You often need to create several native targets that could potentially reuse a lot of the common logic and third-party APIs.

    For example, in a typical multiplatform project targeting iOS, there are two iOS-related targets: one is for iOS ARM64 devices, the other is for the x64 simulator. They have separate platform-specific source sets, but in practice there is rarely a need for different code for the device and simulator, and their dependencies are much the same. So iOS-specific code could be shared between them.

    Evidently, in this setup it would be desirable to have a shared source set for two iOS targets, with Kotlin/Native code that could still directly call any of the APIs that are common to both the iOS device and the simulator.

    In this case, you can share code across native targets in your project using the hierarchical structure.

    There are two ways you can create the hierarchical structure:

    Learn more about and using Native libraries in the hierarchical structure.

    In a typical multiplatform project with two iOS-related targets – iosArm64 and iosX64, the hierarchical structure includes an intermediate source set (iosMain), which is used by the platform-specific source sets.

    Code shared for iOS targets

    The kotlin-multiplatform plugin provides target shortcuts for creating structures for common combinations of targets.

    All shortcuts create similar hierarchical structures in the code. For example, the ios shortcut creates the following hierarchical structure:

    1. kotlin {
    2. sourceSets{
    3. dependsOn(commonMain)
    4. iosX64Main.dependsOn(it)
    5. }
    6. }
    7. }

    To create the hierarchical structure manually, introduce an intermediate source set that holds the shared code for several targets and create a structure of the source sets including the intermediate one.

    For example, if you want to share code among native Linux, Windows, and macOS targets – linuxX64M, mingwX64, and macosX64:

    1. Add the intermediate source set desktopMain that holds the shared logic for these targets.
    2. Specify the hierarchy of source sets using the dependsOn relation.
    1. kotlin {
    2. sourceSets {
    3. desktopMain {
    4. dependsOn(commonMain)
    5. }
    6. linuxX64Main {
    7. dependsOn(desktopMain)
    8. }
    9. mingwX64Main {
    10. macosX64Main {
    11. dependsOn(desktopMain)
    12. }
    13. }
    14. }
    • JVM + JS + Native
    • JVM + Native
    • JS + Native
    • JVM + JS
    • Native

    We don’t currently support sharing a source set for these combinations:

    • Several JVM targets
    • JVM + Android targets
    • Several JS targets

    If you need to access platform-specific APIs from a shared native source set, IntelliJ IDEA will help you detect common declarations that you can use in the shared native code. For other cases, use the Kotlin mechanism of .

    Thanks to the hierarchical project structure, libraries can also provide common APIs for a subset of targets. When a library is published, the API of its intermediate source sets is embedded into the library artifacts along with information about the project structure. When you use this library, the intermediate source sets of your project access only those APIs of the library which are available to the targets of each source set.

    For example, check out the following source set hierarchy from the kotlinx.coroutines repository:

    Library hierarchical structure

    The concurrent source set declares the function runBlocking and is compiled for the JVM and the native targets. Once the kotlinx.coroutines library is updated and published with the hierarchical project structure, you can depend on it and call runBlocking from a source set that is shared between the JVM and native targets since it matches the “targets signature” of the library’s concurrent source set.

    You can use platform-dependent libraries like Foundation, UIKit, and POSIX in source sets shared among several native targets. This helps you share more native code without being limited by platform-specific dependencies.

    No additional steps are required – everything is done automatically. IntelliJ IDEA will help you detect common declarations that you can use in the shared code.

    However, note that there are some limitations:

    • This approach works only for a native source set that is shared among platform-specific source sets. It doesn’t work for native source sets shared at higher levels of the source set hierarchy.
      For example, if you have nativeDarwinMain that is a parent of watchosMain and iosMain, where iosMain has two children – iosArm64Main and iosX64Main, you can use platform-dependent libraries only for iosMain but not for nativeDarwinMain.
    • It works only for interop libraries shipped with Kotlin/Native.
    1. kotlin.native.enableDependencyPropagation=false

    Learn more about the .