Enable ProGuard in Android Studio

Posted By : Sobraj Yumkhaibam | 12-Apr-2016

Enable Proguard in Android Studio

You need only two files: one is property file where you mentioned proguard config file path and another is config file called proguard.cfg

Few things need to be taken care of:

1. If you  have integrated crash reporting tool like ACRA, GA etc it will report wrong line number method name as your codebase is already unclear. 

2. Need to exclude all packages, classes that uses annotations. Also, open source third party libraries are not required for obfuscation.

3. Proguard is also helpful to remove log statements if you put it into your code. Use Following setting in config file.

-assumenosideeffects class android.util.Log {
     public static *** d(...);
}


When an obscure code outputs a stack trace, the method names are unclear, making debugging difficult, if not impossible. Fortunately, whenever ProGuard runs, it outputs a <project_root>/bin/proguard/mapping.txt file, that shows the original class, method, and field names mapped to their obscure names.

The retrace.sh script on Mac OS X or Linux  can convert an obscure stack trace to a readable one. It is found in the <sdk_root>/tools/proguard/ directory. The syntax to execute the retrace tool is:

retrace.bat|Page on retrace.sh [-verbose] mapping.txt [<stacktrace_file>]

For example:

retrace.bat -verbose mapping.txt obfuscated_trace.txt

    ProGuard | Android Developers
    ProGuard Improvements - Android Tools Project Site


2. Use Progaurd settings to avoid obfuscating class/method name.

You can use -keepclassmembers or -keepclassmembernames

If planning to use ProGaurd, use below lines in config to remove debugging Log API Calls.
-dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5
 
-keep class * extends android.app.Activity
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}


As i said remove all library files from config.

-keep class com.facebook.** { *; }
-keep class com.androidquery.** { *; }
-keep class com.google.** { *; }
-keep class org.acra.** { *; }
-keep class org.apache.** { *; }
-keep class com.mobileapptracker.** { *; }
-keep class com.nostra13.** { *; }
-keep class net.simonvt.** { *; }
-keep class android.support.** { *; }
-keep class com.nnacres.app.model.** { *; }
 
 
# Suppress warnings if you are NOT using IAP:
 -dontwarn com.nnacres.app.**
 -dontwarn com.androidquery.**
 -dontwarn com.google.**
 -dontwarn org.acra.**
 -dontwarn org.apache.**
 -dontwarn com.mobileapptracker.**
 -dontwarn com.nostra13.**
 -dontwarn net.simonvt.**
 -dontwarn android.support.**
 
 
 -keepattributes Signature
 
# For using GSON @Expose annotation
-keepattributes *Annotation*
 
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
 
 
# The official support library.
#-keep class android.support.v4.app.** { *; }
#-keep interface android.support.v4.app.** { *; }
 
#  Library JARs.
#-keep class de.greenrobot.dao.** { *; }
#-keep interface de.greenrobot.dao.** { *; }
 
# Library projects.
#-keep class com.actionbarsherlock.** { *; }
#-keep interface com.actionbarsherlock.** { *; }

 

THANKS

About Author

Author Image
Sobraj Yumkhaibam

Sobraj has been developing Android applications for a while now and is expert in mobile application development . He excels in developing mobile applications with location based intelligence. He loves Modelling as a hobby.

Request for Proposal

Name is required

Comment is required

Sending message..