Configure ProGuard/R8 for Android release builds with safe defaults
Configures ProGuard/R8 code minification and resource shrinking with safe default rules.
| Input | Required | Default | Description |
|---|---|---|---|
| project_path | Yes | . | Android project root |
Create or update app/proguard-rules.pro with safe defaults:
cat > app/proguard-rules.pro << 'EOF'
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in the Android SDK.
# Keep line numbers for debugging stack traces
-keepattributes SourceFile,LineNumberTable
# Hide the original source file name
-renamesourcefileattribute SourceFile
# Keep data classes and their fields
-keepclassmembers class * {
@kotlinx.serialization.SerialName <fields>;
}
# Keep Parcelables
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# Keep custom views
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
# Keep native methods
-keepclasseswithmembernames class * {
native <methods>;
}
EOF
Note: If proguard-rules.pro already exists, ask the user if they want to:
Update app/build.gradle.kts to enable ProGuard/R8:
android {
// ... existing config ...
buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
Detection logic:
buildTypes.release already existsisMinifyEnabled is already setproguardFiles if present, append if neededMANDATORY: Run these commands:
# Verify ProGuard rules file exists
test -f app/proguard-rules.pro && echo "โ ProGuard rules exist"
# Verify minification is enabled
grep "isMinifyEnabled = true" app/build.gradle.kts && echo "โ Minification enabled"
# Verify resource shrinking is enabled
grep "isShrinkResources = true" app/build.gradle.kts && echo "โ Resource shrinking enabled"
Expected output:
| Output | Location | Description |
|---|---|---|
| ProGuard rules | app/proguard-rules.pro | Safe default keep rules |
| Build config | app/build.gradle.kts | Minification enabled |
Cause: ProGuard removed required classes Fix: Add keep rules for the failing classes to proguard-rules.pro
Cause: ProGuard obfuscated code that uses reflection Fix: Add keep rules for classes used via reflection
Add these rules based on your project dependencies:
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepclassmembers,allowshrinking,allowobfuscation interface * {
@retrofit2.http.* <methods>;
}
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.google.gson.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
-keepattributes *Annotation*, InnerClasses
-dontnote kotlinx.serialization.AnnotationsKt
-keepclassmembers class kotlinx.serialization.json.** {
*** Companion;
}
-keep class androidx.health.connect.client.** { *; }
-keep class androidx.health.platform.client.** { *; }
-keep class * extends androidx.room.RoomDatabase
-keep @androidx.room.Entity class *
Important: Test libraries should NEVER be in release builds. They are androidTestImplementation only.
If you need to run instrumented tests on release builds (e.g., to verify signing), use a separate test ProGuard file:
Step 1: Create app/proguard-rules-androidTest.pro:
# Keep EVERYTHING in test APK - we only care about signing, not size
-dontobfuscate
-dontoptimize
-dontshrink
-keep class ** { *; }
Step 2: Update app/build.gradle.kts:
android {
buildTypes {
release {
isMinifyEnabled = true
signingConfig = signingConfigs.getByName("release")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
// Keep-all rules for test APK only
testProguardFiles("proguard-rules-androidTest.pro")
}
}
testBuildType = "release"
}
Result:
app/proguard-rules.pro exists with safe defaultsisMinifyEnabled = true in app/build.gradle.ktsisShrinkResources = true in app/build.gradle.kts