mirror of
https://github.com/Guardsquare/proguard.git
synced 2026-03-13 09:50:34 +08:00
Add a check of the AGP version
This commit is contained in:
@@ -25,6 +25,7 @@ import com.android.build.gradle.BaseExtension
|
||||
import org.gradle.api.GradleException
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.util.VersionNumber
|
||||
import proguard.gradle.plugin.android.AndroidPlugin
|
||||
|
||||
class ProGuardPlugin : Plugin<Project> {
|
||||
@@ -41,6 +42,13 @@ class ProGuardPlugin : Plugin<Project> {
|
||||
| }""".trimMargin()
|
||||
when {
|
||||
androidExtension != null -> {
|
||||
if (!isAGPVersionSupported(project)) {
|
||||
throw GradleException(
|
||||
"""The ProGuard plugin only supports Android plugin 4 and higher.
|
||||
|For Android plugin version 3 and lower, you can use ProGuard through the Android plugin integration.
|
||||
|Please refer to the manual for further details: https://www.guardsquare.com/manual/setup/gradleplugin
|
||||
""".trimMargin())
|
||||
}
|
||||
AndroidPlugin(androidExtension).apply(project)
|
||||
}
|
||||
javaExtension != null -> {
|
||||
@@ -57,4 +65,13 @@ class ProGuardPlugin : Plugin<Project> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isAGPVersionSupported(project: Project): Boolean {
|
||||
val version = project.rootProject.buildscript.configurations.getByName("classpath").resolvedConfiguration.resolvedArtifacts.find { module ->
|
||||
module.moduleVersion.id.group.equals("com.android.tools.build")
|
||||
}?.moduleVersion?.id?.version
|
||||
return version?.let {
|
||||
return VersionNumber.parse(it).major >= 4
|
||||
} ?: false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,58 @@ class ProGuardPluginTest : FreeSpec({
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"Given a project with an old Android Gradle plugin" - {
|
||||
val project = autoClose(AndroidProject("""
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral() // For anything else.
|
||||
google() // For the Android plugin.
|
||||
flatDir {
|
||||
dirs "${System.getProperty("local.repo")}"
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
classpath "com.android.tools.build:gradle:3.6.3"
|
||||
classpath ":proguard-gradle:${System.getProperty("proguard.version")}"
|
||||
}
|
||||
}
|
||||
allprojects {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
""".trimIndent()).apply {
|
||||
addModule(applicationModule("app", buildDotGradle = """
|
||||
plugins {
|
||||
id 'com.android.application'
|
||||
id 'proguard'
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
|
||||
buildTypes {
|
||||
release {}
|
||||
debug {}
|
||||
}
|
||||
}
|
||||
|
||||
proguard {
|
||||
configurations {
|
||||
release {}
|
||||
}
|
||||
}
|
||||
""".trimIndent()))
|
||||
}.create())
|
||||
|
||||
"When the project is evaluated" - {
|
||||
val result = createGradleRunner(project.rootDir, testKitDir).buildAndFail()
|
||||
|
||||
"Then the build should fail" {
|
||||
result.output shouldContain "The ProGuard plugin only supports Android plugin 4 and higher."
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user