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
1 2 3 4 5 6 7 8 9 10 |
android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } ... } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in E:\AndroidWorkspace/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} -dontwarn okio.** |
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.