In an Android app, the paid flavor's release build crashes at startup with a ClassNotFoundException debug builds never show; how do you diagnose and fix it?
answer
- only the optimized build differs
- a class reached by name
- deobfuscate with that build's mapping
- narrow rule, or a real reference
- test the release variant in CI
basics
~20 sOnly the release build runs R8, which removed or renamed a class the app loads by name. Retrace with that build's mapping.txt, then keep those classes with a narrow rule or @Keep, or reference them directly.
solid answer
~50 sDebug builds skip R8, so a release-only crash points at code R8 could not see being used: here the paid flavor creates classes with `Class.forName` from names in a config file. Reproduce it by installing the paid release variant, then read the trace: the exception message still carries the name the code asked for, and the obfuscated frames are deobfuscated with the `mapping.txt` R8 wrote for that exact build (ReTrace, or Android Studio's Logcat, which retraces automatically since AGP 8.12). Fix it narrowly: a rule such as `-keep class * implements com.example.notes.export.Exporter { public <init>(); }`, or `@Keep` on those classes; better still, replace the reflection with a map of constructors R8 can see. If the reflection is inside a library, that library should ship consumer keep rules. Then add a CI job that runs UI tests against the optimized build.
code
plaintext · 4 lines# Exporters are created by name from the config file, so R8 sees no reference.
-keep class * implements com.example.notes.export.Exporter {
public <init>();
}go deeper
Recall that release builds are optimized by R8 and that classes loaded by name need keep rules.
Explain why reflection hides code from R8 and how mapping.txt turns obfuscated frames back into source names.
Walk through reproducing on the release variant, retracing with the matching mapping, choosing the narrowest fix, and adding a CI job against the optimized build.
Set the team's rules: minimal reflection, reviewed keep rules, mapping files archived per release, and release-variant tests as a merge gate.
## Why only the release build breaks The debug and release builds compile the same source, but usually only the release build type runs **R8**. R8 keeps what it can reach from entry points such as the activities and services in the manifest and from static references in code; everything else is removed, and what remains is renamed. Code that the app reaches **only by name at run time** is invisible to that analysis: - `Class.forName(nameFromConfig)` and other reflection driven by strings from a file, a server or the manifest's metadata. - JSON libraries that fill fields by reflection. - Enum classes reached only through serialized arguments, the case Jetpack Navigation asks you to mark with `@Keep`, and classes named from native code. In this case the paid flavor has export plugins in `src/paid/` that are created from class names listed in a config file. Nothing references them statically, so R8 removed them, or renamed them so the configured names no longer match. Upgrading AGP can surface the same crash with no code change: full mode became the default in AGP 8.0, and AGP 9.1 started repackaging classes by default. ## Diagnose 1. **Reproduce it locally.** Build and install the same variant, for example `assemblePaidRelease`, rather than a debug build; the crash depends on the optimized output. 2. **Read the message before the frames.** A `ClassNotFoundException` names the class the code asked for, as written in the config, which tells you what to keep. 3. **Deobfuscate the frames.** R8 writes a `mapping.txt` for every optimized build; use the one from that exact version and variant, because the renaming changes whenever the code or the rules change. ReTrace turns `a.b.a` frames back into source names, and Android Studio's Logcat retraces automatically since AGP 8.12. 4. **Confirm what R8 did.** Inspect the release APK's DEX in Android Studio's APK Analyzer: if the class is gone, it was removed; if it is present under another name, it was renamed. 5. **Check the dependencies.** If the failing reflection is inside a library, its AAR should carry consumer keep rules; a missing or outdated rule is the library's bug. ## Fix, from best to bluntest | Fix | Effect | When | |---|---|---| | Replace reflection with a direct reference (a map of constructors) | R8 sees every class and optimizes freely | You own the code | | Narrow keep rule on the exact classes and members | Only those stay unrenamed and unremoved | The names must stay dynamic | | Conditional `-if` rule | Keeps members only for classes that match | A pattern across many classes | | `@Keep` from `androidx.annotation` | Keeps the annotated class or member | A handful of classes | | `-dontobfuscate` or keep-everything rules | Hides the bug, gives up most of the optimization | Never as the final fix | Rules belong in a `.keep` file under `src/<variant>/keepRules` with the AGP 9.3 optimization DSL, or in the module's `proguard-rules.pro` with the legacy DSL. Global options such as `-dontobfuscate` in a library's consumer rules no longer work: since AGP 9.0 apps filter them out. ## Getting the rule right A keep rule that matches nothing does not fail the build, so the crash can survive a rule that looks right. Check these before shipping it: - Use the fully qualified, compiled name: `com.example.notes.export.PdfExporter`, not `PdfExporter`. - Keep the members the reflection actually calls. `Class.forName(...).getDeclaredConstructor().newInstance()` needs `public <init>();`, and a rule that lists no members keeps only that default constructor anyway. - `-keep class * implements com.example.notes.export.Exporter` matches the implementations but not the interface itself; keep the interface too if code looks it up by name. - Re-run the release variant after the change and confirm the class is present under its original name. ## Make it not happen again - Run instrumented UI tests against the optimized release variant in CI, not only against debug. - Archive `mapping.txt` with every release artifact, and upload it wherever crashes are reported, such as the Play Console's ReTrace mapping file upload. - Review new keep rules like code: each one should name the reason and the narrowest set of classes. - Read the R8 changes in each AGP release note before upgrading.
- Production crash reports show frames like a.b.a.c(Unknown Source); how do you read them?Deobfuscate them with the `mapping.txt` that R8 produced for that exact release build, using ReTrace or a crash tool that has the mapping uploaded. A mapping from a different version or variant maps names wrongly, because the renaming changes whenever the code or the rules change. Archive the mapping with every release artifact so it is there when the report arrives.
- A third-party library crashes only in your R8 build; whose keep rule should fix it?The library's: it should ship consumer rules inside its AAR so every app keeps what its reflection needs. Until a fixed version ships, add a narrow rule for the library's classes in your app and leave a note to remove it. Global options in a library's rules no longer help, because since AGP 9.0 apps filter them out.
- If it works in the debug build, R8 cannot be the cause
- Any mapping.txt from the same branch can deobfuscate the trace
- Adding -dontobfuscate fixes the crash for good
- Keeping com.example.** with every member is a fine permanent fix
- Removed classes can only be found by decompiling by hand