Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob committed Aug 20, 2022
1 parent 98c6e26 commit 5389ed3
Showing 1 changed file with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,37 @@ class AndroidMinificationPlugin : BasePlugin() {
lintTasksDependOnProguardRulesTask(extractMinificationRules)
project.afterEvaluate {
android.variants.configureEach { variant ->
if (variant.flavorName != "") {
val isFlavorless = variant.flavorName == ""
if (!isFlavorless) {
// Cannot do this simply because AGP doesn't provide a DSL surface for adding proguard rules for variants.
// It's possible to add for buildType and flavors, but since the mapping file is for variants,
// the generate... task would not be able to create a distinct file.
project.logger.warn("This project uses flavors, it's not possible to generate variant based rules for ${variant.name}.")
}
val generateProguardRulesTask =
createGenerateProguardMinificationRulesTask(variant, generatedProguardRulesFile)
if (variant.flavorName == "") {
if (isFlavorless) {
lintTasksDependOnProguardRulesTask(generateProguardRulesTask)
}
proguardTaskClass
?.let { project.tasks.withType(it) }
?.configureEach { obfuscationTask ->
if (obfuscationTask.variantName == variant.name) {
obfuscationTask.dependsOn(extractMinificationRules)
if (variant.flavorName == "") {
if (isFlavorless) {
obfuscationTask.dependsOn(generateProguardRulesTask)
}
}
}
val generateR8RulesTask =
createGenerateR8MinificationRulesTask(variant, generatedProguardRulesFile)
if (variant.flavorName == "") {
if (isFlavorless) {
lintTasksDependOnProguardRulesTask(generateR8RulesTask)
}
project.tasks.withType<R8Task>().configureEach { obfuscationTask ->
if (obfuscationTask.variantName == variant.name) {
obfuscationTask.dependsOn(extractMinificationRules)
if (variant.flavorName == "") {
if (isFlavorless) {
obfuscationTask.dependsOn(generateR8RulesTask)
}
}
Expand Down Expand Up @@ -152,22 +153,15 @@ class AndroidMinificationPlugin : BasePlugin() {
null
}

private fun <T : ProguardConfigurableTask> Project.findMinificationTaskFor(
variant: @Suppress("DEPRECATION" /* AGP 7.0 */) com.android.build.gradle.api.BaseVariant,
taskClass: Class<T>
): T? =
this
.tasks
.withType(taskClass)
.matching { it.variantName == variant.name }
.singleOrNull()

/**
* Duplicate code, see also [createGenerateProguardMinificationRulesTask].
*/
private fun createGenerateR8MinificationRulesTask(
variant: @Suppress("DEPRECATION" /* AGP 7.0 */) com.android.build.gradle.api.BaseVariant,
outputFile: File,
): TaskProvider<Task> =
project.tasks.register<Task>("generate${variant.name.capitalize()}R8MinificationRules") {
description = "Generates printConfiguration and dump options for R8"
description = "Generates printConfiguration for R8 if supported."
val mappingFolder: Provider<File> = variant.mappingFileProvider.map { it.singleFile.parentFile }
inputs.property("targetFolder", mappingFolder)
outputs.file(outputFile)
Expand All @@ -180,12 +174,15 @@ class AndroidMinificationPlugin : BasePlugin() {
}
}

/**
* Duplicate code, see also [createGenerateR8MinificationRulesTask].
*/
private fun createGenerateProguardMinificationRulesTask(
variant: @Suppress("DEPRECATION" /* AGP 7.0 */) com.android.build.gradle.api.BaseVariant,
outputFile: File,
): TaskProvider<Task> =
project.tasks.register<Task>("generate${variant.name.capitalize()}ProguardMinificationRules") {
description = "Generates printConfiguration and dump options for ProGuard"
description = "Generates printConfiguration and dump options for ProGuard."
val mappingFolder: Provider<File> = variant.mappingFileProvider.map { it.singleFile.parentFile }
inputs.property("targetFolder", mappingFolder)
outputs.file(outputFile)
Expand All @@ -194,9 +191,9 @@ class AndroidMinificationPlugin : BasePlugin() {
outputFile.createNewFile()
outputFile.writeText(
"""
-printconfiguration ${mappingFolder.get().resolve("configuration.txt")}
-dump ${mappingFolder.get().resolve("dump.txt")}
""".trimIndent()
-printconfiguration ${mappingFolder.get().resolve("configuration.txt")}
-dump ${mappingFolder.get().resolve("dump.txt")}
""".trimIndent()
)
}
}
Expand Down

0 comments on commit 5389ed3

Please sign in to comment.