12

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!

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Habizzle
  • 2,971
  • 3
  • 20
  • 23
  • make sure your proguard configuration file is written properly, because it is showing the Parsing error which usually comes when there is something wrong in your proguard file. – Piyush Agarwal Feb 10 '14 at 09:16
  • Thanks for your advice, but my proguard-file should be ok, because it works if I perform the gradle build (including proguard-task) without the proguard-customization within the gradle script. – Habizzle Feb 10 '14 at 09:20
  • 1
    why dont you use `runProguard true` `proguardFile getDefaultProguardFile('proguard-android.txt')` in the flavor configurations for different flavors. – Piyush Agarwal Feb 10 '14 at 09:35
  • This is what I am using right now ;-) but I was just wondering if there was a possibility to configure it all inside gradle wihtout having to create x more files to maintain. – Habizzle Feb 10 '14 at 09:39
  • ok can you include you whole build.gradle file because i think you have to use some repository or dependency classpath in order to include the proguard task, not sure. – Piyush Agarwal Feb 10 '14 at 09:58

1 Answers1

20

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.

Wextux
  • 752
  • 9
  • 18
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Yeah, I don't NEED to configure it in a seperate task - but CAN I do it? ;-). I want to use different proguard configurations to name a mapping.txt according to its flavor name so I won't miss, which mapping belongs to a specific build. I admit, it's nothing big or some kind of a stopper, but I was just wondering if it is possible. – Habizzle Feb 11 '14 at 08:37
  • You don't need a separate ProGuard task to have per-flavor mapping files; I updated my answer. – Scott Barta Feb 11 '14 at 16:51
  • That what I ended up implementing now. Configuring proguard (despite just linking to external files which configure proguard) seems to be not possible right now, at least the documentation says nothing about it. – Habizzle Feb 12 '14 at 12:35
  • 5
    runProguard changed to minifyEnabled, [see here](http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard). – Elhanan Mishraky Jan 18 '15 at 16:42
  • In addition to 'proguard-android.txt', the Android SDK also provides you with a file 'proguard-android-optimize.txt' which already has some rules set to optimize your code at the byte level and will possibly help make your app size smaller. See: http://developer.android.com/tools/help/proguard.html#enabling-gradle – Christian.D Jan 15 '16 at 08:56
  • Hi Scott, could you please take a look at my question [here](http://stackoverflow.com/q/37314760/3287204)? Thank you ... – Yash Sampat May 23 '16 at 12:47