Android Studio ConsumerProguardFiles for library not working

I would like to use proguard in my library, but the file (rules) must be installed inside the library. This means that I do not want to explicitly set the w rules (which belong to the library) in my application module.

I found that there is something like the consumerProguardFiles property. My settings:

gradle library:

buildTypes { debug { debuggable true minifyEnabled false } release { minifyEnabled true consumerProguardFiles 'proguard-rules.pro' } } 

gradle application:

 buildTypes { debug { applicationIdSuffix ".debug" debuggable true minifyEnabled false signingConfig signingConfigs.debug } release { debuggable false minifyEnabled true signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } 

Why doesn't the configuration above work? I get errors that packages / characters from the library cannot be found.


Edited by:

It is important that my proguard-rules.pro for the library and proguard-rules.pro for the main application module are empty.

Examples of errors:

 Error:(3, 60) error: package (my library package here) does not exist (...) Error:(16, 9) error: cannot find symbol class NavigationStructureModel (...) 

More than one manual mistake. Since I see that my classes from the library are missing.

+1
android android-studio proguard android-library android-proguard
source share
2 answers

You need to find out if you want to

  • Run proguard in the library
  • Launch proguard in the app (Hint: this is it.)

Run proguard in the library

This is DO , it reaches the application. The rules of your proguard library are responsible for ensuring that all available and accessible codes are available, but you can confuse inner classes.

Here is the build.gradle library:

 android { defaultConfig { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } buildTypes { release { minifyEnabled true // Yes, run proguard on the library itself. } } } 

If you release this library as open, you will not need it. If you do not publish the library to the public, you do not need it.

I get errors that packages / characters from the library cannot be found.

Proguard builds a dependency graph between classes. The library is currently a standalone unit, so if you haven't specified any rules, none of the classes are ever used, so proguard deletes them all.

Run proguard in the application

Let the consumer (application) know which library classes should be stored.

Here is the build.gradle library:

 android { defaultConfig { // Here what proguard on the app should do on the library behalf. consumerProguardFiles 'proguard-consumer-rules.pro' } buildTypes { release { minifyEnabled false // No, don't proguard the library itself. } } } 

How proguard works

You need to tell proguard which code to keep and which names to keep. Proguard removes all unused code names and scrambling that you did not want to stick with.

You do not tell proguard what to remove, you tell proguard what to save.

+4
source share

Well, since proguard can eat your classes, try something like this in your proguard-rules.pro app file:

 keepclass my.library.package.name.** { *;} 

which proguard has to say to save all the classes from my.library.package.name , so hopefully they will not be lost anymore.

0
source share

All Articles