简体   繁体   中英

Gradle, task to copy mapping file

It's necessary to have mapping.txt file to check crashes come from your app (because of ProGuard ), in many cases developers forget to copy mapping file and back it up and after next release it will be changed and useless to check previous version bugs.

how to copy mapping file after release and copy version as suffix to it's name in particular path using gradle task automatically?

This is the snippet I use. It does depend on having a productFlavor defined but that is only to help name the file and allow the same snippet to be reused in multiple projects without modification but that dependency could be refactored out if you wanted a different filename format.

As it stands, the apk and the mapping file (if required) will be copied to the defined basePath in the format:

FilePath\\appname\\appname buildtype versionname (versioncode)

eg A:\\Common\\Apk\\MyAppName\\MyAppName release 1.0 (1).apk and A:\\Common\\Apk\\MyAppName\\MyAppName release 1.0 (1).mapping

Amend as you see fit.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM