Include configuration files in the order they were specified

This commit is contained in:
James Hamilton
2021-05-22 18:51:40 +02:00
committed by maqsood ahmad
parent d9c64430b4
commit 6330b9eff9
7 changed files with 128 additions and 53 deletions

View File

@@ -37,6 +37,8 @@ import org.gradle.api.tasks.TaskProvider
import proguard.gradle.plugin.android.AndroidProjectType.ANDROID_APPLICATION
import proguard.gradle.plugin.android.AndroidProjectType.ANDROID_LIBRARY
import proguard.gradle.plugin.android.dsl.ProGuardAndroidExtension
import proguard.gradle.plugin.android.dsl.ProGuardConfiguration
import proguard.gradle.plugin.android.dsl.UserProGuardConfiguration
import proguard.gradle.plugin.android.dsl.VariantConfiguration
import proguard.gradle.plugin.android.tasks.CollectConsumerRulesTask
import proguard.gradle.plugin.android.transforms.AndroidConsumerRulesTransform
@@ -113,9 +115,9 @@ class AndroidPlugin(private val androidExtension: BaseExtension) : Plugin<Projec
it.attributes.attribute(ATTRIBUTE_ARTIFACT_TYPE, ARTIFACT_TYPE_CONSUMER_RULES)
}
private fun checkConfigurationFile(project: Project, files: List<String>) {
files.forEach { fileName ->
val file = project.file(fileName)
private fun checkConfigurationFile(project: Project, files: List<ProGuardConfiguration>) {
files.filterIsInstance<UserProGuardConfiguration>().forEach {
val file = project.file(it.path)
if (!file.exists()) throw GradleException("ProGuard configuration file ${file.absolutePath} was set but does not exist.")
}
}

View File

@@ -42,7 +42,6 @@ import com.android.build.gradle.BaseExtension
import java.io.File
import org.gradle.api.Project
import proguard.gradle.ProGuardTask
import proguard.gradle.ProGuardTask.DEFAULT_CONFIG_RESOURCE_PREFIX
import proguard.gradle.plugin.android.AndroidPlugin.Companion.COLLECT_CONSUMER_RULES_TASK_NAME
import proguard.gradle.plugin.android.AndroidProjectType.ANDROID_APPLICATION
import proguard.gradle.plugin.android.AndroidProjectType.ANDROID_LIBRARY
@@ -74,8 +73,7 @@ class ProGuardTransform(
// TODO: collect aapt rules
proguardTask.configuration(project.tasks.getByPath(COLLECT_CONSUMER_RULES_TASK_NAME + variantName.capitalize()).outputs.files)
proguardTask.configuration(variantBlock.configurations.map { project.file(it) })
proguardTask.configuration(variantBlock.defaultConfigurations.map { "$DEFAULT_CONFIG_RESOURCE_PREFIX/${it.filename}" })
proguardTask.configuration(variantBlock.configurations.map { project.file(it.path) })
val mappingDir = File("${project.buildDir.absolutePath}/outputs/proguard/$variantName/mapping")
if (!mappingDir.exists()) mappingDir.mkdirs()

View File

@@ -1,42 +0,0 @@
/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2021 Guardsquare NV
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package proguard.gradle.plugin.android.dsl
enum class DefaultConfiguration(val filename: String) {
ANDROID_DEBUG("proguard-android-debug.txt"),
ANDROID_RELEASE("proguard-android.txt"),
ANDROID_RELEASE_OPTIMIZE("proguard-android-optimize.txt");
override fun toString(): String = filename
companion object {
fun fromString(string: String): DefaultConfiguration =
values().find { it.filename == string }
?: throw IllegalArgumentException("""
The default ProGuard configuration '$string' is not invalid.
Choose from:
${values().joinToString(separator = ", ")}
""".trimIndent())
}
}

View File

@@ -0,0 +1,57 @@
/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2021 Guardsquare NV
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package proguard.gradle.plugin.android.dsl
import proguard.gradle.ProGuardTask.DEFAULT_CONFIG_RESOURCE_PREFIX
sealed class ProGuardConfiguration(val filename: String) {
open val path: String = filename
override fun toString(): String = filename
}
class UserProGuardConfiguration(filename: String) : ProGuardConfiguration(filename)
class DefaultProGuardConfiguration private constructor(filename: String) : ProGuardConfiguration(filename) {
override val path: String
get() = "$DEFAULT_CONFIG_RESOURCE_PREFIX/$filename"
companion object {
private val ANDROID_DEBUG = DefaultProGuardConfiguration("proguard-android-debug.txt")
private val ANDROID_RELEASE = DefaultProGuardConfiguration("proguard-android.txt")
private val ANDROID_RELEASE_OPTIMIZE = DefaultProGuardConfiguration("proguard-android-optimize.txt")
fun fromString(filename: String): ProGuardConfiguration {
return when (filename) {
ANDROID_DEBUG.filename -> ANDROID_DEBUG
ANDROID_RELEASE.filename -> ANDROID_RELEASE
ANDROID_RELEASE_OPTIMIZE.filename -> ANDROID_RELEASE
else -> throw IllegalArgumentException("""
The default ProGuard configuration '$filename' is not invalid.
Choose from:
$ANDROID_DEBUG, $ANDROID_RELEASE, $ANDROID_RELEASE_OPTIMIZE
""".trimIndent())
}
}
}
}

View File

@@ -22,8 +22,7 @@
package proguard.gradle.plugin.android.dsl
class VariantConfiguration(val name: String) {
var configurations = mutableListOf<String>()
var defaultConfigurations = mutableListOf<DefaultConfiguration>()
var configurations = mutableListOf<ProGuardConfiguration>()
var consumerRuleFilter = mutableListOf<String>()
fun configurations(vararg configs: String) {
@@ -31,7 +30,7 @@ class VariantConfiguration(val name: String) {
}
fun configuration(config: String) {
configurations.add(config)
configurations.add(UserProGuardConfiguration(config))
}
fun defaultConfigurations(vararg configs: String) {
@@ -39,7 +38,7 @@ class VariantConfiguration(val name: String) {
}
fun defaultConfiguration(config: String) {
defaultConfigurations.add(DefaultConfiguration.fromString(config))
configurations.add(DefaultProGuardConfiguration.fromString(config))
}
fun consumerRuleFilter(vararg filters: String) {

View File

@@ -0,0 +1,61 @@
/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2021 Guardsquare NV
*/
package proguard.gradle.plugin.android.dsl
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.collections.shouldExistInOrder
import testutils.AndroidProject
import testutils.SourceFile
import testutils.applicationModule
import testutils.createGradleRunner
import testutils.createTestKitDir
class ConfigurationOrderTest : FreeSpec({
val testKitDir = createTestKitDir()
"Given a project with multiple configuration files" - {
val project = autoClose(AndroidProject().apply {
addModule(applicationModule("app", buildDotGradle = """
plugins {
id 'com.android.application'
id 'proguard'
}
android {
compileSdkVersion 30
buildTypes {
release {
minifyEnabled false
}
}
}
proguard {
configurations {
release {
configuration 'proguard-project1.txt'
defaultConfiguration 'proguard-android.txt'
configuration 'proguard-project2.txt'
}
}
}""".trimIndent(),
additionalFiles = listOf(SourceFile("proguard-project1.txt"), SourceFile("proguard-project2.txt"))))
}.create())
"When the project is built" - {
val result = createGradleRunner(project.rootDir, testKitDir, "assembleRelease", "--info").build()
"The configurations should be included in order" {
result.output.lines().shouldExistInOrder(
{ it.contains("proguard-project1.txt") },
{ it.contains("proguard-android.txt") },
{ it.contains("proguard-project2.txt") })
}
}
}
})

View File

@@ -17,7 +17,7 @@ interface ProjectFile {
fun create(moduleDir: File)
}
class SourceFile(override val path: String, val source: String) : ProjectFile {
class SourceFile(override val path: String, val source: String = "") : ProjectFile {
override fun create(moduleDir: File) {
val file = File(moduleDir, path)
file.parentFile.mkdirs()