Applying Proguard In An Android Application

ProGuard tool is used in an Android application for three main reasons such as to reduce the code, to confuse or obfuscate the code, and to optimize the code.

ProGuard thus does add three pros to an Android app.

• Reduce the apk size, since the code is minified.
• Remove the classes and methods that were found unused.
• Protects the application from getting Reverse Engineered by confusing the code.

 

minifyEnabled

 

There is a property called minifyEnabled, which grant the application with 64K method counts limit.

To enable ProGuard, make minifyEnabled to true in the buildTypes in your build.gradle file.

Since ProGuard minifies your code it would slow down the build time as a result. So it is advised to avoid using ProGuard on the debug part of buildTypes. There is no use of applying compression to the part of code that will be considered for just debug process. It is very important that you perform minify of code on your final build APK.

Look down the code snippet of application level build.gradle file

 

This minifies your code and protects from being Reverse Engineered.

For example, simply consider like this,

public void getFevik(int nParam) { ... }

will be minified down to

public void a(int b) {...}

 

Always include Proguard rules in the proguard-rules.pro file for any library that you integrate in your project.
If you are using Okio library, you will have to add the following rule

-dontwarn okio.**

Add it to the bottom of the file like this

 

You can also exclude specific classes from getting compiled with ProGuard.

To do this, simply add the rule for the classes using keep class.

For example, if you don’t want to ProGuard the class FlyActivity.java, then you have to add the rule like this

-keep class com.gurusurend.FlyActivity**

 

Keeping all rules organized

 

When several libraries are used, it is advised to keep all the proguard files organized in a folder and tell the build file to check for the specific folder.

To achieve this, create a proguard directory inside your app folder and have a separate .pro file for each library you use.

app
|– libs
|– proguard
|– butterknife.pro
|– crashlytics.pro
|– square-okio.pro
|– src
|– build
|– build.gradle

 

Now access all the files of this proguard folder using the fileTree method.

proguardFiles getDefaultProguardFile(proguard-android.txt), fileTree('proguard')

 

This method of using proguard in your build.gradle file makes your application security more systematic and organized. Also, it’s always good and recommended to use a feature that is provided by the Google developers team.

Leave a Reply

Your email address will not be published. Required fields are marked *