Publishing multiplatform libraries
You can publish a multiplatform library to a local Maven repository with the
maven-publish Gradle plugin. Specify the group, version, and the repositories where the library should be published. The plugin creates publications automatically.
To get hands-on experience, as well as learn how to publish a multiplatform library to the external Maven Central repository, see the Create and publish a multiplatform library tutorial.
Structure of publications
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 adding dependencies.
kotlinMultiplatform publication includes metadata artifacts and references the other publications as its variants.
kotlinMultiplatform publication may also need the sources and documentation artifacts if that is required by the repository. In that case, add those artifacts by using
artifact(...) in the publication's scope.
Avoid duplicate publications
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.
By default, each publication includes a sources JAR that contains the sources used by the main compilation of the target.
Publish an Android library
To publish an Android library, you need to provide additional configuration.
By default, no artifacts of an Android library are published. To publish artifacts produced by a set of Android variants, specify the variant names in the Android target block:
The example works for Android libraries without product flavors. For a library with product flavors, the variant names also contain the flavors, like
The default publishing setup is as follows:
If the published variants have the same build type (for example, all of them are
debug), they will be compatible with any consumer build type.
If the published variants have different build types, then only the release variants will be compatible with consumer build types that are not among the published variants. All other variants (such as
debug) will only match the same build type on the consumer side, unless the consumer project specifies the matching fallbacks.
If you want to make every published Android variant compatible with only the same build type used by the library consumer, set this Gradle property:
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: