Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly override proguard options

I'm playing with ProGuard in my current project and decided to try optimized android config (with gradle):

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

I didn't find any clear documentation about optimizations performed by proguard and android versions which are compatible with them:

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

Are they up-to-date if app min sdk version is 11?

So i decided to override it to give it a try in proguard-rules.pro:

-optimizations **
-printconfiguration "result.pro"

But final configuration isn't as i expected. It contains all rules combined:

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

So how can option be correctly overridden in ProGuard? Or may be this line equals to -optimizations ** ?

like image 480
tiktak Avatar asked Apr 15 '15 07:04

tiktak


People also ask

What is ProGuard configuration?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.

How do you set ProGuard rules?

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.

What is R8 ProGuard?

R8 is another tool that will convert your java byte code into an optimized format of dex code. It will check the whole application and will remove the unused classes and methods. It helps us to reduce the size of our APK and to make our app more secure. R8 uses similar proguard rules to modify its default behavior.


1 Answers

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

  1. Add a -optimizations line to your ProGuard file and use * to represent all. For example, the following line:

    -optimizations !code/simplification/arithmetic,*
    

    means "enable all optimizations except code/simplification/arithmetic". The list of available optimizations is available in the official website and you can use * to enable/disable classes of optimizations (e.g. !field/*).

  2. You have to make sure that your ProGuard rules file is loaded before the default ProGuard file by swapping the order of proguard-rules.pro and getDefaultProguardFile('proguard-android.txt') in the Gradle file so that proguard-rules.pro appears first:

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

The output should look something like this now:

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
like image 99
Mike Laren Avatar answered Oct 22 '22 08:10

Mike Laren