Log date · Mon
Does Android R8 obfuscation protect your code?
Published on · Updated
R8 shortens names but does not automatically hide constants. We compare two keep rules and show how to check your release build.
Does R8 protect your code? The result first
R8 can shrink and optimize code and shorten names, but name obfuscation is not encryption. With a narrow keep rule, our sample renamed one class, one field and one method; the string DEMO-PLAN and numbers 120 and 17 remained in the bytecode. Keeping the whole class preserved all three names. Both builds printed DEMO-PLAN120.
Android recommends enabling optimization for the final build tested before publication. [1] Start with that representative release as the baseline before judging what any additional measure changes.
| Question | Result |
|---|---|
| What changed with the narrow rule? | Pricing → a.a; basePrice and quote → a. |
| What did the broad rule change? | Pricing, basePrice and quote kept their names. |
| Did literals remain? | Both retained DEMO-PLAN, 120 and 17. |
| Did behavior match? | Both printed DEMO-PLAN120 with no arguments. |
Sample and scope
We wrote two Java classes. Main passes the argument count to Pricing, which adds 17 per argument to a base price of 120 and returns a string prefixed with DEMO-PLAN. We compiled with javac 17.0.19 and used R8 8.3.37 from Google Maven to produce classfiles. Both variants use -dontoptimize to isolate names and bytecode; this is not a measurement of Android Gradle Plugin DEX, resources or a complete release package.
public class Pricing {
private int basePrice = 120;
public String quote(int quantity) {
return "DEMO-PLAN" + (basePrice + quantity * 17);
}
}The narrow rule keeps only the command-line entry point; the broad rule also keeps every member of Pricing. Both preserve the entry point so we can run the results in the same way. Disabling optimization makes this a controlled comparison, so it cannot tell us the size or performance of a normal release build.
-keep public class demo.Main { public static void main(java.lang.String[]); }
-dontoptimize
# Broad variant adds:
-keep class demo.Pricing { *; }Names changed; constants remained
The R8 mapping records the old and new names. Under the narrow rule, demo.Pricing became a.a, while the field basePrice and method quote both became a. They are different kinds of member, so sharing a name does not prevent this example from running. The entry point demo.Main remained.
demo.Pricing -> a.a:
# {"id":"sourceFile","fileName":"Pricing.java"}
int basePrice -> a
1:1:void <init>():0:0 -> <init>
1:1:java.lang.String quote(int):0:0 -> aWe then inspected the generated class with javap. The string and both numbers are still directly readable. The -c option shows instructions and -private includes private members. [3] The excerpts below come from the same output: numbers are instruction operands and the string appears in the constant reference comment.
5: bipush 120
7: putfield #15 // Field a:I
7: ldc #21 // String DEMO-PLAN
9: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
12: aload_0
13: getfield #15 // Field a:I
16: iload_1
17: bipush 17
19: imulShortening names removes some semantic clues, but does not automatically conceal business values or strings. Nor does this result mean every constant will survive unchanged: optimization, reachability and rules affect the final output. Do not put a shared privileged credential in a client and rely on name obfuscation to protect it.
Why keep rules change the result
The broad rule matches the whole Pricing class and all its members. Its javap output still shows private int basePrice and public String quote(int), and the mapping retains demo.Pricing. Both artifacts printed DEMO-PLAN120. That confirms the no-argument path we ran, not every application behavior.
demo.Pricing -> demo.Pricing:
# {"id":"sourceFile","fileName":"Pricing.java"}
DEMO-PLAN120
Compiled from "SourceFile"
public class demo.Pricing {
private int basePrice;Keep rules are a compatibility tool. Reflection, serialization or a dynamic contract between a library and app may need specific names or members retained; a broad rule also limits shrinking and optimization. Android recommends precise rules and documents how keep options and modifiers differ in removal, renaming and optimization. [2] Do not permanently keep an entire package merely to make a build pass. Identify the dynamic dependency and test a narrower rule.
The mapping is useful for diagnosis: it connects short names in a release back to originals. Associate it with the exact build, restrict access and retain it under your team’s policy. Otherwise a crash may be retraced against the wrong release and yield a misleading stack trace.
Recommendations by case
These are engineering recommendations, not benefits measured by our experiment. Establish compatibility and trust boundaries first, then measure a representative release.
| Situation | Recommendation |
|---|---|
| Release baseline | Enable the R8 configuration appropriate to the project; retain versions, rules and mapping. |
| Reflection or serialization fails | Find the dynamic access, keep only what is necessary and add a regression test. |
| Evaluating an extra transformation | Compare against the same R8 release configuration for behavior, performance and crashes. |
| A client calls a service | Keep privileged keys server-side and use scoped, revocable client credentials. |
Hold dependencies, build variant and device conditions constant when comparing. Record installation, startup, critical flows, package size and crash diagnosis, then attribute differences separately. Comparing an unoptimized debug build with a release that received several transformations mixes build mode, R8 and the added processing into one result.
Before release, exercise the diagnostic path as well. Trigger a known exception in a test environment, confirm that the captured stack trace is retraced with the mapping for that exact build, and check that it points to the expected method. Archive the build identifier with its mapping so a later release does not overwrite it. If another processing step is added, repeat this check on the processed artifact: a successful build alone does not show that crash reports will remain usable.
Run the check on your own app
First identify your AGP version and release configuration. The official guide gives optimization DSLs by version and notes that optimization makes debugging harder. [1] Run your own release build from the project root and inspect the mapping generated by that build. The task and path below are common examples; substitute your project’s actual variant and output path.
./gradlew :app:assembleRelease
# Substitute your module and variant output path.
less app/build/outputs/mapping/release/mapping.txtCheck classes you expect to keep or rename in the mapping, review the combined rules and exercise critical flows on devices, including reflection, serialization and error recovery. Do not publish the complete mapping or diagnostic output containing sensitive values in public logs. A passing path does not establish that every other path works.
Finally keep a reproducible record: revision, AGP and dependency versions, build task, rules, artifact identifier, test devices and results. When rules or processing change, repeat under the same conditions and retain the matching mapping so real crashes can be traced back to source.
Your checklist
- Record AGP, dependencies and R8 configuration for the release.
- Review keep rules and retain only what dynamic access needs.
- Save the matching mapping for each release with restricted access.
- Regression-test critical flows and crash diagnosis on devices.
- Evaluate extra processing against the same release baseline.
Primary sources
- Android: Enable app optimization with R8 · Checked 2026-11-11
- Android: Add keep rules · Checked 2026-11-11
- Oracle: javap command · Checked 2026-11-11