Repository: GradleUp/shadow
Pull request #1530 (2025-07-21 02:26:39)

Fail build when inputting AAR files or using Shadow with AGP
Refs #1454.

---

- [x] [CHANGELOG](https://github.com/GradleUp/shadow/blob/main/docs/changes/README.md)'s "Unreleased" section has been updated, if applicable.



Patch:
diff --git a/.github/renovate.json5 b/.github/renovate.json5
index 91920c87..5b51e7a0 100644
--- a/.github/renovate.json5
+++ b/.github/renovate.json5
@@ -13,7 +13,14 @@
       "matchPackageNames": [
         "org.vafer:jdependency",
         "org.ow2.asm:asm-commons"
-      ]
-    }
+      ],
+    },
+    {
+      // AGP version should match the min Gradle version used in tests.
+      "enabled": false,
+      "matchPackageNames": [
+        'com.android.tools.build:gradle',
+      ],
+    },
   ]
 }
diff --git a/build.gradle.kts b/build.gradle.kts
index 2993c976..b2a6fc6c 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -117,6 +117,7 @@ dependencies {
   implementation(libs.plexus.utils)
   implementation(libs.plexus.xml)
 
+  testPluginClasspath(libs.agp)
   testPluginClasspath(libs.foojayResolver)
   testPluginClasspath(libs.kotlin.kmp)
   testPluginClasspath(libs.pluginPublish)
diff --git a/docs/changes/README.md b/docs/changes/README.md
index bbc50ecf..b9ea958c 100644
--- a/docs/changes/README.md
+++ b/docs/changes/README.md
@@ -5,6 +5,7 @@
 **Added**
 
 - Let `assemble` depend on `shadowJar`. ([#1524](https://github.com/GradleUp/shadow/pull/1524))
+- Fail build when inputting AAR files or using Shadow with AGP. ([#1530](https://github.com/GradleUp/shadow/pull/1530))
 
 **Fixed**
 
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 0a2fcf01..bb9f2568 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -21,6 +21,9 @@ moshi-kotlin = { module = "com.squareup.moshi:moshi-kotlin", version.ref = "mosh
 foojayResolver = "org.gradle.toolchains.foojay-resolver-convention:org.gradle.toolchains.foojay-resolver-convention.gradle.plugin:1.0.0"
 kotlin-kmp = { module = "org.jetbrains.kotlin.multiplatform:org.jetbrains.kotlin.multiplatform.gradle.plugin", version.ref = "kotlin" }
 pluginPublish = { module = "com.gradle.publish:plugin-publish-plugin", version.ref = "pluginPublish" }
+# AGP version should match the min Gradle version used in tests.
+# https://developer.android.com/build/releases/gradle-plugin#updating-gradle
+agp = "com.android.tools.build:gradle:8.8.0"
 
 androidx-gradlePluginLints = "androidx.lint:lint-gradle:1.0.0-alpha05"
 # Dummy to get renovate updates, the version is used in rootProject build.gradle with spotless.
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt
index b35c5dc2..1f0b52a8 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt
@@ -22,6 +22,12 @@ public abstract class ShadowPlugin : Plugin<Project> {
     withId(KOTLIN_MULTIPLATFORM_PLUGIN_ID) {
       apply(ShadowKmpPlugin::class.java)
     }
+    withId("com.android.base") {
+      error(
+        "Shadow does not support using with AGP, you may need Android Fused Library plugin instead. " +
+          "See https://developer.android.com/build/publish-library/fused-library",
+      )
+    }
 
     // Apply the legacy plugin last.
     // Because we apply the ShadowJavaPlugin/ShadowApplication plugin in a withType callback for the
diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt
index 28422774..b63133c4 100644
--- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt
+++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt
@@ -311,7 +311,19 @@ public abstract class ShadowJar :
 
   @TaskAction
   override fun copy() {
-    from(includedDependencies.files.map { archiveOperations.zipTree(it) })
+    from(
+      includedDependencies.files.map { file ->
+        if (file.extension.equals("aar", ignoreCase = true)) {
+          val message = """
+            Shadowing AAR file is not supported.
+            Please exclude dependency artifact: $file
+            or use Android Fused Library plugin instead. See https://developer.android.com/build/publish-library/fused-library.
+          """.trimIndent()
+          error(message)
+        }
+        archiveOperations.zipTree(file)
+      },
+    )
     injectMultiReleaseAttrIfPresent()
     super.copy()
   }


Test patch:
diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/AndroidPluginTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/AndroidPluginTest.kt
new file mode 100644
index 00000000..6cce09a7
--- /dev/null
+++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/AndroidPluginTest.kt
@@ -0,0 +1,29 @@
+package com.github.jengelman.gradle.plugins.shadow
+
+import assertk.assertThat
+import assertk.assertions.contains
+import kotlin.io.path.writeText
+import org.junit.jupiter.params.ParameterizedTest
+import org.junit.jupiter.params.provider.MethodSource
+
+class AndroidPluginTest : BasePluginTest() {
+  @ParameterizedTest
+  @MethodSource("androidIdsProvider")
+  fun doesNotCompatAgp(pluginId: String) {
+    projectScriptPath.writeText(getDefaultProjectBuildScript(plugin = pluginId))
+
+    assertThat(runWithFailure().output).contains(
+      "Shadow does not support using with AGP, you may need Android Fused Library plugin instead.",
+    )
+  }
+
+  private companion object {
+    @JvmStatic
+    fun androidIdsProvider() = listOf(
+      "com.android.application",
+      "com.android.library",
+      "com.android.test",
+      "com.android.dynamic-feature",
+    )
+  }
+}
diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt
index 71b69c5b..b205fa18 100644
--- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt
+++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginTest.kt
@@ -596,6 +596,26 @@ class JavaPluginTest : BasePluginTest() {
     assertThat(result.output).containsMatch("Cannot expand ZIP '.*bad\\.jar'".toRegex())
   }
 
+  @Test
+  fun failBuildIfProcessingAar() {
+    val fooAarPath = path("foo.aar")
+
+    projectScriptPath.appendText(
+      """
+        dependencies {
+          ${implementationFiles(fooAarPath)}
+        }
+      """.trimIndent(),
+    )
+
+    val result = runWithFailure(shadowJarTask)
+
+    assertThat(result.output).contains(
+      "Shadowing AAR file is not supported.",
+      "Please exclude dependency artifact:",
+    )
+  }
+
   @Test
   fun worksWithArchiveFileName() {
     val mainClassEntry = writeClass()