How to change the proguard mapping file name in gradle for Android project

后端 未结 12 1700
灰色年华
灰色年华 2020-11-30 08:48

I have android project based on gradle and I want to change mapping.txt file name after it\'s generated for my build. How can it be done?

upd

How it can be d

12条回答
  •  被撕碎了的回忆
    2020-11-30 09:23

    Pinhassi's solution above works great and it is conforms to the latest Gradle changes. There are a couple of things though that I had to change:

    1. The module name is hardcoded ("app"), which is not ideal since in a lot of cases (including mine) that will not be true. It is better to dynamically detect the module name.
    2. The mapping file also only conforms to the Windows file system by having backward escaped slashes ("\"). If you are on a *NIX system like Linux or Mac, you need to replace those with forward non escaped slashes ("/")
    3. Changed a bit the renaming of the .apk file to include the project name and added a date/time stamp at the end.

    Here is the finished code:

    import java.util.regex.Matcher
    import java.util.regex.Pattern
    
    buildTypes {
            release {
            debuggable false
            minifyEnabled true
            proguardFiles 'proguard.cfg'
    
            // Rename the apk file and copy the ProGuard mapping file to the root of the project
            applicationVariants.all { variant ->
                if (variant.getBuildType().name.equals("release")) {
                    def formattedDate = new Date().format('yyyyMMddHHmmss')
                    def projectName = ""
                    variant.outputs.each { output ->
                        def fullName = output.outputFile.name
                        projectName = fullName.substring(0, fullName.indexOf('-'))
                        // ${variant.name} has the value of "paidRelease"
                        output.outputFile = new File((String) output.outputFile.parent, (String) output.outputFile.name.replace(".apk", "-v${variant.versionName}-${formattedDate}.apk"))
                    }
                    def mappingFile = "${rootDir}/${projectName}/build/outputs/mapping/${getCurrentFlavor()}/release/mapping.txt"
                    println("mappingFile:  ${mappingFile}")
                    if (variant.getBuildType().isMinifyEnabled()) {
                        variant.assemble.doLast {
                            copy {
                                from "${mappingFile}"
                                into "${rootDir}"
                                rename { String fileName ->
                                    "mapping-${variant.name}.txt"
                                }
                            }
                        }
                    }
                }
            }
        }
    
            debug {
                debuggable true
            }
        }
    
    def getCurrentFlavor() {
        Gradle gradle = getGradle()
        String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
        Pattern pattern;
    
        if( tskReqStr.contains( "assemble" ) )
            pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
        else
            pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    
        Matcher matcher = pattern.matcher( tskReqStr )
    
        if( matcher.find() )
            return matcher.group(1).toLowerCase()
        else {
            println "NO MATCH FOUND"
            return "";
        }
    }
    

提交回复
热议问题