Search code examples
androidgradleproguardobfuscation

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


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 done in build.gradle? Since I have access there to my flavors and other stiff, I would like to create mapping file name based on flavor/build variant version.


Solution

  • As of today (May 2020) former solution, which uses variant.mappingFile is not working anymore in new Android Gradle plugin (Android Studio) 3.6 and higher.

    Instead variant.mappingFile returns null and following is displayed in the logs:

    WARNING: API 'variant.getMappingFile()' is obsolete and has been replaced with 'variant.getMappingFileProvider()'.

    I am sharing my working solution, which uses new api:

    
        applicationVariants.all { variant ->
            variant.assembleProvider.get().doLast {
                def mappingFiles = variant.getMappingFileProvider().get().files
    
                for (file in mappingFiles) {
                    if (file != null && file.exists()) {
                        def nameMatchingApkFile = "$archivesBaseName-$variant.baseName-$file.name"
                        def newMappingFile = new File(file.parent, nameMatchingApkFile)
    
                        newMappingFile.delete() //clean-up if exists already
                        file.renameTo(newMappingFile)
                    }
                }
            }
        }
    

    Note, that variant.getBuildType().isMinifyEnabled() is not used since we are using DexGuard.

    The code above makes mapping file's name match apk's file name.

    Just in case, if you need to change apk name - following could be used:

    android {
        defaultConfig {
            //resulting apk will looks like: "archive base name" + -<flavour>-<buildType>.apk
            archivesBaseName = "$applicationId-$versionName"
        }
    }