ConsumerProguardFiles vs ProguardFiles

Peddro picture Peddro · Jan 10, 2017 · Viewed 10.9k times · Source

I was trying to build an application which contained a library module La with proguard and I noticed that the library was not being obfuscated. Was trying to understand why. At this moment in time this was my buildType:

release {
 minifyEnabled false
 useProguard true
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} 

After some search I've come across the ConsumerProguardFiles function which states:

ProGuard rule files to be included in the published AAR.

These proguard rule files will then be used by any application project that consumes the AAR (if ProGuard is enabled).

This allows AAR to specify shrinking or obfuscation exclude rules.

This is only valid for Library project. This is ignored in Application project.

With this now the buildType of my library La is the following:

release {
 minifyEnabled false
 useProguard true
 consumerProguardFiles 'proguard-rules.pro'
}

And now my library La is using it's proguard rules and the library code is obfuscated.

So my questions are:

1) What is the reason for this different behaviour. Why doesn't ProguardFiles obfuscate library proguarded rules but instead ignores it?

2) I'm guessing that what the ConsumerProguardFiles does is to merge the rules of the library with the main application proguard rules. Is this assumption correct?

3) In short: use proguardFiles for applications and ConsumerProguardFiles for libraries. Correct?

Thank you for your time reading!

Answer

Fabian Streitel picture Fabian Streitel · Apr 23, 2017

As far as I understand, minifyEnabled false means that ProGuard is not run to minify/obfuscate your library module. Which is what you want because the library module can't know which parts of it are going to be used in your application module.

Instead, the library module must provide its relevant proguard rules to the app module (via consumerProguardFiles) and then the app module will run ProGuard with both its own rules and the library's rules together to minimize the final application APK.