Skip to content

Android and its ProGuard

Published: at 5 min read

Table of contents

Open Table of contents

What is ProGuard in Android

ProGuard and its key functions

ProGuard Workflow

ProGuard works in Android by performing three main tasks: code shrinking, code optimization, and code obfuscation. It is integrated into the Android build process and is used to improve the performance and security of Android applications.

proguardWorks

The result is a smaller, optimized, and obfuscated APK file that can be distributed to users.

Steps to Define ProGuard in an Android project:

  1. Create a ProGuard Configuration File: In the Android Studio project, create a file named proguard-rules.pro in the app module’s proguard directory if it doesn’t already exist.

  2. Specify ProGuard Rules: Define the ProGuard rules in the proguard-rules.pro file, which tell ProGuard what to keep, optimize, or obfuscate.

    Syntax for rule definitions:

    • -keep: Keep specific classes, methods, or fields.
    • -dontwarn: Suppress warnings about specific classes or packages.
    • -keepattributes : Keep specific attributes, such as annotations.
    • -assumenosideeffects : Make assumptions about methods with no side effects.
    • -optimizations : Configure optimization options.

    Example of a ProGuard rule to keep a specific class:

    -keep class com.example.myapp.MyClass {
        public *;
    }
  3. Add ProGuard Configuration to Gradle: In the app’s build.gradle file, specify the location of the ProGuard configuration.

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

Explanation :

  1. Build the App: After defining the ProGuard rules and adding them to the Gradle configuration, build the Android app using the release build type. ProGuard will be invoked during the build process to process the code based on the defined rules.

  2. Testing and Debugging: Thoroughly testing of the app ensure that ProGuard does not break any functionality. It’s essential to debug and make adjustments to the ProGuard rules if required.

Custom Module in app and enabling ProGuard

buildTypes {
    release {
        minifyEnabled true
        shrinkResource true
        consumerProguardFiles 'library-proguard-rule.pro'
    }
}

We can define all Proguard rules for any dependency used in any module in two places:

Negative Points to be reminded while using ProGuard

  1. Configuration Complexity: Writing and maintaining ProGuard rules can be complex and also it is required to ensure that ProGuard doesn’t inadvertently remove or obfuscate code that’s essential for your app’s functionality.

  2. Reflection and Dynamic Class Loading: If the codebase rely on reflection then it is important to create specific rules as ProGuard can interfere with reflection and dynamic class loading.

  3. Debugging Challenges: After enabling ProGuard, the obfuscation of class and method names makes debugging harder to trace errors and exceptions back to your original source code.

  4. Increased Build Time: Enabling ProGuard, analysis and processing can be resource-intensive and also can increase the app’s build time.

  5. Impact on APK Size: While the primary purpose of ProGuard is to reduce APK size, it may not always result in significant size reductions, especially if the app is already well-optimized or if the bulk of the APK size is due to non-code assets like images and videos.

  6. Impact on Third-Party Libraries: ProGuard may require to configure rules to ensure that the third-party libraries are correctly processed. Incorrect configuration can lead to issues with library functionality.

  7. Potential for Over-Optimization: ProGuard’s aggressive optimization settings can sometimes result in unexpected removal of code that one intended to keep. Careful configuration is required for optimization and maintaining functionality.

Alternatives of ProGuard

With the advancements in the Android Ecosystem, there are alternative tools and techniques available for achieving similar results.

Conclusion

ProGuard remains a valuable tool for many Android developers. Its benefits in terms of optimizing APK size, improving app startup times, and enhancing app security often outweigh its drawbacks.

Careful configuration and thorough testing can help mitigate many of the challenges associated with using ProGuard.

For More Information, please refer this link for ProGuard

Happy Learning !

Share :
Written by:Parita Dey

Interested in Writing Blogs, showcase yourself ?

If you're passionate about technology and have insights to share, we'd love to hear from you! Fill out the form below to express your interest in writing technical blogs for us.

If you notice any issues in this blog post or have suggestions, please contact the author directly or send an email to hi@asdevs.dev.