def cfg = rootProject.ext
def bakPath = file("${buildDir.parent}/tinkerBackup/")
def bakResguard = file("${bakPath}")
def debugBakPath = file("${buildDir.absolutePath}/tinkerBackup")
ext {
appName = "nicepro"
...
省略tinker配置
}
def isNeedBackup = true
task AtinkerPatchPrepare << {
isNeedBackup = false
def backUpVersion = ""
if (new File("${bakResguard}/version.txt").exists()) {
backUpVersion = new File("${bakResguard}/version.txt").getText()
}
if (!backUpVersion.isEmpty()) {
print "app version : + ${backUpVersion} \n"
project.tinkerPatch.oldApk = "${bakResguard}/${backUpVersion}.apk"
project.tinkerPatch.buildConfig.applyResourceMapping = "${bakResguard}/${backUpVersion}_R.txt"
project.andResGuard.mappingFile = file("${bakResguard}/resource_mapping_${backUpVersion}.txt")
}
}
* bak apk and mapping
*/
android.applicationVariants.all { variant ->
* task type, you want to bak
*/
def taskName = 'release'
if (taskName.equals("debug")) {
bakResguard = file("${buildDir.absolutePath}/tinkerBackup")
}
tasks.all {
if (variant.buildType.name == taskName) {
def backUpVersion = ""
if (new File("${bakResguard}/version.txt").exists()) {
backUpVersion = new File("${bakResguard}/version.txt").getText()
}
if ("tinkerPatch${taskName.capitalize()}".equalsIgnoreCase(it.name)) {
def resguardTask
tasks.all {
if (it.name.equalsIgnoreCase("resguard${taskName.capitalize()}")) {
resguardTask = it
}
}
it.doFirst({
it.buildApkPath = "${buildDir}/outputs/apk/AndResGuard_${appName}_${cfg.versionName}_${releaseTime()}/${appName}_${cfg.versionName}_${releaseTime()}_signed_7zip_aligned.apk"
project.android.ext.tinkerOldApkPath = "${bakResguard}/${backUpVersion}.apk"
project.android.ext.tinkerApplyResourcePath = "${bakResguard}/${backUpVersion}_R.txt"
})
it.dependsOn AtinkerPatchPrepare
it.dependsOn resguardTask
}
if ("resguard${taskName.capitalize()}".equalsIgnoreCase(it.name)) {
if (!backUpVersion.isEmpty() && new File("${backUpVersion}_mapping.txt").exists()) {
ext.andResMappingFile = new File("${backUpVersion}_mapping.txt")
} else {
ext.andResMappingFile = null
}
it.doLast {
if (!isNeedBackup) {
return 0
}
def date = new Date().format("MMdd-HH-mm-ss")
def orgAndresPrefix = "AndResGuard_${appName}_${cfg.versionName}_${releaseTime()}"
def orgApkPrefix = "${appName}_${cfg.versionName}_${releaseTime()}"
def targetApkPrefix = "${appName}_${taskName.capitalize()}_${cfg.versionName}_${date}"
File version = new File("${bakResguard}/version.txt")
if (!version.parentFile.exists()) {
version.parentFile.mkdir()
}
version.write("${targetApkPrefix}")
copy {
from "${buildDir}/outputs/apk/${orgAndresPrefix}/${orgApkPrefix}_signed_7zip_aligned.apk"
into file(bakResguard.absolutePath)
rename { String fileName ->
try {
fileName.replace("${orgApkPrefix}_signed_7zip_aligned.apk", "${targetApkPrefix}.apk")
} catch (Exception e) {
print "rename apk mapping error"
e.printStackTrace()
}
}
from "${buildDir}/outputs/mapping/${taskName}/mapping.txt"
into file(bakResguard.absolutePath)
rename { String fileName ->
fileName.replace("mapping.txt", "${targetApkPrefix}_mapping.txt")
}
from "${buildDir}/intermediates/symbols/${taskName}/R.txt"
into file(bakResguard.absolutePath)
rename { String fileName ->
fileName.replace("R.txt", "${targetApkPrefix}_R.txt")
}
from "${buildDir}/outputs/apk/${orgAndresPrefix}/resource_mapping_${orgApkPrefix}.txt"
into file(bakResguard.absolutePath)
rename { String fileName ->
try {
fileName.replace("resource_mapping_${orgApkPrefix}.txt", "resource_mapping_${targetApkPrefix}.txt")
} catch (Exception e) {
print "rename resource mapping error"
e.printStackTrace()
}
}
print "one resguard backup tinker base apk ok! \n"
}
packageChannel("${buildDir}/outputs/apk/${orgAndresPrefix}/${orgApkPrefix}_signed_7zip_aligned.apk")
}
}
}
}
}