Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • ProGuard – Obfuscating, Shrinking & Optimizing Android Tool

ProGuard – Obfuscating, Shrinking & Optimizing Android Tool

 July 14  | 0 Comments

When you are developing an Android app, some of the most common questions that you come across are:

  1. How to safeguard an Android app?
  2. How to minimize the final Android device executable apk file size?
  3. How to make the app run faster?

The answer to the above questions is a tool called ‘ProGuard’. This tool is used worldwide as shrinker, optimizer, and obfuscator, which is usually neglected while releasing an app in the market. This blog post focuses on digging deeper about the usage of this tool while developing an android app.

So, what do these terms (Shrinkage, Optimization & Obfuscation) means?

  1. Obfuscation – It a process of making an app code difficult to read & the app code becomes secure so that others will not be able to reverse engineer it to get the source code back from .apk
  2. Shrinkage – It is process of making your app executable .apk file size to almost half of its original size.
  3. Optimization – It is process which enhances the code to make the app run faster

Let’s deep dive into each technique.

Obfuscation

When we compile bytecodes, all the debugging information like classes, fields, methods, and attributes etc. are included. This makes it very easy to decompile and reverse engineer the code. To stop this from happening, ProGuard uses the technique called Obfuscator. Here, all the debugging information will be replaced by special characters making it very difficult to read and understand and at the same time the internal functionality remains the same.

Steps to Enable ProGuard in Your App

The following are the step to enable ProGuard in your app:

Step 1: In Android Studio, open up an Android project and change it to Project View.

Step 2: Edit the app’s  [Android project]/app/build.gradle file by changing the value of minifyEnable to ‘true’.

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

Step 3: You can set rules to ProGuard which are optional. To do that, go to Project View, select the [Android project]/app/proguard-rules.pro file. Add in the following lines of code  to tell ProGuard not to obfuscate certain classes.

-keepclassmembers class com.acadgild.xxxx {
   public *;   }

Shrinkage

The unused resources and libraries can be removed automatically at the built-time when packaging the .apk file. To automate this process, the resource shrinking should be enabled. This can be done as shown below:

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

Note : To use shrinkResources property, minifynabled should be set to ‘true’.

Optimization

The Optimization process is turned off by default. Dex does not like the code to be run through the ProGuard’s optimization and pre-verify steps as it performs some of these optimizations on its own.

#-dontoptimize
#-dontpreverify

If you want to enable optimization, go to Project View, select the [Android project]/app/proguard-rules.pro file and include the following:

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

Note: These flags cannot be included in your own configuration file. If included, the optimization will be turned off. You’ll need to either edit this file or duplicate the content and remove the include of this file from your project’s ProGuard.config path property.

The above information is for beginners who are new to ProGuard. You can refer to the official ProGuard documentation for more information.

Keep visiting our Acadgild for more updates on Android and other technologies. Click here to learn Android App Development.

 

>