I have an Android project with a proguard-rules.pro
file for the app
module that contains only the following:
# ProGuard rules
-dontobfuscate
-dontwarn android.arch.util.paging.CountedDataSource
-dontwarn android.arch.persistence.room.paging.LimitOffsetDataSource
I am not keeping anything myself. All -keep
rules are coming from something else, whether that is the rules provided by getDefaultProguardFile('proguard-android-optimize.txt')
or from rules packaged in libraries.
However, stuff is being kept that I am not using. If I use the Android Studio APK Analyzer on my release
build, while lots of things are removed by ProGuard, lots of other things are kept that I am not referencing.
For example: through transitive dependencies, I have the Support Library module that contains ViewPager
in the app's dependencies tree. However, I am not (presently) using ViewPager
in this app. Despite this, something is causing it to be kept, as the APK Analyzer shows 107 defined methods for android.support.v4.view.ViewPager
, including its constructor.
I could use various ProGuard options to track down why this is being kept. However, it is not coming from my rules. There is no -keep
of my own that needs fixing — the -keep
is coming from somebody 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 is causing it to be kept (e.g., using allowshrinking
)? If so, how does ProGuard, as invoked by Android Studio, determine whose -keep
rule wins?
Shrinking Options Specifies not to shrink the input. By default, ProGuard shrinks the code: it removes all unused classes and class members. It only keeps the ones listed by the various -keep options, and the ones on which they depend, directly or indirectly.
-keep disables all of ProGuard's goodness. No shrinking, no obfuscation; not for classes, not for members. In real use cases, you can let ProGuard do at least some of it's work. Even if your variables are accessed by reflection, you could remove and rename unused classes, for example.
When you create a new project or module using Android Studio, the IDE creates a <module-dir>/proguard-rules.pro file for you to include your own rules. You can also include additional rules from other files by adding them to the proguardFiles property in your module's build.gradle file.
The ViewPager class isn't kept in a small app that I just checked, so it must be other code or other rules in your project indeed.
You can start with letting ProGuard print out the chain that triggers ViewPager to be kept:
-whyareyoukeeping class android.support.v4.view.ViewPager
You may need to repeat this a number of times for various classes and methods to get to the root cause. ProGuard doesn't print out which rule exactly is responsible -- admittedly, this would be a useful feature.
You can then look for the proguard.txt
file in build/intermediates/exploded-aar
that contains a matching rule.
As for a solution at that point:
-keep
rules; they only accumulate.proguard.txt
files in libraries, so you'd need to create a custom .aar file with the updated rule, or send a suggestion to the developers of the library.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With