How app development companies use Proguard in android?


What you need to know about Proguard in Android ?
Proguard is free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. Mobile app development companies use proguard in android , it optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names.
1. Shrinking – detects and removes unused classes, fields, methods, and attributes.
2. Optimization – analyzes and optimizes the bytecode of the methods.
3. 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 it is highly useful. Optimization operates with java bytecode, though, since Android runs on special bytecode which is converted from java bytecode some optimizations won’t work well.
So, let’s talk about the benefits
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. In fact, this process directly supports mobile app testing by ensuring testers work with a release build that mirrors production conditions, making it possible to catch issues that might only appear after code shrinking or obfuscation.
How to Enable Proguard in Android Studio?
1. Proguard is integrated into the Android build system.
2. Proguard runs only when you build your application in release mode.
3. Having Proguard run is completely optional, but highly recommended.
4. In Android Studio project, the minifyEnabled property in the build. gradle file enables and disables Proguard for release builds.
5. The minifyEnabled property is part of the buildTypes release block that controls the settings applied to release builds.
6. Set the minifyEnabled property to true to enable Proguard.
7. The getDefaultProguardFile(‘proguard-android.txt’) method obtains the default Proguard settings from the Android SDK tools/proguard folder.
8. Android Studio adds the proguard-rules.pro file at the root of the module, which helps to add custom Proguard rules.
Now, let’s see the drawbacks
1. Potential misconfiguration causes the app to get crash.
2. Additional testing is required
3. Stacktraces are difficult to read with obfuscated method names.
4. 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
Happy Coding !