diff --git a/gradle-plugin/src/main/kotlin/proguard/gradle/plugin/ProGuardPlugin.kt b/gradle-plugin/src/main/kotlin/proguard/gradle/plugin/ProGuardPlugin.kt index 96adcd21..87aa8d2b 100644 --- a/gradle-plugin/src/main/kotlin/proguard/gradle/plugin/ProGuardPlugin.kt +++ b/gradle-plugin/src/main/kotlin/proguard/gradle/plugin/ProGuardPlugin.kt @@ -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 { @@ -41,6 +42,13 @@ class ProGuardPlugin : Plugin { | }""".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 { } } } + + 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 + } } diff --git a/gradle-plugin/src/test/kotlin/proguard/gradle/ProGuardPluginTest.kt b/gradle-plugin/src/test/kotlin/proguard/gradle/ProGuardPluginTest.kt index 364c409c..83901307 100644 --- a/gradle-plugin/src/test/kotlin/proguard/gradle/ProGuardPluginTest.kt +++ b/gradle-plugin/src/test/kotlin/proguard/gradle/ProGuardPluginTest.kt @@ -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." + } + } + } })