KT-43944: Don't access Android test source sets' API configurations

When dependencies are added to our corresponding configurations,
report errors, as there's no proper way to add those dependencies.

Issue #KT-43944 Fixed
This commit is contained in:
Sergey Igushkin
2021-01-13 15:25:35 +04:00
committed by Space
parent 77ffc318f2
commit 17b71555e0
5 changed files with 65 additions and 9 deletions
@@ -75,7 +75,7 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
} }
@Test @Test
fun testAndroidWithNewMppApp() = with(Project("new-mpp-android", GradleVersionRequired.FOR_MPP_SUPPORT)) { fun testAndroidWithNewMppApp() = with(Project("new-mpp-android")) {
build("assemble", "compileDebugUnitTestJavaWithJavac", "printCompilerPluginOptions") { build("assemble", "compileDebugUnitTestJavaWithJavac", "printCompilerPluginOptions") {
assertSuccessful() assertSuccessful()
@@ -114,7 +114,10 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
compilerPluginArgsRegex.findAll(output).associate { it.groupValues[1] to it.groupValues[2] } compilerPluginArgsRegex.findAll(output).associate { it.groupValues[1] to it.groupValues[2] }
compilerPluginOptionsBySourceSet.entries.forEach { (sourceSetName, argsString) -> compilerPluginOptionsBySourceSet.entries.forEach { (sourceSetName, argsString) ->
val shouldHaveAndroidExtensionArgs = sourceSetName.startsWith("androidApp") val shouldHaveAndroidExtensionArgs =
sourceSetName.startsWith("androidApp") && (
androidGradlePluginVersion < AGPVersion.v7_0_0 || !sourceSetName.contains("AndroidTestRelease")
)
if (shouldHaveAndroidExtensionArgs) if (shouldHaveAndroidExtensionArgs)
assertTrue("$sourceSetName is an Android source set and should have Android Extensions in the args") { assertTrue("$sourceSetName is an Android source set and should have Android Extensions in the args") {
"plugin:org.jetbrains.kotlin.android" in argsString "plugin:org.jetbrains.kotlin.android" in argsString
@@ -260,7 +263,7 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
} }
@Test @Test
fun testAndroidMppProductionDependenciesInTests() = with(Project("new-mpp-android", GradleVersionRequired.FOR_MPP_SUPPORT)) { fun testAndroidMppProductionDependenciesInTests() = with(Project("new-mpp-android")) {
// Test the fix for KT-29343 // Test the fix for KT-29343
setupWorkingDir() setupWorkingDir()
@@ -314,7 +317,7 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
} }
@Test @Test
fun testCustomAttributesInAndroidTargets() = with(Project("new-mpp-android", GradleVersionRequired.FOR_MPP_SUPPORT)) { fun testCustomAttributesInAndroidTargets() = with(Project("new-mpp-android")) {
// Test the fix for KT-27714 // Test the fix for KT-27714
setupWorkingDir() setupWorkingDir()
@@ -346,6 +349,16 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
run { run {
val appBuildScriptBackup = gradleBuildScript("app").readText() val appBuildScriptBackup = gradleBuildScript("app").readText()
gradleBuildScript("lib").appendText(
"\n" + """
kotlin.targets.all {
attributes.attribute(
Attribute.of("com.example.target", String),
targetName
)
}
""".trimIndent()
)
gradleBuildScript("app").appendText( gradleBuildScript("app").appendText(
"\n" + """ "\n" + """
kotlin.targets.androidApp.attributes.attribute( kotlin.targets.androidApp.attributes.attribute(
@@ -357,9 +370,24 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
build(":app:compileDebugKotlinAndroidApp") { build(":app:compileDebugKotlinAndroidApp") {
assertFailed() // dependency resolution should fail assertFailed() // dependency resolution should fail
assertContains("Required com.example.target 'notAndroidLib'") assertTrue(
"Required com.example.target 'notAndroidLib'" in output ||
"attribute 'com.example.target' with value 'notAndroidLib'" in output
)
} }
gradleBuildScript("lib").writeText(
appBuildScriptBackup + "\n" + """
kotlin.targets.all {
compilations.all {
attributes.attribute(
Attribute.of("com.example.compilation", String),
targetName + compilationName.capitalize()
)
}
}
""".trimIndent()
)
gradleBuildScript("app").writeText( gradleBuildScript("app").writeText(
appBuildScriptBackup + "\n" + """ appBuildScriptBackup + "\n" + """
kotlin.targets.androidApp.compilations.all { kotlin.targets.androidApp.compilations.all {
@@ -373,7 +401,10 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
build(":app:compileDebugKotlinAndroidApp") { build(":app:compileDebugKotlinAndroidApp") {
assertFailed() assertFailed()
assertContains("Required com.example.compilation 'notDebug'") assertTrue(
"Required com.example.compilation 'notDebug'" in output ||
"attribute 'com.example.compilation' with value 'notDebug'" in output
)
} }
} }
} }
@@ -405,6 +436,20 @@ open class KotlinAndroid36GradleIT : KotlinAndroid33GradleIT() {
} }
} }
open class KotlinAndroid70GradleIT : KotlinAndroid36GradleIT() {
override val androidGradlePluginVersion: AGPVersion
get() = AGPVersion.v7_0_0
override val defaultGradleVersion: GradleVersionRequired
get() = GradleVersionRequired.AtLeast("6.8")
override fun defaultBuildOptions(): BuildOptions {
val javaHome = File(System.getProperty("jdk11Home")!!)
Assume.assumeTrue("JDK 11 should be available", javaHome.isDirectory)
return super.defaultBuildOptions().copy(javaHome = javaHome, warningMode = WarningMode.Summary)
}
}
open class KotlinAndroid32GradleIT : KotlinAndroid3GradleIT() { open class KotlinAndroid32GradleIT : KotlinAndroid3GradleIT() {
override val androidGradlePluginVersion: AGPVersion override val androidGradlePluginVersion: AGPVersion
get() = AGPVersion.v3_2_0 get() = AGPVersion.v3_2_0
@@ -26,5 +26,6 @@ class AGPVersion private constructor(private val versionNumber: VersionNumber) {
val v3_6_0 = fromString("3.6.0") val v3_6_0 = fromString("3.6.0")
val v4_1_0 = fromString("4.1.0-beta02") val v4_1_0 = fromString("4.1.0-beta02")
val v4_2_0 = fromString("4.2.0-alpha10") val v4_2_0 = fromString("4.2.0-alpha10")
val v7_0_0 = fromString("7.0.0-alpha03")
} }
} }
@@ -10,7 +10,7 @@ android {
defaultConfig { defaultConfig {
applicationId "app.example.com.app_sample" applicationId "app.example.com.app_sample"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 27 targetSdkVersion 29
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -10,7 +10,7 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 27 targetSdkVersion 29
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
@@ -855,7 +855,17 @@ abstract class AbstractAndroidProjectHandler(private val kotlinConfigurationTool
compileOnlyConfigurationName: String, compileOnlyConfigurationName: String,
runtimeOnlyConfigurationName: String runtimeOnlyConfigurationName: String
) { ) {
project.addExtendsFromRelation(androidSourceSet.apiConfigurationName, apiConfigurationName) if (project.configurations.findByName(androidSourceSet.apiConfigurationName) != null) {
project.addExtendsFromRelation(androidSourceSet.apiConfigurationName, apiConfigurationName)
} else {
// If any dependency is added to this configuration, report an error:
project.configurations.getByName(apiConfigurationName).dependencies.all { dependency ->
throw InvalidUserCodeException(
"API dependencies are not allowed for Android source set ${androidSourceSet.name}. " +
"Please use an implementation dependency instead."
)
}
}
project.addExtendsFromRelation(androidSourceSet.implementationConfigurationName, implementationConfigurationName) project.addExtendsFromRelation(androidSourceSet.implementationConfigurationName, implementationConfigurationName)
project.addExtendsFromRelation(androidSourceSet.compileOnlyConfigurationName, compileOnlyConfigurationName) project.addExtendsFromRelation(androidSourceSet.compileOnlyConfigurationName, compileOnlyConfigurationName)
project.addExtendsFromRelation(androidSourceSet.runtimeOnlyConfigurationName, runtimeOnlyConfigurationName) project.addExtendsFromRelation(androidSourceSet.runtimeOnlyConfigurationName, runtimeOnlyConfigurationName)