Android 如何使用 Gradle 配置 Proguard?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21672409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:54:46  来源:igfitidea点击:

How to configure Proguard using Gradle?

androidgradleandroid-studioproguardandroid-gradle-plugin

提问by max.mustermann

I recently switched to Android Studio / Gradle and I am wondering, how ProGuard can be configured in the build.gradle script. I am new to Gradle, but I thought, configuring the Proguard task would be a good idea (as documented in the Proguard project documentation.

我最近切换到 Android Studio/Gradle,我想知道如何在 build.gradle 脚本中配置 ProGuard。我是 Gradle 的新手,但我认为,配置 Proguard 任务将是一个好主意(如 Proguard 项目文档中所述。

I want to configure Proguard to save the mapping in different files for different product flavors with the 'printmapping' setting

我想配置 Proguard 以使用“printmapping”设置将映射保存在不同的文件中以用于不同的产品风格

task myProguardTask(type: proguard.gradle.ProGuardTask) {
     printmapping file("test.txt")
}

but it crashes on task-execution with

但它在任务执行时崩溃

Gradle: Execution failed for task ':module:proguardFlavorVariant'.
    > proguard.ConfigurationParser.<init>(Ljava/io/File;Ljava/util/Properties;)V

In the newer versions of the Gradle 'android'-plugin, Proguard seems to be included and I think this might be the reason, why configuring the Proguard task as stated on the Proguard documentation did not work. But I did not find any documentation on this topic of how to do this with the newer android-gradle-plugin.

在较新版本的 Gradle 'android'-plugin 中,似乎包含了 Proguard,我认为这可能是原因,为什么配置 Proguard 文档中所述的 Proguard 任务不起作用。但是我没有找到有关如何使用较新的 android-gradle-plugin 执行此主题的任何文档。

Thanks for your help!

谢谢你的帮助!

回答by Scott Barta

Proguard is built into the Android-Gradle plugin and you don't need to configure it as a separate task. The docs are at:

Proguard 内置于 Android-Gradle 插件中,您无需将其配置为单独的任务。文档位于:

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

Are your flavors so different that you really want different ProGuard configurations for them? I'd think in most cases you could have one config that could cover them all.

您的口味是否如此不同以至于您真的想要为它们提供不同的 ProGuard 配置?我认为在大多数情况下,您可以拥有一个可以涵盖所有内容的配置。

EDIT:

编辑:

If you do want to change ProGuard rules for different flavors, the Android Gradle DSL allows you to do so. The sample in the docs shows how to do it:

如果您确实想更改不同风格的 ProGuard 规则,Android Gradle DSL 允许您这样做。文档中的示例显示了如何执行此操作:

android {
    buildTypes {
        release {
            // in later versions of the Gradle plugin runProguard -> minifyEnabled
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }

    productFlavors {
        flavor1 {
        }
        flavor2 {
            proguardFile 'some-other-rules.txt'
        }
    }
}

That should handle your use case, unless you're looking for a way to have it automatically determine the proguardFile value based on the flavor name without you having to set it manually; you could do that through some custom Groovy scripting.

这应该可以处理您的用例,除非您正在寻找一种方法让它根据风味名称自动确定 proguardFile 值,而无需手动设置它;你可以通过一些自定义的 Groovy 脚本来做到这一点。