必须有 mapping.txt
文件来检查来自应用程序的崩溃(由于 ProGuard
),在许多情况下,开发人员忘记复制映射文件并将其备份以及在下一个发布
之后,它将被更改并且无法检查以前版本的错误。
如何在发布后复制映射文件,并使用 gradle 任务自动将版本作为后缀复制到特定路径中?
最佳答案
这是我使用的 fragment 。它确实依赖于定义了 productFlavor
,但这只是为了帮助命名文件并允许在多个项目中重用相同的代码 fragment 而无需修改,但如果您想要不同的文件名,则可以重构该依赖项格式。
就目前而言,apk 和映射文件(如果需要)将以以下格式复制到定义的 basePath:
文件路径\应用程序名称\应用程序名称构建类型版本名称(版本代码)
例如 A:\Common\Apk\MyAppName\MyAppName 发行版 1.0 (1).apk 和 A:\Common\Apk\MyAppName\MyAppName 版本 1.0 (1).mapping
根据您的需要进行修改。
android {
productFlavors {
MyAppName {
}
}
//region [ Copy APK and Proguard mapping file to archive location ]
def basePath = "A:\\Common\\Apk\\"
applicationVariants.all { variant ->
variant.outputs.each { output ->
// Ensure the output folder exists
def outputPathName = basePath + variant.productFlavors[0].name
def outputFolder = new File(outputPathName)
if (!outputFolder.exists()) {
outputFolder.mkdirs()
}
// set the base filename
def newName = variant.productFlavors[0].name + " " + variant.buildType.name + " " + defaultConfig.versionName + " (" + defaultConfig.versionCode + ")"
// The location that the mapping file will be copied to
def mappingPath = outputPathName + "\\" + newName + ".mapping"
// delete any existing mapping file
if (file(mappingPath).exists()) {
delete mappingPath
}
// Copy the mapping file if Proguard is turned on for this build
if (variant.getBuildType().isMinifyEnabled()) {
variant.assemble.doLast {
copy {
from variant.mappingFile
into output.outputFile.parent
rename { String fileName ->
newName + ".mapping"
}
}
}
}
// Set the filename and path that the apk will be created at
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
def path = outputPathName + "\\" + newName + ".apk"
if (file(path).exists()) {
delete path
}
output.outputFile = new File(path)
}
}
}
//endregion
}
关于android - Gradle,复制映射文件的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38472995/