What is ProGuard in Android ?

ProGuard  



ProGuard is a tool that shrinks, optimizes and obfuscates code. While there are other tools available for developers, ProGuard is readily available as part of the Android Gradle build process and ships with the SDK.

There are many reasons why you might want to enable ProGuard when building your app. Some developers care about the obfuscation part more, but for me the main benefit is the removal of all unused code that you otherwise ship with your APK as part of the classes.dex file.

Proguard can help us limit the size of app and also reduce the code size smaller.
 As less code equals shorter compilation times on the device and less storage used.

What is code obfuscation ? 
 It means changing all identifiers(package, class and class members) to a use short names like a.A or b.c and d.d.e.e 

Proguard has resource shinker enables that will remove out resources that are not referenced from code to our project. 

How to use Proguard ? 
add this code in you build.gradle file 
buildTypes {/* you will normally want to enable ProGuard only for your release
builds, as it’s an additional step that makes the build slower and can make debugging more difficult */

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


Proguard takes your project’s class files as input, then looks at all possible application entry points and calculates the map of all code reachable from those entry points, then removes the rest (dead code, or code that can never run because it’s never called).
 It adds explicit  forAndroid application entry points, so all your Activities, Services, BroadcastReceivers and ContentProviders from the Android Manifest should be left intact. 

add -dontwarn rules to ProGuard config if it breaks your build. 

-dontwarn javax.annotation.Nullable
-dontwarn javax.annotation.ParametersAreNonnullByDefault


When ProGuard removes too much and too less - we have to modify the rules. 

Comments

Popular Posts