如何使用 Gradle 配置 Proguard? [英] How to configure Proguard using Gradle?

查看:52
本文介绍了如何使用 Gradle 配置 Proguard?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

但它在任务执行时崩溃

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

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

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.

感谢您的帮助!

推荐答案

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

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

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

您的口味是否如此不同以至于您真的想要为它们提供不同的 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 规则,Android Gradle DSL 允许您这样做.文档中的示例显示了如何操作:

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:

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

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

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.

这篇关于如何使用 Gradle 配置 Proguard?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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