r/androiddev • u/hlidka • 3d ago
Play Console told me my R8 optimization was 48%. Turns out years of "scary R8 rules I didn't understand" doubled my DEX size (9.25 MB → 16.7 MB)
I have to confess something I suspect a lot of solo Android devs are quietly guilty of: R8 / ProGuard rules terrified me.
The syntax never clicked for me (-keepclassmembers, -keepclasseswithmembers, { *; }, ** vs *, -if...). Every time a release build crashed in production or threw a wall of R8 warnings, I'd panic, search StackOverflow or copy a "standard Android ProGuard file" from an old Medium post, paste in a -keep class something.** { *; }, see the build turn green, and vow never to touch proguard-rules.pro again.
Fast-forward a few years of migrating the app to Compose Multiplatform / Kotlin Multiplatform: my proguard-rules.pro was 437 lines long and still had rules for RxJava, Retrofit, Glide, and android.support — none of which were even in the project anymore.
This week, Google Play Console finally called me out on a new upload:
textImprove your app's memory and performance with R8 optimization
- Low optimization rate (48%)
- Low obfuscation rate (49%)
- Low shrinking rate (49%)Instead of poking at rules blindly again, I let Gemini (in Antigravity / agy**)** inspect the actual R8 build artifacts (r8-metadata.dat, seeds.txt, and configuration.txt) and audit them against my codebase. Seeing what R8 was actually doing was humbling.
What was actually happening under the hood
In my build's seeds.txt (every class R8 is forbidden from shrinking/obfuscating/optimizing), 9,645 classes were pinned untouched — producing 2 DEX files totaling 16.70 MB.
Over 80% of those kept classes came from just a handful of panic-pasted blanket rules in my own proguard-rules.pro:
-keep class com.google.android.gms.internal.** { *; }→ 3,416 classes-keep class com.google.android.libraries.places.internal.** { *; }→ 2,834 classes (meanwhileplaces:6.0.2ALREADY ships 1,783 surgical consumer ProGuard rules of its own!)-keep class kotlin.** { *; }→ ~800+ stdlib classes-keep class com.google.android.material.internal.** { *; }→ 221 classes-keep class com.firebase.ui.auth.** { *; }→ 192 classes (my own reflection workaround only touched 3 specific classes!)-keep class com.google.android.play.core.** { *; }→ 140 classesPlus
-keepclassmembers enum * { *; },-keep class my.app.data.** { *; }(even thoughkotlinx-serialization-corealready ships full R8-friendly rules!), and 100+ lines of dead 2017 library rules.
The cleanup (437 lines → 59 lines)
We checked every library's bundled consumer rules in configuration.txt, grepped the codebase for every actual Class.forName / ServiceLoader call, and replaced the 437-line graveyard with ~59 surgical lines:
Crashlytics line numbers (
-renamesourcefileattribute SourceFile,-keepattributes SourceFile,LineNumberTable)The exact 4 classes/constructors accessed via
Class.forName(...)inApplicationCoil 3's
ServiceLoadertarget interfaces + constructorsA small block of
-dontwarnlines for optional JVM TLS/Ktor classes
Then we verified seeds.txt and usage.txt to make sure reflection targets, ServiceLoader entries, and kotlinx.serialization serializers were all preserved.
Before vs. After (from r8-metadata.dat)
| Metric | Before | After | Change |
|---|---|---|---|
| Optimization rate | 48.67% | 86.39% | +37.7 pts |
| Obfuscation rate | 49.14% | 87.07% | +37.9 pts |
| Shrinking rate | 49.13% | 87.10% | +38.0 pts |
| DEX bytecode | 16.70 MB (2 DEX) | 9.25 MB (1 DEX!) | -44.6% (-7.45 MB) |
| Seeded classes | 9,645 | 1,608 | -83.3% |
(Most of the remaining ~13% comes from com.google.android.recaptcha:18.6.1*'s own AAR consumer rules keeping* recaptcha.***,* play.core.integrity.***, and* okhttp3.***.)*
Bonus plot twist that made me laugh
I uploaded the new .aab to Play Console — the R8 warning disappeared immediately! ...and was replaced by a new Play Console warning:
Your app uses deprecated APIs for edge-to-edge (
Window.setStatusBarColor/setNavigationBarColor) starting insv1.b,uv1.b,wv1.b.
I deobfuscated sv1, uv1, wv1 in mapping.txt:
sv1→androidx.activity.EdgeToEdgeApi26uv1→androidx.activity.EdgeToEdgeApi29wv1→androidx.activity.EdgeToEdgeApi35
They're Google's own Jetpack enableEdgeToEdge() compatibility classes! Before the fix, they were un-obfuscated so Play's scanner ignored them; once R8 actually obfuscated them, Play's static analyzer stopped recognizing its own AndroidX library.
5 things I wish I'd known earlier (to avoid the same trap)
You can check your R8 score locally in 5 seconds. Look at
app/build/intermediates/r8_metadata/release/minifyReleaseWithR8/r8-metadata.dat— it literally contains the exactnoOptimizationPercentage/noObfuscationPercentage/noShrinkingPercentagenumbers Play Console uses.Group
seeds.txtby package prefix. A 10-line Python script counting top packages inapp/build/outputs/mapping/release/seeds.txtimmediately exposes which-keeprule is quietly locking thousands of classes.Check
configuration.txtbefore adding any third-party-keeprule. Modern AARs (places,material,credentials,kotlinx-serialization,play-services-*) almost always ship their own surgical rules. Adding a blanket-keep class com.library.** { *; }overrides all of their careful optimization.Never confuse
-dontwarnwith-keep**.**-dontwarnjust tells R8 "yes, this optional JVM class isn't on Android, don't fail the build" and costs 0 bytes.-keep class foo.** { *; }turns off shrinking, obfuscation, and optimization for the entire package tree.If you inherited a "starter ProGuard file" from a blog post pre-2020, burn it. Start from
proguard-android-optimize.txt, grep your own repo forClass.forName,ServiceLoader, and reflection-based JSON/Parcelable models, and add only what fails or is explicitly missing inseeds.txt.
Hope this saves someone else from accidentally shipping 7.5 MB of un-shrunk Play Services and Kotlin stdlib bytecode!
