Error implementing multiplatform library dependency in Android project

Hello, community!

I have created Kotlin Multiplatform library “c4view-worldwind” with Android, JVM and JS targets.
I have successfully publish this library into maven local repository. I can see all classes, resources, manifest and meta info inside local repository “~/.m2/…” folder.
I have successfully added dependency to my library in my Android application using following code:
implementation ('vision.combat:c4view-worldwind-android:1.0-SNAPSHOT@aar') {transitive=true}

But when I try to build project, I receive following error:
Execution failed for task ‘:c4map:dataBindingMergeDependencyArtifactsDebug’.
> Could not resolve all files for configuration ‘:c4map:debugCompileClasspath’.
> Could not resolve vision.combat:c4view-worldwind-android:1.0-SNAPSHOT.
Required by:
project :c4map
> No matching variant of vision.combat:c4view-worldwind-android:1.0-SNAPSHOT was found. The consumer was configured to find an API of a component, as well as attribute ‘com.android.build.api.attributes.BuildTypeAttr’ with value ‘debug’ but:
- Variant ‘commonMainMetadataElements-published’ capability vision.combat:c4view-worldwind-android:1.0-SNAPSHOT:
- Incompatible because this component declares a usage of ‘kotlin-api’ of a component and the consumer needed an API of a component
- Other compatible attribute:
- Doesn’t say anything about com.android.build.api.attributes.BuildTypeAttr (required ‘debug’)
- Variant ‘metadataApiElements-published’ capability vision.combat:c4view-worldwind-android:1.0-SNAPSHOT:
- Incompatible because this component declares a usage of ‘kotlin-metadata’ of a component and the consumer needed an API of a component
- Other compatible attribute:
- Doesn’t say anything about com.android.build.api.attributes.BuildTypeAttr (required ‘debug’)
- Variant ‘releaseApiElements-published’ capability vision.combat:c4view-worldwind-android:1.0-SNAPSHOT declares an API of a component:
- Incompatible because this component declares a component, as well as attribute ‘com.android.build.api.attributes.BuildTypeAttr’ with value ‘release’ and the consumer needed a component, as well as attribute ‘com.android.build.api.attributes.BuildTypeAttr’ with value ‘debug’
- Variant ‘releaseRuntimeElements-published’ capability vision.combat:c4view-worldwind-android:1.0-SNAPSHOT declares a runtime of a component:
- Incompatible because this component declares a component, as well as attribute ‘com.android.build.api.attributes.BuildTypeAttr’ with value ‘release’ and the consumer needed a component, as well as attribute ‘com.android.build.api.attributes.BuildTypeAttr’ with value ‘debug’

How to fix this issue?

Similar issue Publish library that uses apollo-android internally · Issue #2401 · apollographql/apollo-kotlin · GitHub

There is no error in case I implement dependency like this, but it selects JVM library instead of Android:
implementation 'vision.combat:c4view-worldwind:1.0-SNAPSHOT'

Fixed by adding in library:

kotlin {
    android {
        publishAllLibraryVariants()
        publishLibraryVariantsGroupedByFlavor = true // This line
    }
}
2 Likes