[MPP] Fix Android test-on-main dependencies

Add dependencies from associated compilations conventionally
for Android, as it's done for all other multiplatform targets.
It used to work unintentionally before because of the configuration
name clash between test compilation and its default source set.

KT-35916
KT-49877
This commit is contained in:
Pavel Kirpichenkov
2022-07-06 21:54:00 +03:00
committed by Space
parent 69bb85f71f
commit 857d8aa8ae
10 changed files with 90 additions and 4 deletions
@@ -784,6 +784,31 @@ open class KotlinAndroid71GradleIT : KotlinAndroid70GradleIT() {
}
}
}
@Test
fun `test associate compilation dependencies are passed correctly to android test compilations`() {
with(Project("kt-49877")) {
build("allTests") {
assertSuccessful()
assertTasksExecuted(
":compileDebugKotlinAndroid",
":compileReleaseKotlinAndroid",
":compileDebugUnitTestKotlinAndroid",
":compileReleaseUnitTestKotlinAndroid",
":testDebugUnitTest",
":testReleaseUnitTest",
)
}
// instrumented tests don't work without a device, so we only compile them
build("packageDebugAndroidTest") {
assertSuccessful()
assertTasksExecuted(
":compileDebugAndroidTestKotlinAndroid",
)
}
}
}
}
abstract class KotlinAndroid3GradleIT : AbstractKotlinAndroidGradleTests() {
@@ -955,6 +955,7 @@ Finished executing task ':$taskName'|
options.androidHome?.let { sdkDir ->
sdkDir.parentFile.mkdirs()
put("ANDROID_HOME", sdkDir.canonicalPath)
put("ANDROID_SDK_ROOT", sdkDir.canonicalPath)
}
options.javaHome?.let {
@@ -0,0 +1,27 @@
plugins {
kotlin("multiplatform")
id("com.android.library")
}
repositories {
mavenLocal()
mavenCentral()
google()
}
android {
compileSdk = 30
}
kotlin {
android()
linuxX64()
sourceSets {
getByName("commonMain") {
dependencies {
implementation("com.squareup.okio:okio:3.2.0")
}
}
}
}
@@ -0,0 +1,3 @@
private fun test() {
val sink: okio.Sink = null!!
}
@@ -0,0 +1,3 @@
private fun test() {
val sink: okio.Sink = null!!
}
@@ -0,0 +1,3 @@
private fun test() {
val sink: okio.Sink = null!!
}
@@ -0,0 +1,3 @@
private fun test() {
val sink: okio.Sink = null!!
}
@@ -251,7 +251,7 @@ open class DefaultCompilationDetails<T : KotlinCommonOptions>(
* Adds `allDependencies` of configurations mentioned in `configurationNames` to configuration named [this] in
* a lazy manner
*/
private fun String.addAllDependenciesFromOtherConfigurations(project: Project, vararg configurationNames: String) {
protected fun String.addAllDependenciesFromOtherConfigurations(project: Project, vararg configurationNames: String) {
project.configurations.named(this).configure { receiverConfiguration ->
receiverConfiguration.dependencies.addAllLater(
project.objects.listProperty(Dependency::class.java).apply {
@@ -528,10 +528,29 @@ class AndroidCompilationDetails(
override val friendArtifacts: FileCollection
get() = target.project.files(super.friendArtifacts, compilation.testedVariantArtifacts)
/*
* Example of how multiplatform dependencies from common would get to Android test classpath:
* commonMainImplementation -> androidDebugImplementation -> debugImplementation -> debugAndroidTestCompileClasspath
* After the fix for KT-35916 MPP compilation configurations receive a 'compilation' postfix for disambiguation.
* androidDebugImplementation remains a source set configuration, but no longer contains compilation dependencies.
* Therefore, it doesn't get dependencies from common source sets.
* We now explicitly add associate compilation dependencies to the Kotlin test compilation configurations (test classpaths).
* This helps, because the Android test classpath configurations extend from the Kotlin test compilations' directly.
*/
override fun addAssociateCompilationDependencies(other: KotlinCompilation<*>) {
if ((other as? KotlinJvmAndroidCompilation)?.androidVariant != getTestedVariantData(androidVariant)) {
super.addAssociateCompilationDependencies(other)
} // otherwise, do nothing: the Android Gradle plugin adds these dependencies for us, we don't need to add them to the classpath
compilation.compileDependencyConfigurationName.addAllDependenciesFromOtherConfigurations(
project,
other.apiConfigurationName,
other.implementationConfigurationName,
other.compileOnlyConfigurationName
)
compilation.runtimeDependencyConfigurationName.addAllDependenciesFromOtherConfigurations(
project,
other.apiConfigurationName,
other.implementationConfigurationName,
other.runtimeOnlyConfigurationName
)
}
override val kotlinDependenciesHolder: HasKotlinDependencies