Sphere
Log in Join
☆ 6

R8 minification breaks Gson models in release build

Android release build with minifyEnabled true (R8, AGP 8.4). Debug works. Release installs but every API parse returns objects with null fields, no crash. Using Gson 2.10.1 with data classes: data class User(val id: String, val name: String). I think R8 is stripping or renaming the fields. Do I need @Keep everywhere or is there a proguard rule? No crash makes it harder to spot than you would think.

⚑ Report

4 replies

☆ 0
M moon ·2026-10-25#1

Gson uses reflection on field names, so R8 renaming fields breaks it. Add rules: -keep class com.yourapp.model.** { *; } and -keepattributes Signature. If using Kotlin with Gson, also keep the metadata. Better long-term: use Gson TypeAdapter or Moshi with codegen, which is reflection-free and R8-friendly.

⚑ Report
☆ 0
I inode ·2026-11-01#2

The -keep rule for the model package fixed nulls. I will look at Moshi, we are stuck with Gson for now because of an old shared module. Kept the rule scoped to just the model package so we do not keep the whole app.

⚑ Report
☆ 0
G glitch ·2026-11-03#3

Careful with data class -keep { ; }, it also keeps synthetic methods and can bloat the dex a bit. A tighter rule keeping just the fields is enough: -keepclassmembers class com.app.model.* { <fields>; }. That is usually all Gson needs.

⚑ Report
☆ 0
G glitch ·2026-11-09#4

You can verify what R8 actually removed by checking build/outputs/mapping/release/usage.txt and mapping.txt. Search for your model class. If it is in usage.txt it was stripped entirely. Also keep in mind that mapping.txt is needed to deobfuscate release stack traces. Archive it per release instead of only keeping it locally.

⚑ Report
Log in to reply.