如何在Android项目的gradle中更改proguard映射文件名 [英] How to change the proguard mapping file name in gradle for Android project

查看:58
本文介绍了如何在Android项目的gradle中更改proguard映射文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 gradle 的 android 项目,我想在为我的构建生成后更改 mapping.txt 文件名.怎么办?

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?

更新

如何在 build.gradle 中完成?由于我可以访问我的口味和其他僵硬的东西,我想根据口味/构建变体版本创建映射文件名.

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.

推荐答案

截至今天(2020 年 5 月) 以前使用 variant.mappingFile 的解决方案不再有效在新的 Android Gradle 插件 (Android Studio) 3.6 及更高版本中.

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.

取而代之的是 variant.mappingFile 返回 null 并在日志中显示以下内容:

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

警告:API 'variant.getMappingFile()' 已过时并已替换为 'variant.getMappingFileProvider()'.

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

我正在分享我的工作解决方案,它使用了新的 api:

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)
                }
            }
        }
    }

请注意,variant.getBuildType().isMinifyEnabled() 未使用,因为我们使用的是 DexGuard.

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

上面的代码使映射文件的名称与apk的文件名匹配.

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

以防万一,如果您需要更改 apk 名称 - 可以使用以下内容:

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"
    }
}

这篇关于如何在Android项目的gradle中更改proguard映射文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆