Last active
November 7, 2023 19:20
-
-
Save handstandsam/7c68b84e89ed27351df109152254c127 to your computer and use it in GitHub Desktop.
Work in Progress (Not finished) : Kotlin Script to take an AAR file and then strip a list of Classes out of it.
This file contains 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
fun runCmd( | |
cwd: File, | |
cmd: String | |
) = runCatching { | |
println("In cwd ${cwd.path}, Running cmd $cmd") | |
ProcessBuilder("\\s".toRegex().split(cmd)) | |
.directory(cwd) | |
.redirectOutput(ProcessBuilder.Redirect.PIPE) | |
.redirectError(ProcessBuilder.Redirect.PIPE) | |
.start() | |
.inputStream.bufferedReader().readText() | |
}.onFailure { it.printStackTrace() }.getOrNull() | |
fun main() { | |
val testDir = File("/Users/samedwards/Downloads/jarcompare/test") | |
val origAarFile = File(testDir, "lib.aar") | |
val repackagedAarFile = File(testDir, "repackaged.aar") | |
val aarUnzippedDir = File(testDir, "aar") | |
val classesUnzippedDir = File(testDir, "classes") | |
val classesJarFile = File(aarUnzippedDir, "classes.jar") | |
val intersectionsFile = File(testDir, "intersection.txt") | |
listOf( | |
classesUnzippedDir, | |
aarUnzippedDir, | |
repackagedAarFile | |
).forEach { | |
if (it.exists()) { | |
if (it.isDirectory) { | |
it.deleteRecursively() | |
} else { | |
it.delete() | |
} | |
} | |
} | |
runCmd(testDir, "unzip ${origAarFile.canonicalPath} -d ${aarUnzippedDir.name}") | |
runCmd(testDir, "unzip ${classesJarFile.canonicalPath} -d ${classesUnzippedDir.name}") | |
val exclusions = intersectionsFile.readText().lines().filter { it.isNotBlank() }.map { | |
it.replace(".//", "") | |
} | |
exclusions.forEach { str -> | |
val classFileToExclude = File(classesUnzippedDir, str) | |
println("Deleting Class File ${classFileToExclude.path}") | |
classFileToExclude.delete() | |
} | |
// Delete the empty directories | |
classesUnzippedDir.walkBottomUp().forEach { | |
if (it.exists() && it.isDirectory && it.listFiles().isEmpty()) { | |
it.deleteRecursively() | |
} | |
} | |
classesJarFile.delete() | |
runCmd( | |
classesUnzippedDir, | |
"zip -vr ../${aarUnzippedDir.name}/${classesJarFile.name} ./ -x \"*.DS_Store\"" | |
) | |
runCmd(aarUnzippedDir, "zip -vr ../${repackagedAarFile.name} ./ -x \"*.DS_Store\"") | |
} |
This file contains 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
.//com/something/RealLauncher_Factory.class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment