R8 is the Android code shrinker and optimizing compiler that turns Java and Kotlin bytecode into compact DEX files while removing unused code, rewriting the remaining code for speed and renaming symbols to short identifiers. In mobile performance work, the R8 compiler is the main build-time lever for smaller APK and AAB downloads, faster class loading and leaner runtime behavior on Android devices.
R8 became the default shrinker in Android Gradle Plugin 3.4 and replaced the older two-step pipeline of ProGuard plus the D8 dexer. It reads the same ProGuard keep rules, so most projects migrated without rewriting configuration, yet R8 applies more aggressive whole-program optimizations than ProGuard ever did for Android targets.
How the R8 compiler processes an Android app
R8 works as a whole-program compiler: it sees the app code and all library dependencies at once, which lets it prove that a class, method or field is never reachable and then delete it. The process starts from entry points, such as activities declared in the manifest and classes named in keep rules, and traces every reference from there.
Everything the tracer cannot reach is discarded, a step usually called tree shaking. The surviving code is then optimized, obfuscated and emitted as DEX bytecode in a single pass, which is faster than the historical sequence of separate tools. The following table summarizes the stages that run inside one R8 invocation.
| Stage | What happens | Main effect |
|---|---|---|
| Shrinking | Unreachable classes, methods and fields are removed | Smaller DEX files |
| Optimization | Inlining, class merging, constant propagation | Less code, fewer calls |
| Obfuscation | Names are replaced with short identifiers | Smaller string pool, harder reverse engineering |
| Desugaring | Newer Java language features are rewritten for older Android versions | Wider API level support |
| Dexing | Optimized bytecode is converted to DEX format | Output ready for ART |
Resource shrinking is a related but separate feature. When shrinkResources is enabled, the build removes unreferenced drawables, layouts and strings after R8 has shown which code, and therefore which resource IDs, is still in use.
Optimizations that affect runtime performance
The size savings of R8 are well known, but its runtime optimizations matter just as much for mobile performance. Fewer classes mean less work for the Android Runtime (ART) during class loading and verification, which shortens app startup time, and inlined code avoids call overhead on hot paths.
| Optimization | Mechanism | Performance benefit |
|---|---|---|
| Method inlining | Small or single-caller methods are copied into callers | Fewer invocations on hot paths |
| Class merging | Classes with one subclass or implementation are combined | Fewer classes to load and verify |
| Devirtualization | Interface calls with one target become direct calls | Cheaper dispatch |
| Enum unboxing | Simple enums are replaced with integer constants | Fewer allocations |
| Constant propagation | Known values replace field reads and branches | Dead branches removed |
| Startup layout | Classes from a startup profile are placed in the primary DEX | Fewer page faults at launch |
Recent Android Gradle Plugin versions can feed a startup profile to R8 so that classes needed during launch are grouped together. This complements Baseline Profiles, which tell ART which methods to compile ahead of time; R8 improves the DEX layout, while Baseline Profiles improve compilation coverage.
R8 compared with ProGuard and D8
R8 is often confused with ProGuard because both accept the same rule syntax, but they are different tools. ProGuard is an independent Java bytecode optimizer that produces Java class files, whereas R8 is built by the Android toolchain team and emits DEX directly. D8 is the plain dexer that R8 extends.
| Tool | Output | Shrinks code | Typical role today |
|---|---|---|---|
| R8 | DEX bytecode | Yes, plus optimization | Default for release builds |
| ProGuard | Java class files | Yes | Legacy Android setups, non-Android Java |
| D8 | DEX bytecode | No | Debug builds without minification |
Since Android Gradle Plugin 8.0, R8 runs in full mode by default. Full mode makes fewer assumptions to stay compatible with ProGuard behavior, so it removes more code but also requires more precise keep rules for code accessed through reflection.
Configuring R8 in a release build
R8 is activated by setting isMinifyEnabled = true in the release build type, usually together with isShrinkResources = true and the default file proguard-android-optimize.txt. Project-specific rules go into a file such as proguard-rules.pro, and library modules ship consumer rules that are merged automatically into the app configuration.
-keeppreserves a class and its members from removal and renaming.-keepclassmemberspreserves only members, for example fields used by a JSON serializer.-keepattributesretains metadata such as annotations and generic signatures.-dontobfuscatedisables renaming, which is useful while diagnosing a problem but costs size.
Every release build produces a mapping file that links obfuscated names back to the original ones. This file must be archived for each released version and uploaded to the crash analytics service or the Google Play Console, otherwise stack traces from production devices are unreadable. The retrace tool uses the same file to decode traces locally.
| Situation | Recommended approach | Reason |
|---|---|---|
| New app | Enable R8 full mode from the first release | Rules grow with the code |
| Heavy reflection | Add narrow keep rules per class | Broad rules waste the savings |
| Library author | Publish consumer keep rules | Apps stay correct without guessing |
| Startup-critical app | Combine R8 with startup and Baseline Profiles | Layout and compilation both improve |
Limitations and common R8 mistakes
The most frequent failure is a runtime crash caused by missing keep rules. R8 cannot see code reached through reflection, string-based class lookups, JNI or some serialization libraries, so it may delete or rename classes that are needed at runtime. These errors appear only in release builds, which is why release variants must be tested before shipping.
| Symptom | Likely cause | Fix |
|---|---|---|
| ClassNotFoundException | Class used only through reflection was removed | Add a targeted keep rule |
| Empty JSON fields | Model fields were renamed | Keep members or use annotations |
| Unreadable stack traces | Mapping file not uploaded | Store and upload mapping per release |
| Little size reduction | Overly broad keep rules | Narrow the rules and audit libraries |
Size reductions vary widely. Apps with large dependency trees often see the DEX size shrink by a substantial fraction, while small apps with few libraries gain less. Build time also increases, because whole-program analysis is more expensive than plain dexing, so many teams run R8 only for release and benchmark variants.
The simple explanation
Imagine packing for a trip with a large toolbox. R8 looks at every tool, checks which ones the trip actually needs and leaves the rest at home. It then labels the remaining tools with short tags instead of long names so the box is lighter still.
For an Android app, this means a smaller download and a faster start. The only catch is that R8 can remove something that the app uses in a hidden way, so developers add notes that say keep this, it is needed.
How it is used in practice
A team maintaining a shopping app for Android notices that its release AAB has grown past 40 MB and that cold starts on low-end phones take more than two seconds. The app uses a networking library, a JSON parser, an image loader and several analytics SDKs, but release builds had never enabled minification.
The team turns on R8 full mode with resource shrinking, adds keep rules for the data models parsed by reflection and adds a startup profile generated with Macrobenchmark. Release testing reveals one crash in a payment screen, fixed with a single -keepclassmembers rule. After the change, the download size drops noticeably and the cold start on the slowest test device improves by a few hundred milliseconds. The mapping file is now uploaded automatically with each release, so crash reports remain readable.