Override proguard options correctly

I play with ProGuard in my current project and decided to try the optimized Android configuration (with gradle):

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 

I did not find clear documentation on the optimization performed by the versions of proguard and android that are compatible with them:

 -optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* 

Are they relevant if the app min sdk version is 11?

So, I decided to redefine it to try in proguard-rules.pro :

 -optimizations ** -printconfiguration "result.pro" 

But the final configuration is not what I expected. It contains all the combined rules:

 -optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*,** 

So, how can you correctly override an option in ProGuard? Or maybe this line is equal to -optimizations ** ?

+6
source share
1 answer

It took me a bit of trial and error, but eventually found out. To override the default ProGuard optimization, for example, apply everything except code/simplification/arithmetic , then:

  • Add the -optimizations line to the ProGuard file and use * to represent everyone. For example, the following line:

     -optimizations !code/simplification/arithmetic,* 

    means "enable all optimizations except code/simplification/arithmetic ". The list of available optimizations is available on the official website , and you can use * to enable / disable optimization classes (for example !field/* ).

  • You must make sure that the ProGuard rules file is loaded before the default ProGuard file, replacing the order of proguard-rules.pro and getDefaultProguardFile('proguard-android.txt') in the Gradle file so that proguard-rules.pro first appears:

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

The result should look something like this:

 Optimizing... Number of finalized classes: 68 Number of unboxed enum classes: 1 Number of vertically merged classes: 0 Number of horizontally merged classes: 3 Number of removed write-only fields: 0 (disabled) Number of privatized fields: 58 Number of inlined constant fields: 375 Number of privatized methods: 13 Number of staticized methods: 37 Number of finalized methods: 210 Number of removed method parameters: 290 Number of inlined constant parameters: 236 Number of inlined constant return values: 239 Number of inlined short method calls: 35 Number of inlined unique method calls: 114 Number of inlined tail recursion calls: 0 Number of merged code blocks: 4 Number of variable peephole optimizations: 723 Number of arithmetic peephole optimizations: 10 Number of cast peephole optimizations: 0 Number of field peephole optimizations: 0 Number of branch peephole optimizations: 42 Number of string peephole optimizations: 35 Number of simplified instructions: 369 Number of removed instructions: 5019 Number of removed local variables: 154 Number of removed exception blocks: 0 Number of optimized local variable frames: 201 
+4
source

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


All Articles