Skip to content

Commit 69dfdfc

Browse files
CopilotGoooler
andauthored
Improve file type check for AAR (#1987)
* Initial plan * Update AAR detection to check actual file content instead of extension Co-authored-by: Goooler <10363352+Goooler@users.noreply.github.com> * Update JavaPluginsTest.kt * Reformat * Check extension as well --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Goooler <10363352+Goooler@users.noreply.github.com> Co-authored-by: Zongle Wang <wangzongler@gmail.com>
1 parent 1395087 commit 69dfdfc

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

  • src
    • functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow
    • main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks

src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/JavaPluginsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ class JavaPluginsTest : BasePluginTest() {
853853

854854
@Test
855855
fun failBuildIfProcessingAar() {
856-
val fooAarPath = path("foo.aar")
856+
val fooAarPath = buildJar("foo.aar") { insert("AndroidManifest.xml", "<manifest/>") }
857857

858858
projectScript.appendText(
859859
"""

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransf
2626
import java.io.File
2727
import java.io.IOException
2828
import java.util.jar.JarFile
29+
import java.util.zip.ZipException
30+
import java.util.zip.ZipFile
2931
import javax.inject.Inject
3032
import kotlin.reflect.full.hasAnnotation
3133
import org.apache.tools.zip.Zip64Mode
@@ -451,7 +453,7 @@ public abstract class ShadowJar : Jar() {
451453
file.isDirectory -> {
452454
from(file)
453455
}
454-
file.extension.equals("aar", ignoreCase = true) -> {
456+
file.extension.equals("aar", ignoreCase = true) && file.isAar() -> {
455457
val message =
456458
"""
457459
Shadowing AAR file is not supported.
@@ -666,3 +668,11 @@ public abstract class ShadowJar : Jar() {
666668
}
667669
}
668670
}
671+
672+
private fun File.isAar(): Boolean =
673+
try {
674+
ZipFile(this).use { zip -> zip.getEntry("AndroidManifest.xml") != null }
675+
} catch (_: ZipException) {
676+
// File is not a valid ZIP, so it cannot be an AAR.
677+
false
678+
}

0 commit comments

Comments
 (0)