Android, proguard

Proguard

ProGuard — shrinking, optimization, obfuscation, and preverification of Java bytecode.

Proguard optimizes the bytecode, removes unused codes, and obfuscates the classes, fields, methods with shorter names. Optimization operates with java bytecode. Since android runs on Dalvik bytecode which is converted from java bytecode, some optimizations usually doesn’t work well ( So you should be careful ).

The obfuscated code makes your APK difficult to reverse engineer, which is the main reason why most of the developers choose proguard.

Proguard can sometimes break your code up since it renames almost everything. Be sure to thoroughly test your app before release, especially after changing proguard config.

Prevent Obfuscation In Some Classes

Every app has some kind of data classes, models, or some important classes which they cannot be obfuscated. In such circumstances, we cannot let proguard to rename or remove any fields of those classes. It’s a safe bet to add a @Keep annotation on the whole class or methods, or a wildcard rule on proguard-rules.pro.

1 – @Keep Annotation:

Denotes that the annotated element should not be renamed when the code is minified at build time. This is typically used on methods and classes that are accessed only via reflection so a compiler may think that the code is unused.

@Keep
  public void foo() {
      ...
  }
 

Note that: This annotation is available only when using the Annotations Support Library.

2 – Using ProGuard, keep class fields with wildcard:

-keepclassmembers class com.my.package.** {
    public protected <fields>;
    private *** string*;
}

Tips & Tricks in Proguard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s