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

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

问题描述

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



我想配置Proguard以保存使用'printmapping'设置在不同产品口味的不同文件中进行映射

$ $ $ $ $ $ $ $ $ $ $ $ $ $> $ myProguardTask(类型:proguard.gradle.ProGuardTask) {
printmapping file(test.txt)
}

但它使用

  Gradle:执行任务执行失败':module:proguardFlavorVariant'。
> proguard .ConfigurationParser。< init>(Ljava / io / File; Ljava / util / Properties;)V

在更新版本的Gradle'android'-plugin中,Proguard似乎被包含在内,我认为这可能是为什么配置Proguard任务的说明在Proguard文档中无法正常工作的原因,但是我没有找到任何文档关于这个话题该怎么办这与新的Android的gradle插件。



感谢您的帮助!

解决方案

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


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

您的口味如此不同以至于您真的需要不同的ProGuard配置吗?我认为在大多数情况下,你可以有一个配置,可以覆盖所有。



编辑:



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

  android {
buildTypes {
release {
//在更高版本的Gradle插件中runProguard - > minifyEnabled
minifyEnabled true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}

productFlavors {
flavor1 {

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

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


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.

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

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.

Thanks for your help!

解决方案

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

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.

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:

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.

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

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