Gradle,任务复制映射文件

时间:2016-07-20 05:31:53

标签: android gradle

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

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

1 个答案:

答案 0 :(得分:5)

这是我使用的代码段。它确实依赖于定义了productFlavor,但这只是为了帮助命名文件并允许在没有修改的情况下在多个项目中重用相同的代码片段,但如果您想要不同的文件名格式,则可以重构该依赖项。 / p>

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

FilePath \ appname \ appname buildtype versionname(versioncode)

e.g 答:\ Common \ Apk \ MyAppName \ MyAppName版本1.0(1).apk 和 答:\ 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

}