Gradle,任务复制映射文件

Ali*_*aXX 2 android gradle

有必要让mapping.txt文件检查来自你的应用程序的崩溃(因为ProGuard),在许多情况下,开发人员忘记复制映射文件并备份它,然后release它将被更改并且无用于检查以前的版本错误.

如何在发布后复制映射文件并将版本作为后缀复制到特定路径中的名称,并自动使用gradle任务?

Kuf*_*ffs 5

这是我使用的片段.它确实取决于具有已productFlavor定义但仅用于帮助命名文件并允许相同的代码段在多个项目中重用而无需修改,但如果您需要不同的文件名格式,则可以重构该依赖项.

按照目前的情况,apk和映射文件(如果需要)将以以下格式复制到定义的basePath:

FilePath\appname\appname buildtype versionname(versioncode)

例如A:\ Common\Apk\MyAppName\MyAppName release 1.0(1).apk和A:\ Common\Apk\MyAppName\MyAppName release 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
Run Code Online (Sandbox Code Playgroud)

}