Fix gradle build cache

This commit is contained in:
vvb2060 2023-02-20 01:23:56 -08:00 committed by topjohnwu
parent 9b1e173373
commit c1909d520b
2 changed files with 40 additions and 38 deletions

View File

@ -73,8 +73,6 @@ fun genKeyData(keysDir: File, outSrc: File) {
} }
fun genStubManifest(srcDir: File, outDir: File): String { fun genStubManifest(srcDir: File, outDir: File): String {
outDir.deleteRecursively()
fun String.ind(level: Int) = replaceIndentByMargin(" ".repeat(level)) fun String.ind(level: Int) = replaceIndentByMargin(" ".repeat(level))
val cmpList = mutableListOf<String>() val cmpList = mutableListOf<String>()

View File

@ -134,6 +134,7 @@ private fun Project.setupAppCommon() {
lint { lint {
disable += "MissingTranslation" disable += "MissingTranslation"
checkReleaseBuilds = false
} }
dependenciesInfo { dependenciesInfo {
@ -193,7 +194,6 @@ fun Project.setupApp() {
} }
val syncResources by tasks.registering(Sync::class) { val syncResources by tasks.registering(Sync::class) {
dependsOn(syncLibs)
into("src/main/resources/META-INF/com/google/android") into("src/main/resources/META-INF/com/google/android")
from(rootProject.file("scripts/update_binary.sh")) { from(rootProject.file("scripts/update_binary.sh")) {
rename { "update-binary" } rename { "update-binary" }
@ -206,13 +206,16 @@ fun Project.setupApp() {
android.applicationVariants.all { android.applicationVariants.all {
val variantCapped = name.replaceFirstChar { it.uppercase() } val variantCapped = name.replaceFirstChar { it.uppercase() }
tasks.getByPath("merge${variantCapped}JniLibFolders").dependsOn(syncLibs)
processJavaResourcesProvider.configure { dependsOn(syncResources) }
val stubTask = tasks.getByPath(":stub:package$variantCapped") val stubTask = tasks.getByPath(":stub:package$variantCapped")
val stubApk = stubTask.outputs.files.asFileTree.filter { val stubApk = stubTask.outputs.files.asFileTree.filter {
it.name.endsWith(".apk") it.name.endsWith(".apk")
} }
val syncAssets = tasks.register("sync${variantCapped}Assets", Sync::class) { val syncAssets = tasks.register("sync${variantCapped}Assets", Sync::class) {
dependsOn(syncResources, stubTask) dependsOn(stubTask)
inputs.property("version", Config.version) inputs.property("version", Config.version)
inputs.property("versionCode", Config.versionCode) inputs.property("versionCode", Config.versionCode)
into("src/main/assets") into("src/main/assets")
@ -240,8 +243,7 @@ fun Project.setupApp() {
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf")) filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
} }
} }
mergeAssetsProvider.configure { dependsOn(syncAssets) }
preBuildProvider.get().dependsOn(syncAssets)
val keysDir = rootProject.file("tools/keys") val keysDir = rootProject.file("tools/keys")
val outSrcDir = File(buildDir, "generated/source/keydata/$name") val outSrcDir = File(buildDir, "generated/source/keydata/$name")
@ -265,7 +267,8 @@ fun Project.setupStub() {
val variantCapped = name.replaceFirstChar { it.uppercase() } val variantCapped = name.replaceFirstChar { it.uppercase() }
val variantLowered = name.lowercase() val variantLowered = name.lowercase()
val manifest = file("src/${variantLowered}/AndroidManifest.xml") val manifest = file("src/${variantLowered}/AndroidManifest.xml")
val outSrcDir = File(buildDir, "generated/source/obfuscate/${variantLowered}") val outManifestDir = File(buildDir, "generated/source/manifest/${variantLowered}")
val outResDir = File(buildDir, "generated/source/res/${variantLowered}")
val templateDir = file("template") val templateDir = file("template")
val aapt = File(android.sdkDirectory, "build-tools/${android.buildToolsVersion}/aapt2") val aapt = File(android.sdkDirectory, "build-tools/${android.buildToolsVersion}/aapt2")
val apk = File(buildDir, "intermediates/processed_res/" + val apk = File(buildDir, "intermediates/processed_res/" +
@ -273,21 +276,23 @@ fun Project.setupStub() {
val apkTmp = File("${apk}.tmp") val apkTmp = File("${apk}.tmp")
val genManifestTask = tasks.register("generate${variantCapped}ObfuscatedManifest") { val genManifestTask = tasks.register("generate${variantCapped}ObfuscatedManifest") {
inputs.property("seed", RAND_SEED)
inputs.dir(templateDir)
outputs.dir(outManifestDir)
outputs.file(manifest)
doLast { doLast {
val xml = genStubManifest(templateDir, outSrcDir) val xml = genStubManifest(templateDir, outManifestDir)
manifest.parentFile.mkdirs() manifest.parentFile.mkdirs()
PrintStream(manifest).use { PrintStream(manifest).use {
it.print(xml) it.print(xml)
} }
} }
} }
tasks.getByPath(":stub:process${variantCapped}MainManifest").dependsOn(genManifestTask) preBuildProvider.configure { dependsOn(genManifestTask) }
registerJavaGeneratingTask(genManifestTask, outManifestDir)
val genSrcTask = tasks.register("generate${variantCapped}ObfuscatedSources") { val processResourcesTask = tasks.getByPath(":stub:process${variantCapped}Resources")
dependsOn(":stub:process${variantCapped}Resources") processResourcesTask.doLast {
inputs.file(apk)
outputs.file(apk)
doLast {
exec { exec {
commandLine(aapt, "optimize", "-o", apkTmp, "--collapse-resource-names", apk) commandLine(aapt, "optimize", "-o", apkTmp, "--collapse-resource-names", apk)
} }
@ -305,19 +310,18 @@ fun Project.setupStub() {
} }
} }
apkTmp.delete() apkTmp.delete()
genEncryptedResources(ByteArrayInputStream(bos.toByteArray()), outSrcDir) genEncryptedResources(ByteArrayInputStream(bos.toByteArray()), outResDir)
} }
} @Suppress("DEPRECATION")
registerJavaGeneratingTask(genSrcTask, outSrcDir) registerJavaGeneratingTask(processResourcesTask, outResDir)
} }
// Override optimizeReleaseResources task // Override optimizeReleaseResources task
tasks.whenTaskAdded {
if (name == "optimizeReleaseResources") {
dependsOn("generateReleaseObfuscatedSources")
val apk = File(buildDir, "intermediates/processed_res/" + val apk = File(buildDir, "intermediates/processed_res/" +
"release/out/resources-release.ap_") "release/out/resources-release.ap_")
val optRes = File(buildDir, "intermediates/optimized_processed_res/" + val optRes = File(buildDir, "intermediates/optimized_processed_res/" +
"release/resources-release-optimize.ap_") "release/resources-release-optimize.ap_")
tasks.whenTaskAdded {
if (name == "optimizeReleaseResources") {
doLast { apk.copyTo(optRes, true) } doLast { apk.copyTo(optRes, true) }
} }
} }