ProGuard and Android

ProGuard is distributed as part of the Android SDK and runs when building the application in release mode.

ProGuard makes your APK file as small as possible, you should enable shrinking to remove unused code and resources in your release build.

Shrinking detects and removes unused classes, fields, methods, and attributes from your packaged app. ProGuard also optimizes the bytecode and obfuscates the remaining classes, fields, and methods with short names. The obfuscated code makes your APK difficult to reverse engineer.

Code shrinking

Following snippet from a build.gradle file in Android Studio enables code shrinking for the release build.

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

ProGuard outputs

With each build, ProGuard outputs the following files:

  • dump.txt - describes the internal structure of all the class files in the APK.
  • mapping.txt - provides a translation between the original and obfuscated class, method, and field names.
  • seeds.txt - lists the classes and members that were not obfuscated.
  • usage.txt - lists the code that was removed from the APK.

Android proguard-rules.pro file

The proguard-rules.pro file is where you can add custom ProGuard rules in Android Studio. Example of the rules for proguard-rules.pro file.

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.view.View {
      public (android.content.Context);
      public (android.content.Context, android.util.AttributeSet);
      public (android.content.Context, android.util.AttributeSet, int);
      public void set*(...);
}

-keepclasseswithmembers class * {
    public (android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public (android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
    public void *(android.view.View);
    public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static ;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface ;
}

Static Gson ProGuard rules

Popular ProGuard rules