Publish a multiplatform library

    You can also use gradle-bintray-plugin for publishing multiplatform libraries. However, this plugin does not support publishing Gradle module metadata required for . Use this workaround to enable metadata publishing or migrate to the .

    When used with maven-publish, the Kotlin plugin automatically creates publications for each target that can be built on the current host, except for the Android target, which needs an additional step to configure publishing.

    Publications of a multiplatform library include an additional root publication kotlinMultiplatform that stands for the whole library and is automatically resolved to the appropriate platform-specific artifacts when added as a dependency to the common source set. Learn more about .

    This kotlinMultiplatform publication includes metadata artifacts and references the other publications as its variants.

    To avoid duplicate publications of modules that can be built on several platforms (like JVM and JS), configure the publishing tasks for these modules to run conditionally.

    You can detect the platform in the script, introduce a flag such as isMainHost and set it to true for the main target platform. Alternatively, you can pass the flag from an external source, for example, from CI configuration.

    This simplified example ensures that publications are only uploaded when isMainHost=true is passed. This means that a publication that can be published from multiple platforms will be published only once – from the main host.

    1. kotlin {
    2. jvm()
    3. mingwX64()
    4. linuxX64()
    5. def publicationsFromMainHost =
    6. [jvm(), js()].collect { it.name } + "kotlinMultiplatform"
    7. publications {
    8. matching { it.name in publicationsFromMainHost }.all { targetPublication ->
    9. tasks.withType(AbstractPublishToMaven)
    10. .matching { it.publication == targetPublication }
    11. .configureEach { onlyIf { findProperty("isMainHost") == "true" } }
    12. }
    13. }
    14. }

    By default, each publication includes a sources JAR that contains the sources used by the main compilation of the target.

    To publish an Android library, you need to provide additional configuration.

    1. kotlin {
    2. publishLibraryVariants("release", "debug")
    3. }
    4. }

    The example works for Android libraries without product flavors. For a library with product flavors, the variant names also contain the flavors, like fooBarDebug or fooBazRelease.

    Similarly, a library consumer needs to provide matching fallbacks for custom product flavors if some are missing in the library publications.

    You can also publish variants grouped by the product flavor, so that the outputs of the different build types are placed in a single module, with the build type becoming a classifier for the artifacts (the release build type is still published with no classifier). This mode is disabled by default and can be enabled as follows: