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

后端 未结 12 1675
灰色年华
灰色年华 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条回答
  •  -上瘾入骨i
    2020-11-30 09:30

    All these answers used copy to rename the file. I didn't want to move the file however, I just wanted to change it's name, keeping in mind the build type and flavor.

    I based myself on the code from the other posters here and changed it up a bit. Since Minify can be false, while still using proguard, I just check if the file is present.

    Following code accomplishes just that.

    android {
        applicationVariants.all { variant ->
            variant.assemble.doLast {
                def mappingFolderUrl = "${project.buildDir.path}/outputs/mapping/"
    
                if (variant.buildType.name) {
                    mappingFolderUrl += variant.buildType.name + "/"
                }
    
                if (variant.flavorName) {
                    mappingFolderUrl += variant.flavorName + "/"
                }
    
                def mappingFileUrl = mappingFolderUrl + "mapping.txt"
                logger.lifecycle("mappingFile path: ${mappingFileUrl}")
    
                File mappingFile = file(mappingFileUrl)
                if (mappingFile.exists()) {
                    def newFileName = mappingFolderUrl + "mapping-${variant.name}.txt"
                    mappingFile.renameTo(newFileName)
                }
            }
        }
    }
    

    NOTE

    You could probably use this code to move the file as well.

    the .renameTo() method expects a full path, If you change the path, I would suppose you effectively move the File to another place.

提交回复
热议问题