DEV Community

Cover image for Build small-sized apps using ProGuard
Nwokocha wisdom maduabuchi
Nwokocha wisdom maduabuchi

Posted on

Build small-sized apps using ProGuard

It's important to enable proguard in our project, so if a question arises in the form "should all android apps enable proguard?" I will say yes.
Because you will build a small-sized app, unused code removed and all identifiers changed to protect from decompiling apk.

What is ProGuard

ProGuard is a tool that shrinks, optimizes and obfuscates code, it is readily available as part of the Android Gradle build process and ships with the SDK.

Why Proguard

1: it gets rid of unused code
2: it renames identifiers to make the code smaller,
3: it performs the whole program optimizations.

Note:

However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. That’s because these compile-time optimizations increase the build time of your project and might introduce bugs if you do not sufficiently
So, it’s best to enable these compile-time tasks when building the final version of your app that you test prior to publishing. To enable shrinking, obfuscation, and optimization, you will include the following in your project-level build.gradle file.

How to Setup Proguard


android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}


Conclusion:
When you use ProGuard you should always QA your release builds thoroughly, either by having end-to-end tests or manually going through all screens in your app to see if anything is missing or crashing.
also, some Libraries include Proguard/R8 guide to help protect some of the classes created to avoid missing classes example Retrofit

Top comments (0)