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

后端 未结 12 1702
灰色年华
灰色年华 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: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.

提交回复
热议问题