Move more code into buildSrc

This commit is contained in:
topjohnwu
2021-09-09 20:19:49 -07:00
parent 24a8b41182
commit 136d8c39d9
5 changed files with 210 additions and 190 deletions

View File

@@ -1,8 +1,3 @@
import org.gradle.internal.os.OperatingSystem
import java.io.OutputStream
import java.io.PrintStream
import java.nio.file.Paths
plugins {
id("com.android.application")
id("io.michaelrocks.paranoid")
@@ -13,8 +8,6 @@ paranoid {
includeSubprojects = true
}
setupApp()
android {
val canary = !Config.version.contains(".")
@@ -43,93 +36,7 @@ android {
}
}
// Make sure we have a working manifest while building
val ensureManifest by tasks.registering {
val manifest = file("src/main/AndroidManifest.xml")
if (!manifest.exists()) {
PrintStream(manifest).use {
it.println("<manifest package=\"com.topjohnwu.magisk\"/>")
}
}
}
tasks.preBuild {
dependsOn(ensureManifest)
}
android.applicationVariants.all {
val manifest = file("src/main/AndroidManifest.xml")
val outSrcDir = File(buildDir, "generated/source/obfuscate/$name")
val templateDir = file("template")
val resDir = file("res")
val androidJar = Paths.get(android.sdkDirectory.path, "platforms",
android.compileSdkVersion, "android.jar")
val aaptCommand = if (OperatingSystem.current().isWindows) "aapt2.exe" else "aapt2"
val aapt = Paths.get(android.sdkDirectory.path,
"build-tools", android.buildToolsVersion, aaptCommand)
val dummy = object : OutputStream() {
override fun write(b: Int) {}
override fun write(bytes: ByteArray, off: Int, len: Int) {}
}
val genSrcTask = tasks.register("generate${name.capitalize()}ObfuscatedSources") {
doLast {
val xml = genStubManifest(templateDir, outSrcDir)
PrintStream(manifest).use {
it.print(xml)
}
val compileTmp = File.createTempFile("tmp", ".zip")
val linkTmp = File.createTempFile("tmp", ".zip")
val optTmp = File.createTempFile("tmp", ".zip")
val stubXml = File.createTempFile("tmp", ".xml")
try {
PrintStream(stubXml).use {
it.println("<manifest package=\"com.topjohnwu.magisk\"/>")
}
exec {
commandLine(aapt, "compile",
"-o", compileTmp,
"--dir", resDir)
standardOutput = dummy
errorOutput = dummy
}
exec {
commandLine(aapt, "link",
"-o", linkTmp,
"-I", androidJar,
"--min-sdk-version", android.defaultConfig.minSdk,
"--target-sdk-version", android.defaultConfig.targetSdk,
"--manifest", stubXml,
"--java", outSrcDir, compileTmp)
standardOutput = dummy
errorOutput = dummy
}
exec {
commandLine(aapt, "optimize",
"-o", optTmp,
"--collapse-resource-names", linkTmp)
standardOutput = dummy
errorOutput = dummy
}
genEncryptedResources(optTmp, outSrcDir)
} finally {
compileTmp.delete()
linkTmp.delete()
optTmp.delete()
stubXml.delete()
}
}
}
registerJavaGeneratingTask(genSrcTask, outSrcDir)
}
setupStub()
dependencies {
implementation(project(":app:shared"))