-
Notifications
You must be signed in to change notification settings - Fork 238
update: new Android KMP library Gradle publishing logic #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2a4949
update: new Android KMP library Gradle logic
zamulla c762347
fix: block name
zamulla a44a2ef
update: remove the flavors section irrelevant for the new Android KMP…
zamulla 952d23e
update: mention test configuration migration as well
zamulla 8ede33c
fix: tw review
zamulla 918b2a6
fix: tw review
zamulla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -132,7 +132,7 @@ This guarantees that all artifacts are available and correctly referenced. | |||||
| Kotlin/Native supports cross-compilation, allowing any host to produce the necessary `.klib` artifacts. | ||||||
| However, there are still some limitations you should keep in mind. | ||||||
|
|
||||||
| **Compilation for Apple targets** | ||||||
| ### Compilation for Apple targets | ||||||
|
|
||||||
| You can use any host to produce artifacts for projects with Apple targets. | ||||||
| However, you still need to use a Mac machine if: | ||||||
|
|
@@ -141,56 +141,85 @@ However, you still need to use a Mac machine if: | |||||
| * You have [CocoaPods integration](multiplatform-cocoapods-overview.md) set up in your project. | ||||||
| * You need to build or test [final binaries](multiplatform-build-native-binaries.md) for Apple targets. | ||||||
|
|
||||||
| **Duplicating publications** | ||||||
| ### Duplicating publications | ||||||
|
|
||||||
| To avoid any issues during publication, publish all artifacts from a single host to avoid duplicating publications in the | ||||||
| repository. Maven Central, for example, explicitly forbids duplicate publications and fails the process. | ||||||
| To avoid duplicating publications in the repository, publish all artifacts from a single host. | ||||||
| Maven Central, for example, explicitly forbids duplicate publications and fails the process when they are created. | ||||||
|
|
||||||
| ## 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. | ||||||
|
|
||||||
| By default, no artifacts of an Android library are published. To publish artifacts produced by a set of Android [build variants](https://developer.android.com/build/build-variants), | ||||||
| specify the variant names in the Android target block in the `shared/build.gradle.kts` file: | ||||||
| > This section assumes that you are using the Android Gradle Library Plugin. | ||||||
| > For a guide on setting up the plugin, or on migrating from the legacy `com.android.library` plugin, | ||||||
| > see the [Set up the Android Gradle Library Plugin](https://developer.android.com/kotlin/multiplatform/plugin#migrate) | ||||||
| > page in the Android documentation. | ||||||
| > | ||||||
| {style="note"} | ||||||
|
|
||||||
| To publish artifacts, add the `androidLibrary {}` block | ||||||
| to the `shared/build.gradle.kts` file, and configure the publication using the KMP DSL. | ||||||
| For example: | ||||||
|
|
||||||
| ```kotlin | ||||||
| kotlin { | ||||||
| androidTarget { | ||||||
| publishLibraryVariants("release") | ||||||
| androidLibrary { | ||||||
zamulla marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| namespace = "org.example.library" | ||||||
| compileSdk = libs.versions.android.compileSdk.get().toInt() | ||||||
| minSdk = libs.versions.android.minSdk.get().toInt() | ||||||
|
|
||||||
| // Enables Java compilation support. | ||||||
| // This improves build times when Java compilation is not needed | ||||||
| withJava() | ||||||
|
|
||||||
| compilations.configureEach { | ||||||
| compilerOptions.configure { | ||||||
| jvmTarget.set( | ||||||
| JvmTarget.JVM_11 | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| The example works for Android libraries without [product flavors](https://developer.android.com/build/build-variants#product-flavors). | ||||||
| For a library with product flavors, the variant names also contain the flavors, like `fooBarDebug` or `fooBarRelease`. | ||||||
| Note that the Android Gradle Library plugin doesn't support product flavors and build variants, streamlining configuration. | ||||||
| One of the consequences is that you need to opt in to create test source sets and configurations, for example: | ||||||
zamulla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| The default publishing setup is as follows: | ||||||
| * If the published variants have the same build type (for example, all of them are `release` or`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](https://developer.android.com/reference/tools/gradle-api/4.2/com/android/build/api/dsl/BuildType). | ||||||
| ```kotlin | ||||||
| kotlin { | ||||||
| androidLibrary { | ||||||
| // ... | ||||||
|
|
||||||
| 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: `kotlin.android.buildTypeAttribute.keep=true`. | ||||||
| // Opt in to enable and configure host-side (unit) tests | ||||||
| withHostTestBuilder {}.configure {} | ||||||
|
|
||||||
| 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 in the `shared/build.gradle.kts` file: | ||||||
| // Opt in to enable device tests, specifying the source set name | ||||||
| withDeviceTestBuilder { | ||||||
| sourceSetTreeName = "test" | ||||||
| } | ||||||
|
|
||||||
| ```kotlin | ||||||
| kotlin { | ||||||
| androidTarget { | ||||||
| publishLibraryVariantsGroupedByFlavor = true | ||||||
| // ... | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| > It is not recommended that you publish variants grouped by the product flavor in case they have different dependencies, | ||||||
| > as those will be merged into one dependency list. | ||||||
| > | ||||||
| {style="note"} | ||||||
| Before, running tests with a GitHub action, for example, required mentioning debug and release variants: | ||||||
|
||||||
| Before, running tests with a GitHub action, for example, required mentioning debug and release variants: | |
| Previously, running tests with a GitHub action required specifying debug and release variants separately: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.