Understanding ProGuard in Android

Mayur Waghmare
Mobile Innovation Network
3 min readJun 10, 2024

Proguard is an open-source tool designed to shrink, optimize and obfuscate Java bytecode.

It shrinks APK sizes by removing unused code and obfuscates the remaining code to prevent reverse engineering.

Need of ProGuard in Android?

1. Code Optimisation :
-
ProGuard detects & removes unused elements making APK size smaller.
- Smaller APKs mean quicker downloads.

2. Enhanced Security :
-
ProGuard obfuscates code by renaming classes, methods, and attributes.
- Obfuscated code prevents reverse engineering, safeguarding your code.

3. Improved Performance :
-
Removal of unused code helps faster build process and smoother operations.
- Smaller APK sizes decrease app memory usage, enhancing device performance.

4. App Size Reduction :
-
ProGuard shrinks APK size by removing unnecessary code, resources, and metadata.
- Smaller apps save device storage and reduce bandwidth.

What is Shrinking in ProGuard ?

Shrinking means ability to locate and remove unnecessary classes, fields, methods & attributes from your app’s bytecode or any libraries you may be using.

What is Obsfucation in ProGuard ?

Obfuscation is the process by which ProGuard renames classes, fields & methods with short, meaningless names, which makes the codebase difficult to understand.

Implementation

Step 1: Enable ProGuard in your app’s build.gradle file:

android {     
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

Here, minifyEnabled true tells Gradle to run ProGuard when building the APK.

Step 2: Test Your App

After integrating ProGuard, thoroughly test your app. The changes can sometimes introduce unexpected behaviour.

Step 3: mapping.txt File

Once you’ve built your project with ProGuard enabled, it will generate a mapping.txt file.

  • Maps the original class, method and field names to their obfuscated counterparts.
  • Crucial for de-obfuscating stack traces in the obfuscated app.

ProGuard Rules

ProGuard rules determine how your application code is treated during the optimisation and obfuscation processes.

Lets look at some common examples:

Keeping all class members: This rule instructs ProGuard to keep all members (fields and methods) of all classes.

keepclassmembers class com.example.MyClass {     *; }

Keeping all classes in package: This rule preserves all classes and their members within the specified package.

keep class com.example.mypackage.** { *; }Keeping all class members:

Keeping classes that implements a specific interface: This rule ensures that classes implementing the specified interface are retained.

keep class * implements com.example.myapp.InterfaceName

Keep Application Entry Points: This rule instructs ProGuard to keep all public methods and fields within the MainActivity class

keep class com.example.myapp.MainActivity { public *; }

Keep model classes: This rule tells ProGuard to keep all classes and their members within the model package.

keep class com.example.myapp.model.** { *; }

Keep specific methods: This rule preserves all public static methods in the Utility class within the com.example.myapp.utils package.

keepclassmembers class com.example.myapp.utils.Utility {     
public static int* *(...);
}

Alternatives to ProGuard — R8

R8 is a Google product

  • R8 has better performance because convert .class directly into .dex without extra step (optimised .class)
  • R8 has better compatibility with Kotlin

Other Tools to Combine with Proguard

Integrating other tools and best practices can further enhance the efficiency and security of your Android applications:

Lint: Integrated into Android Studio, Lint detects bugs, performance issues, and security vulnerabilities in the codebase.

Firebase Performance Monitoring: Monitors real-time app performance, highlighting areas for enhancement like slow network requests or CPU-heavy tasks.

Conclusion

  • Consider app dependencies and configurations when applying ProGuard rules.
  • Refer to library documentation for recommended ProGuard configurations for specific dependencies.
  • Test your app thoroughly after applying ProGuard.
  • Proper configuration and testing are crucial for ProGuard’s success.

Thank you for taking the time to read this article. If you found the information valuable, please consider giving it a clap or sharing it with others who might benefit from it.

Any Suggestions are welcome. If you need any help or have questions for Code Contact US. You can follow us on LinkedIn for more updates 🔔

--

--

Mayur Waghmare
Mobile Innovation Network

"Mobile App Developer specializing in Android, iOS, and Flutter. Passionate about crafting user-focused, efficient mobile solutions."