android - Gradle,复制映射文件的任务

标签 android gradle

必须有 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/

相关文章:

android - 如何从多项目目录的顶层禁用 Android Gradle 插件中的 lint abortOnError

android - 恢复默认操作栏布局

android - 从 fragment 中的 MainActivity 中从 JSON API 获取数据

intellij-idea - 在 Gradle 中设置 IntelliJ 编译器参数

java - 同时使用多个 Gradle 依赖版本

Android Studio - 导入外部库/Jar

android - 通过文件夹组织drawable-hdpi(mdpi、ldpi等)的内容

android - 我可以在 Android 的 Settings.NameValueTable 内容提供者中添加/检索/删除条目吗?

android - 为什么某些广播接收器只能通过 Code 或 AndroidManifest 注册

gradle - 如何关闭 Sonar 分析仪却仍然获得覆盖率报告?