- R8 acts as an advanced compiler that merges dead code removal, logic optimization, and obfuscation into a single process.
- The correct implementation of conservation rules and the use of full mode allow you to drastically reduce the APK size and improve boot speed.
- Integrated resource optimization allows you to eliminate unused assets by analyzing the code's reference graph together.
If you work in Android development, you know that battling file size is a constant struggle. It's not just that a large app is annoying; it directly impacts abandonment rates . If users see that the download takes forever or that they're running out of space on their phone, they'll most likely abandon your app before even installing it.
To solve this headache, we have R8, the modern successor to ProGuard. This tool doesn't just trim the code; it restructures the application to be as efficient as possible, targeting binary size, RAM consumption, and execution speed, thus preventing dreaded interface crashes.
How exactly does the R8 engine work?

R8 is not a simple line eraser, but a sophisticated process that operates in several stages. The first is tree shaking, or code reduction , where the optimizer traces the entry points defined in the manifest and removes anything that is not accessible, including pieces of external libraries that come loaded with functions you'll never use.
Next, logical optimizations come into play. Here, R8 performs veritable feats of engineering by rewriting code. For example, it uses method inlining to copy the body of a function to where it's invoked, eliminating the load of the call. It also applies class merging , combining similar interfaces and classes so the system uses less memory and the app starts up faster.
Finally, obfuscation arrives. Although many believe it's for protecting secrets, its primary purpose is saving space. By replacing very long names with short identifiers like abc , the size of the DEX file and symbol tables is significantly reduced, resulting in less strain on the device's memory.
Technical configuration and deployment
Depending on the version of the Android Gradle Plugin (AGP) you use, the setup process varies. In the most recent versions (9.3 and later), the process is very simple thanks to a new DSL where you only need to set `enable = true` in the `optimization` block of the release build. Keep rules are now managed in `.keep` files within the variants folder.
For those using older versions, the solution is to use legacy DSL, enabling `isMinifyEnabled` and `isShrinkResources` . It's vital that these options are only enabled in the release version, as optimizing code significantly increases compilation time and makes debugging a nightmare because the code becomes unreadable.
Taking resource reduction to the limit

Starting with AGP 8.12, optimized resource reduction has arrived. Previously, code and assets were analyzed separately, leaving behind mutually referencing junk. Now, R8 treats resources as part of the program's reference graph , allowing for much more aggressive deletion of inaccessible images or layouts.
To get the most out of this, it's recommended to enable full mode by removing the `android.enableR8.fullMode=false` restriction in your `gradle.properties` file. This unlocks deeper optimizations, though it requires you to be more precise with your preservation rules to avoid deleting code you use through reflection.
The workflow in platform and Soong environments
For engineers working at the operating system level with Soong, R8 is the key to reducing system partition space. Setting `obfuscate: true` in the Android.bp files reduces the size of the .vdex and .odex files, lowering memory usage during runtime and preventing paging errors when apps launch.
In this environment, it's recommended to follow a zero-custom-markup philosophy . Instead of filling up external rule files, it's preferable to use direct code annotations like @Keep or @VisibleForTesting . This makes maintenance much simpler and prevents unnecessary symbols from being retained in the production version.
Advanced strategies for weight loss

R8 is a beast, but it doesn't work miracles on its own. To achieve a truly lightweight app, you have to tackle the root of the resource usage. A masterstroke is to switch from PNGs to WebP format or use vector drawables, which are independent of screen density and take up a fraction of the space.
Another key technique is configuring resConfigs in build.gradle to limit supported languages and densities. This prevents the APK from including translations or images that the end user will never see. Furthermore, migrating to Android App Bundles (AAB) is now essential, as Google Play generates a customized APK for each device, delivering only the strictly necessary components.
Configuration analysis and debugging

To avoid going in blind, the R8 Configuration Analyzer is the ideal tool. It provides metrics on the quality of the reduction, optimization, and obfuscation. If the percentages are low, it means you have overly broad retention rules that are limiting the tool's potential.
When errors like ClassNotFoundException arise after optimization, the solution is usually found in the mapping.txt file , which records the relationship between the original and obfuscated code. This file is key to deobfuscating the stack traces and pinpointing the exact error in the original source code.
Common mistakes and how to avoid them
A common mistake is including generic rules like `-keep class * extends Activity`. This is redundant because AAPT2 already parses the manifest and generates exact rules. By using broad wildcards, you force R8 to maintain unnecessary debugging classes or libraries, inflating the final size.
Another common mistake is adding `-dontoptimize` to rule files. This silently overrides the configuration in `android.bp` and disables optimizations across the entire module . The correct approach is to manage optimization behavior from within the `optimize` block of the target build file.
The combination of a full R8 mode, a thorough cleanup of conservation rules, and the adoption of App Bundles ensures that the app is not only smaller, but also starts up faster and consumes less battery, improving user retention and store ratings.
FAQ
What is the main difference between R8 and ProGuard?
R8 is the modern successor that integrates reduction, optimization, and obfuscation in a single step, being more aggressive and faster than ProGuard.
What is R8's full mode and what is it used for?
It's a configuration that applies more powerful optimizations based on stricter assumptions about the use of reflection, further reducing the APK size.
How do I prevent R8 from deleting classes I use through reflection?
Annotations such as @Keep should be used, or specific rules should be defined in the configuration file to tell the optimizer to preserve those elements.
What is the purpose of the mapping.txt file?
This file stores the relationship between the original code names and the obfuscated names, allowing production errors to be translated into source code.
What exactly does optimized resource reduction do?
Analyze the code and assets together to remove resources that are only referenced by code that has already been marked as dead.
Is it advisable to enable R8 in debug versions?
It is not recommended because it considerably increases compilation time and makes it difficult to read errors because the code is obfuscated.
What impact does obfuscation have on app performance?
It reduces the size of the DEX file and symbol tables, which decreases resident memory and speeds up application startup time.
Table of Contents