Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Proguard using Gradle?

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!

like image 760
max.mustermann Avatar asked Feb 10 '14 08:02

max.mustermann


1 Answers

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.

like image 149
Scott Barta Avatar answered Sep 17 '22 19:09

Scott Barta