Skip to content

Instantly share code, notes, and snippets.

@sdetilly
Created October 31, 2025 15:27
Show Gist options
  • Select an option

  • Save sdetilly/fc1751c8833a1442cec8b3894b8eec31 to your computer and use it in GitHub Desktop.

Select an option

Save sdetilly/fc1751c8833a1442cec8b3894b8eec31 to your computer and use it in GitHub Desktop.
Repackaging conflicting aars
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