Add checks for configured variants

This commit is contained in:
James Hamilton
2021-05-21 20:31:28 +02:00
committed by maqsood ahmad
parent 6cf1999f52
commit 1a7959d978
2 changed files with 184 additions and 8 deletions

View File

@@ -63,23 +63,40 @@ class AndroidPlugin(private val androidExtension: BaseExtension) : Plugin<Projec
collectConsumerRulesTask)
project.afterEvaluate {
if (proguardBlock.configurations.isEmpty())
throw GradleException("There are no configured variants in the 'proguard' block")
val matchedConfigurations = mutableListOf<VariantConfiguration>()
when (androidExtension) {
is AppExtension -> androidExtension.applicationVariants.all { applicationVariant ->
setupVariant(proguardBlock, applicationVariant, collectConsumerRulesTask, project)?.let {
matchedConfigurations.add(it)
}
}
is LibraryExtension -> androidExtension.libraryVariants.all { libraryVariant ->
setupVariant(proguardBlock, libraryVariant, collectConsumerRulesTask, project)?.let {
matchedConfigurations.add(it)
}
}
}
proguardBlock.configurations.forEach {
checkConfigurationFile(project, it.configurations)
}
when (androidExtension) {
is AppExtension -> androidExtension.applicationVariants.all { applicationVariant ->
setupVariant(proguardBlock, applicationVariant, collectConsumerRulesTask, project)
}
is LibraryExtension -> androidExtension.libraryVariants.all { libraryVariant ->
setupVariant(proguardBlock, libraryVariant, collectConsumerRulesTask, project)
(proguardBlock.configurations - matchedConfigurations).apply {
if (isNotEmpty()) when (size) {
1 -> throw GradleException("The configured variant '${first().name}' does not exist")
else -> throw GradleException("The configured variants ${joinToString(separator = "', '", prefix = "'", postfix = "'") { it.name }} do not exist")
}
}
}
}
private fun setupVariant(proguardBlock: ProGuardAndroidExtension, variant: BaseVariant, collectConsumerRulesTask: TaskProvider<Task>, project: Project) {
if (proguardBlock.configurations.hasVariantConfiguration(variant.name)) {
private fun setupVariant(proguardBlock: ProGuardAndroidExtension, variant: BaseVariant, collectConsumerRulesTask: TaskProvider<Task>, project: Project): VariantConfiguration? {
val matchingConfiguration = proguardBlock.configurations.findVariantConfiguration(variant.name)
if (matchingConfiguration != null) {
verifyNotMinified(variant)
collectConsumerRulesTask.dependsOn(createCollectConsumerRulesTask(
@@ -88,6 +105,7 @@ class AndroidPlugin(private val androidExtension: BaseExtension) : Plugin<Projec
createConsumerRulesConfiguration(project, variant),
File("${project.buildDir}/intermediates/proguard/configs")))
}
return matchingConfiguration
}
private fun createCollectConsumerRulesTask(

View File

@@ -123,4 +123,162 @@ class ConfigurationTest : FreeSpec({
}
}
}
"Given a project no configured variants" - {
val project = autoClose(AndroidProject().apply {
addModule(applicationModule("app", buildDotGradle = """
plugins {
id 'com.android.application'
id 'proguard'
}
android {
compileSdkVersion 30
buildTypes {
release {
minifyEnabled true
}
}
}
proguard {
configurations {
}
}""".trimIndent()))
}.create())
"When the project is evaluated" - {
val result = createGradleRunner(project.rootDir, testKitDir).buildAndFail()
"Then the build should fail with an error message" {
result.output shouldContain "There are no configured variants in the 'proguard' block"
}
}
}
"Given a project configured with a variant that does not exist" - {
val project = autoClose(AndroidProject().apply {
addModule(applicationModule("app", buildDotGradle = """
plugins {
id 'com.android.application'
id 'proguard'
}
android {
compileSdkVersion 30
buildTypes {
release {
minifyEnabled true
}
}
}
proguard {
configurations {
foo {
defaultConfiguration 'proguard-android.txt'
}
}
}""".trimIndent()))
}.create())
"When the project is evaluated" - {
val result = createGradleRunner(project.rootDir, testKitDir).buildAndFail()
"Then the build should fail with an error message" {
result.output shouldContain "The configured variant 'foo' does not exist"
}
}
}
"Given a project configured with multiple variants that do not exist" - {
val project = autoClose(AndroidProject().apply {
addModule(applicationModule("app", buildDotGradle = """
plugins {
id 'com.android.application'
id 'proguard'
}
android {
compileSdkVersion 30
buildTypes {
release {
minifyEnabled true
}
}
}
proguard {
configurations {
foo {
defaultConfiguration 'proguard-android.txt'
}
bar {
defaultConfiguration 'proguard-android.txt'
}
}
}""".trimIndent()))
}.create())
"When the project is evaluated" - {
val result = createGradleRunner(project.rootDir, testKitDir).buildAndFail()
"Then the build should fail with an error message" {
result.output shouldContain "The configured variants (('foo', 'bar')|('bar', 'foo')) do not exist".toRegex()
}
}
}
"Given a project configured with a flavor variant that does not exist" - {
val project = autoClose(AndroidProject().apply {
addModule(applicationModule("app", buildDotGradle = """
plugins {
id 'com.android.application'
id 'proguard'
}
android {
compileSdkVersion 30
buildTypes {
release {
minifyEnabled true
}
}
flavorDimensions "version"
productFlavors {
demo {
dimension "version"
applicationIdSuffix ".demo"
versionNameSuffix "-demo"
}
full {
dimension "version"
applicationIdSuffix ".full"
versionNameSuffix "-full"
}
}
}
proguard {
configurations {
fooRelease {
defaultConfiguration 'proguard-android.txt'
}
}
}""".trimIndent()))
}.create())
"When the project is evaluated" - {
val result = createGradleRunner(project.rootDir, testKitDir).buildAndFail()
"Then the build should fail with an error message" {
result.output shouldContain "The configured variant 'fooRelease' does not exist"
}
}
}
})