Add a check of the AGP version

This commit is contained in:
Sven Cuyt
2021-05-21 18:06:38 +02:00
committed by maqsood ahmad
parent 4d84aa543f
commit 8b1bd44bca
2 changed files with 71 additions and 0 deletions

View File

@@ -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
}
}

View File

@@ -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."
}
}
}
})