chrmelchior
05/26/2023, 8:27 AMandroidUnitTest
prefer the -android
variants when resolving multiplatform dependencies, which will break if that dependency contain references to things like android.os.Build
which ours do. We also ship a -jvm
variant which we could use in this case.
It looks something like this:
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
// This will pull in library-base-android for both
// Android Unit Test and Instrumented Tests
implementation("io.realm.kotlin:library-base:1.10.0-SNAPSHOT")
}
}
val androidUnitTest by getting {
dependencies {
// Here I need to override the library-base-android variant
// with the library-base-jvm dependency.
}
}
val androidInstrumentedTest by getting {
dependencies {
// This should continue to use the library-base-android variant.
}
}
}
}
So far the work-around I have come with is using dependency substitution:
configurations.all {
resolutionStrategy.dependencySubstitution {
// Ensure that androidUnitTest uses the Realm JVM variant rather than Android.
if (name == "debugUnitTestRuntimeClasspath") {
resolutionStrategy.dependencySubstitution {
substitute(module("io.realm.kotlin:library-base:${Realm.version}")).using(
module("io.realm.kotlin:library-base-jvm:${Realm.version}")
)
substitute(module("io.realm.kotlin:cinterop:${Realm.version}")).using(
module("io.realm.kotlin:cinterop-jvm:${Realm.version}")
)
}
}
}
This seems to work, but also feels very brittle as it depends on knowing debugUnitTestRuntimeClasspath
and hoping it doesn’t change.
So I guess my question is this:
1. Is there a better way of achieving this? Preferably one that involves less code for the users of our library?
2. Can we modify the published attributes so Gradle will pick the JVM variant over Android in this case? Right now KMP publishes both org.gradle.jvm.environment
(with android
and standard-jvm
) and org.jetbrains.kotlin.platform.type
(with jvm
and androidJvm
. I could also live with a solution where we release a separate publication with attributes that will ensure that variant will be picked first for androidUnitTest
(it would basically just mirror the -jvm variant in that case).