Proguard is free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names.
- Shrinking – detects and removes unused classes, fields, methods and attributes.
- Optimization – analyzes and optimizes the bytecode of the methods.
- Obfuscation – renames the remaining classes,fields and methods using short meaningless names.
Why Proguard for Android ?
Android Applications are quite easy to reverse engineer, so if you want to prevent this from happening, you should use Proguard for its main function : Obfuscation.The other two important functions of Proguard are Shrinking and Optimization. Shrinking eliminates unused codes and its is highly useful. Optimization operates with java byte code, though, since Android runs on special bytecode which is converted from java bytecode some optimizations won’t work well.
Proguard obfuscates your code by removing unused code and renaming classes, fields and methods with semantically obscure names which make the code base, smaller and more efficient. The result is a smaller sized .apk file that is more difficult to reverse engineer.
How to Enable Proguard in Android Studio ?
- Proguard is integrated into Android build system.
- Proguard runs only when you build your application in release mode.
- Having Proguard run is completely optional, but highly recommended.
- In Android Studio project, the minifyEnabled property in the build.gradle file enables and disables Proguard for release builds.
- The minifyEnabled property is part of the buildTypes release block that controls the settings applied to release builds.
- Set the minifyEnabled property to true to enable Proguard.
- The getDefaultProguardFile(‘proguard-android.txt’) method obtains the default Proguard settings from the Android SDK tools/proguard folder.
- Android Studio adds the proguard-rules.pro file at the root of the module, which helps to add custom Proguard rules.
- Potential misconfiguration causes the app to get crash.
- Additional testing is required
- Stacktraces are difficult to read with obfuscated method names.
- ClassNotFoundExceptions, which happens when Proguard strips away an entire class that application calls.
We can fix ClassNotFoundExceptions by adding a -keep line in Proguard configuration file.
-keep public class <MyClass>
Happy Coding !