How do I teach ProGuard to get rid of something that I keep that I donโ€™t use?

I have an Android project with the file proguard-rules.pro for the app module, which contains only the following:

 # ProGuard rules -dontobfuscate -dontwarn android.arch.util.paging.CountedDataSource -dontwarn android.arch.persistence.room.paging.LimitOffsetDataSource 

I do not hold anything myself. All -keep rules come from something else, be it the rules provided by getDefaultProguardFile('proguard-android-optimize.txt') , or from rules packaged in libraries.

However, the material is stored in what I do not use. If I use the Android Studio APK Analyzer on my release build while many things are removed by ProGuard, many other things are stored that I donโ€™t reference.

For example: through transitive dependencies, I have a support library module that contains a ViewPager in the application dependency tree. However, I am not currently using ViewPager in this application. Despite this, something forces him to save, since APK Analyzer shows 107 specific methods for android.support.v4.view.ViewPager , including its constructor.

I could use various ProGuard options to keep track of why this persists. However, this does not come from my rules. There is no -keep that needs to be fixed - -keep comes from someone else, presumably a Google engineer.

So how do I get rid of ViewPager ? Is there a way that I can override the -keep rule that causes it to be saved (e.g. using allowshrinking )? If so, how does ProGuard called by Android Studio determine whose -keep rule wins?

+5
source share
1 answer

The ViewPager class is not stored in a small application that I just checked, so your project should have different code or different rules.

You can start by having ProGuard print a chain that launches ViewPager:

 -whyareyoukeeping class android.support.v4.view.ViewPager 

You may need to repeat this several times for various classes and methods in order to get to the root cause. ProGuard does not print out which rule is responsible - admittedly, this will be a useful feature.

You can then search for the proguard.txt file in build/intermediates/exploded-aar , which contains the matching rule.

As for the solution at this point:

  • Unable to override -keep rules; they only accumulate.
  • As far as I know, the Android Gradle plugin also does not support disabling overly conservative proguard.txt files in libraries, so you will need to create your own .aar file with an updated rule or send an offer to the library developers.
+2
source

Source: https://habr.com/ru/post/1270930/


All Articles