From 2a46648e3e9b4ecd8407fed896ccf0dd84335c10 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Tue, 8 May 2018 16:35:04 +0300 Subject: [PATCH] In Android MPP target, add common module dependency to api configuration Since Android considers the `compile` configuration as deprecated and reports a warning when a dependency is added to it, use the `api` configuration when dealing with an Android platform module. Issue #KT-23719 Fixed --- .../gradle/AbstractKotlinAndroidGradleTests.kt | 17 ++++++++++++++--- .../gradle/plugin/KotlinMultiplatformPlugin.kt | 12 +++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/AbstractKotlinAndroidGradleTests.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/AbstractKotlinAndroidGradleTests.kt index cc18a412ed5..4f64ff0d819 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/AbstractKotlinAndroidGradleTests.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/AbstractKotlinAndroidGradleTests.kt @@ -289,10 +289,21 @@ fun getSomething() = 10 } @Test - fun testMultiplatformAndroidCompile() { - val project = Project("multiplatformAndroidProject", gradleVersion) + fun testMultiplatformAndroidCompile() = with(Project("multiplatformAndroidProject", gradleVersion)) { + setupWorkingDir() - project.build("build") { + if (!isLegacyAndroidGradleVersion(androidGradlePluginVersion)) { + // Check that the common module is not added to the deprecated configuration 'compile' (KT-23719): + gradleBuildScript("libAndroid").appendText( + """${'\n'} + configurations.compile.dependencies.all { aDependencyExists -> + throw GradleException("Check failed") + } + """.trimIndent() + ) + } + + build("build") { assertSuccessful() assertTasksExecuted( ":lib:compileKotlinCommon", diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt index f6cbb7e3553..03c7f0360b6 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinMultiplatformPlugin.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.gradle.plugin import com.android.build.gradle.BaseExtension import org.gradle.api.* +import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.file.SourceDirectorySet import org.gradle.api.plugins.JavaPluginConvention @@ -49,6 +50,9 @@ const val IMPLEMENT_DEPRECATION_WARNING = "The '$IMPLEMENT_CONFIG_NAME' configur open class KotlinPlatformImplementationPluginBase(platformName: String) : KotlinPlatformPluginBase(platformName) { private val commonProjects = arrayListOf() + protected open fun configurationsForCommonModuleDependency(project: Project): List = + listOf(project.configurations.getByName("compile")) + override fun apply(project: Project) { val implementConfig = project.configurations.create(IMPLEMENT_CONFIG_NAME) val expectedByConfig = project.configurations.create(EXPECTED_BY_CONFIG_NAME) @@ -69,7 +73,9 @@ open class KotlinPlatformImplementationPluginBase(platformName: String) : Kotlin // Needed for the projects that depend on this one to recover the common module sources through // the transitive dependency (also, it will be added to the POM generated by Gradle): - project.configurations.getByName("compile").dependencies.add(dep) + configurationsForCommonModuleDependency(project).forEach { configuration -> + configuration.dependencies.add(dep) + } } else { throw GradleException("$project '${config.name}' dependency is not a project: $dep") @@ -177,6 +183,10 @@ open class KotlinPlatformAndroidPlugin : KotlinPlatformImplementationPluginBase( super.apply(project) } + override fun configurationsForCommonModuleDependency(project: Project): List = + (project.configurations.findByName("api"))?.let(::listOf) + ?: super.configurationsForCommonModuleDependency(project) // older Android plugins don't have api/implementation configs + override fun namedSourceSetsContainer(project: Project): NamedDomainObjectContainer<*> = (project.extensions.getByName("android") as BaseExtension).sourceSets