Created
October 31, 2025 15:27
-
-
Save sdetilly/fc1751c8833a1442cec8b3894b8eec31 to your computer and use it in GitHub Desktop.
Repackaging conflicting aars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plugins { | |
| id("com.github.johnrengelman.shadow") version "8.1.1" | |
| } | |
| configurations { | |
| create("aarToRepackage") | |
| } | |
| dependencies { | |
| // Point to the original Lib-A AAR | |
| "aarToRepackage"(files("../path/to/Lib-A.aar")) | |
| } | |
| tasks { | |
| val buildDir = layout.buildDirectory.get().asFile | |
| register("extractAar") { | |
| val aarFile = file("../path/to/Lib-A.aar") | |
| val extractDir = file("$buildDir/extracted-aar") | |
| inputs.file(aarFile) | |
| outputs.dir(extractDir) | |
| doLast { | |
| extractDir.deleteRecursively() | |
| extractDir.mkdirs() | |
| // Extract AAR | |
| copy { | |
| from(zipTree(aarFile)) | |
| into(extractDir) | |
| } | |
| println("Extracted AAR to: $extractDir") | |
| } | |
| } | |
| register<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("relocateClasses") { | |
| dependsOn("extractAar") | |
| val extractDir = file("$buildDir/extracted-aar") | |
| from(zipTree(file("$extractDir/classes.jar"))) | |
| // Relocate root-level single-letter obfuscated packages | |
| // Use specific patterns to avoid matching android.*, androidx.*, etc. | |
| relocate("b.", "com.new.package.name.obf.b.") { | |
| // Include classes directly in package 'b' | |
| include("b/**") | |
| // Exclude anything that's part of com.l1inc public API | |
| exclude("com/**") | |
| } | |
| relocate("a.", "com.new.package.name.obf.a.") { | |
| include("a/**") | |
| exclude("com/**") | |
| // Explicitly exclude android packages | |
| exclude("android/**") | |
| exclude("androidx/**") | |
| } | |
| relocate("c.", "com.new.package.name.obf.c.") { | |
| include("c/**") | |
| exclude("com/**") | |
| } | |
| archiveFileName.set("classes-relocated.jar") | |
| destinationDirectory.set(file("$buildDir/relocated")) | |
| } | |
| register<Zip>("repackageAar") { | |
| dependsOn("relocateClasses") | |
| val extractDir = file("$buildDir/extracted-aar") | |
| val relocatedJar = file("$buildDir/relocated/classes-relocated.jar") | |
| from(extractDir) { | |
| exclude("classes.jar") | |
| } | |
| from(relocatedJar) { | |
| rename { "classes.jar" } | |
| } | |
| archiveFileName.set("Lib-A_relocated.aar") | |
| destinationDirectory.set(file("$buildDir/outputs")) | |
| doLast { | |
| println("Repackaged AAR created at: $buildDir/outputs/Lib-A_relocated.aar") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment