Proguard the App - xialin/GrabMovie GitHub Wiki

The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer.

Enabling Proguard

It is easy to enable proguard in a Gradle project. The property to enable/disable proguard is minifyEnabled in build.gradle:

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

The getDefaultProguardFile('proguard-android.txt') method obtains the default ProGuard settings from the Android SDK tools/proguard/ folder. The proguard-android-optimize.txt file is also available in this Android SDK folder with the same rules but with optimizations enabled. Android Studio adds the proguard-rules.pro file at the root of the module, so you can also easily add custom ProGuard rules specific to the current module.

Configuring Custom ProGuard Rules

The default ProGuard configuration file tries to cover general cases, but you might encounter exceptions such as ClassNotFoundException, which happens when ProGuard strips away an entire class that your application calls.

You can fix errors when ProGuard strips away your code by adding a -keep line in the ProGuard configuration file. For example:

-keep public class <MyClass>
Tips:

If you are using third party dependencies, they usually have pre-defined ProGuard rules for you. Remember to copy the rules to your proguard-rules.pro when you add the dependency.

Example:

In this project, custom ProGuard rules are defined in app/proguard-rules.pro

# ProGuard for OkHttp
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

References

http://developer.android.com/tools/help/proguard.html

⚠️ **GitHub.com Fallback** ⚠️