Skip to content
All posts
May 26, 20263 min read

Shrinking, Obfuscation, and Debugging Release Builds with ProGuard and R8 in Android

In this article, we will discuss how to use ProGuard and R8 to shrink and obfuscate your Android app, and how to debug release builds.

AndroidKotlin
Share:

As a solo Android developer with over 13 years of experience, I have worked on numerous Android applications, and one of the most critical aspects of releasing a production-ready app is optimizing its size and performance. In this article, we will discuss how to use ProGuard and R8 to shrink and obfuscate your Android app, and how to debug release builds.

Context

When we talk about optimizing an Android app, we are usually referring to the process of making the app smaller and faster. This is crucial because a smaller app size results in faster download times, lower storage requirements, and improved performance. However, this process can be complex and requires careful consideration.

ProGuard and R8 are two popular tools used for shrinking and obfuscating Android apps. ProGuard is a Java obfuscator that can rewrite and obfuscate the code, making it more difficult for reverse engineers to understand. R8 is a new tool introduced by Google that is specifically designed for Android apps and is more efficient and effective than ProGuard.

Shrinking with R8

To shrink an Android app with R8, we need to add the following configuration to our

code
build.gradle
file:

groovy
android {
    // ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            // Use R8 instead of ProGuard
            shrinkResources true
            // Enable the following to shrink the APK size
            // shrinkResources true
        }
    }
}

We can also configure the ProGuard rules to exclude certain classes or methods that we don't want to be obfuscated. For example:

groovy
android {
    // ...
    buildTypes {
        release {
            // ...
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            // Exclude the Retrofit library
            -keep class retrofit2.** { *; }
            -keep interface retrofit2.** { *; }
        }
    }
}

Obfuscation with ProGuard

To obfuscate an Android app with ProGuard, we need to enable it in the

code
build.gradle
file:

groovy
android {
    // ...
    buildTypes {
        release {
            // ...
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            minifyEnabled true
        }
    }
}

We can also configure the ProGuard rules to exclude certain classes or methods that we don't want to be obfuscated. For example:

groovy
android {
    // ...
    buildTypes {
        release {
            // ...
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            -keep class retrofit2.** { *; }
            -keep interface retrofit2.** { *; }
        }
    }
}

Debugging Release Builds

When we enable ProGuard or R8, the class names and method names are obfuscated, making it difficult to debug the app. To debug release builds, we need to use the

code
debuggable
tag in the
code
application
tag in the
code
AndroidManifest.xml
file:

xml
<application
    android:debuggable="false"
    // ...>

However, this will not work if we have enabled ProGuard or R8. Instead, we need to use the following configuration in the

code
build.gradle
file:

groovy
android {
    // ...
    buildTypes {
        debug {
            // ...
            debuggable true
        }
        release {
            // ...
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            shrinkResources true
        }
    }
}

Key Takeaways

  • Use R8 instead of ProGuard for shrinking and obfuscating Android apps.
  • Exclude certain classes or methods from obfuscation by using ProGuard rules.
  • Use the
    code
    debuggable
    tag in the
    code
    application
    tag in the
    code
    AndroidManifest.xml
    file to debug release builds.
  • Use a separate
    code
    debug
    buildType to debug the app without enabling ProGuard or R8.
Share:
S

Sudarshan Chaudhari

AI Systems Builder / Product Engineer

Bangkok, Thailand

Solo Android developer with 13+ years in QA, building Android apps, AI automation systems, and developer tools at SudarshanTechLabs.

Stay updated

Get new posts on Android, Kotlin, and solo dev straight to your inbox.

Newsletter preferences

Related Apps

MyFamilyTracker

Real-time family location sharing — Firebase Realtime DB for sub-second propagation, WorkManager + ForegroundService for OS-compliant background collection, geofencing via Google Maps API.

Building something? Available for Android dev and QA consulting.

Work with me

Comments — powered by Giscus

Apps tagged with this