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

后端 未结 12 1643
灰色年华
灰色年华 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:10
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (variant.getBuildType().isMinifyEnabled()) {
                variant.assemble.doLast{
                    copy {
                        from variant.mappingFile
                        into "${rootDir}/mapping"
                        rename { String fileName ->
                            "mapping-${variant.name}-${new Date().format('yyyy_MM_dd')}.txt"
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 09:12

    variant.assemble is now deprecated, suggested solution incorporating previous modifications:

    archivesBaseName = "MyCompany-MyAppName-$versionName"
    ...
    applicationVariants.all { variant ->
        variant.assembleProvider.get().doLast {
            if (variant.mappingFile != null && variant.mappingFile.exists()) {
                def mappingFilename = "$archivesBaseName-$variant.baseName-mapping.txt"
            (new File(variant.mappingFile.parent, mappingFilename)).delete()
            variant.mappingFile.renameTo(variant.mappingFile.parent +
                    "/" + mappingFilename)
            }
        }
    }
    

    If using app bundle (aab) instead of apk, add this to after the android section:

    afterEvaluate {
        bundleRelease.doLast {
            android.applicationVariants.all { variant ->
                if (variant.buildType.name == 'release') {
                    tasks.create(name: "renameMappingFile") {
                        if (variant.mappingFile != null && variant.mappingFile.exists()) {
                            variant.mappingFile.renameTo(variant.mappingFile.parent + "/$variant.baseName-$versionName-${new Date().format('yyyy-MM-dd_HHmm')}-mapping.txt")
                        }
                    }
                }
            }
        }
    }
    

    Swap bundleRelease for assembleRelease for apks in the last example too.

    Update: However that last doesn't work if you try and build a normal debug directly to your phone then. Error:

    Could not get unknown property 'bundleRelease' for project ':app' of type org.gradle.api.Project.

    0 讨论(0)
  • 2020-11-30 09:14

    Use this command in your proguard-rules.pro file:

    -printmapping path/to/your/file/file_name.txt
    

    the file will be written in part {root}/path/to/your/file with file_name.txt name.

    If you want to have different setting for different flavors you can define many proguard-rules for them

    I found one more idea but I am not sure that it is right way.

    You can define your path in flavors:

    productFlavors {
        test1 {
            applicationId "com.android.application.test"
            project.ext."${name}Path" = 'path/one/mapp.txt'
        }
        test2 {
            project.ext."${name}Path" = 'path/two/mapp.txt'
        }
    }
    

    And as next you can define new task before $asseble{variant.name.capitalize()} task as is shown below:

    android.applicationVariants.all { variant ->
        def envFlavor = variant.productFlavors.get(0).name
    
        def modifyProguardPath = tasks.create(name: "modifyProguardFor${variant.name.capitalize()}", type: Exec) {
            def pathToMap = project."${envFlavor}Test1"
            doFirst {
                println "==== Edit start: $pathToMap ===="
            }
            doLast {
                println "==== Edit end: $pathToMap ===="
            }
            executable "${rootDir}/utils/test.bash"
            args pathToMap
        }
    
        project.tasks["assemble${variant.name.capitalize()}"].dependsOn(modifyProguardPath);
    }
    

    and in script ${root}/utils/test.bash - you can modify proguard-rules.pro.

    But I think that exist better solution.

    0 讨论(0)
  • 2020-11-30 09:17

    New for Android Studio Gradle Plugin Version 3.3.0 (January 2019) This overcomes previous issues where Android 3.0/Android Gradle Plugin 3.0 deprecated BuildType.isMinifyEnabled() and the gradle plugin deprecated variant.getAssemble().

    applicationVariants.all { variant ->
        variant.assembleProvider.get().doLast {
            if (variant.mappingFile != null && variant.mappingFile.exists()) {
                def mappingFilename = "$archivesBaseName-$variant.baseName-mapping.txt"
                (new File(variant.mappingFile.parent, mappingFilename)).delete()
                variant.mappingFile.renameTo(variant.mappingFile.parent +
                        "/" + mappingFilename)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 09:18

    This is a variation of igorpst's answer but renames mapping.txt to match the apk's name exactly including the app version name. I've combined this with code to name the APK with a version number as described in this answer. I had to snoop through the gradle source code to find $variant.baseName

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"
        defaultConfig {
            applicationId "com.company.app"
            minSdkVersion 13
            targetSdkVersion 21
            versionCode 14       // increment with every release
            versionName '1.4.8'   // change with every release
            archivesBaseName = "MyCompany-MyAppName-$versionName"
        }
        applicationVariants.all { variant ->
            if (variant.getBuildType().isMinifyEnabled()) {
                variant.assemble.doLast {
                    (new File(variant.mappingFile.parent, "$archivesBaseName-$variant.baseName-mapping.txt")).delete();
                    variant.mappingFile.renameTo(variant.mappingFile.parent +
                            "/$archivesBaseName-$variant.baseName-mapping.txt")
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 09:18

    A complete solution that worked for me

    applicationVariants.all { variant ->
            def variantType = variant.buildType.name
            if (variantType == "release") {
                variant.assemble.doLast {
                    def mappingFile = variant.mappingFile
                    mappingFile.renameTo(mappingFile.parent + "/mapping-${variant.name}.txt")       
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题