Mosmoxmosmox

Mosmox journal

In focus — R8.

A selection of publications grouped under the R8 tag.

R8 and Keep Rules: Find the Library Slowing Down Your Android App and Meet Google Play Requirements

R8 and Keep Rules: Find the Library Slowing Down Your Android App and Meet Google Play Requirements

Starting in February 2027, Google Play will require Android apps to reach a minimum level of code optimization: at least 25% for shrinking, optimization and obfuscation. Most teams will find that the problem is not their own code but one or two libraries that bring broad keep rules and switch R8 off for a large part of the app. On November 17, 2026 the Android Developer Relations team published a guide for library authors on writing narrow consumer keep rules. Here is what it means for an app team: how to measure the problem, find the culprit, and what to do when the culprit is someone else's SDK.

What Google Play requires

In August 2026 Google announced new bad behavior thresholds for memory, bitmaps and DEX code optimization. The code requirement is detailed in the Google Play technical requirements:

MetricApps (DEX > 10 MB)Games (DEX > 50 MB)
Shrinking25%25%
Optimization25%25%
Obfuscation25%25%

A few important details:

  • The requirement applies from February 2027 across all form factors. Apps and games that miss the thresholds may see reduced visibility and publishing capabilities on Google Play.
  • It only applies to apps with a non-negligible amount of DEX code: more than 10 MB for apps and more than 50 MB for games.
  • The tool is up to you. R8 or any other shrinker is fine as long as you reach the thresholds.
  • You can check your numbers today. For every uploaded app bundle, Play Console shows DEX size and optimization percentages in the app bundle explorer, and Android vitals warns about limited DEX optimization.
  • Your local build may differ from what Play Console sees. Google notes that CI/CD and enterprise pipelines can produce slightly different numbers, so treat the console as the source of truth.

The key point of the November guide: the metrics in Play Console reflect the combined effect of keep rules from the app and all its dependencies. Your own configuration can be perfect and the score can still be low because of a single SDK.

What consumer keep rules are and why they break optimization

R8 removes unused code, optimizes the rest (method inlining, class merging) and renames classes, fields and methods to short names. Keep rules tell R8 what to leave alone, usually code accessed through reflection or JNI, because R8 cannot see those calls at compile time.

Consumer keep rules are rules a library bundles in its AAR or JAR. When your app is built, they are merged with your own rules. The catch is that keep rules are additive: you cannot cancel a library rule with one of yours. If a library ships something like

-keep public class com.google.android.gms.** { *; }

R8 keeps the entire package untouched, even if another GMS library carefully described what it really needs. The broad rule subsumes the narrow ones. According to Google, the result is a bigger download, slower startup, higher memory use and more ANRs.

Library authors do not do this out of malice: nobody wants their SDK to crash with ClassNotFoundException in a customer's release build. But as Google points out, broad rules are not a long-term answer to crashes, because they severely limit optimization of the whole app.

Step 1. Measure your scores

Start with two sources.

  • Play Console. Open the app bundle explorer for your latest release and check DEX size and the three percentages. If your DEX is under 10 MB, the requirement does not apply to you yet, but optimization still affects startup and memory.
  • R8 Configuration Analyzer. It shows the same three scores (shrinking, optimization, obfuscation) locally and, crucially, breaks them down by individual keep rule. It needs AGP 9.3.0 or R8 9.3.7-dev or newer. The fastest way to get a report without building a full APK or bundle is a dedicated Gradle task: ./gradlew :app:analyzeReleaseR8Config. The HTML report lands in app/build/reports/r8/r8-config-analyzer-release.html. A regular release build also generates it automatically in build/outputs/mapping/release/configanalyzer.html.

For AGP 9.2 and earlier, the documentation suggests temporarily adding a recent R8 as a classpath dependency in settings.gradle.kts and running the build with the system property -Dcom.android.tools.r8.dumpkeepradiushtmltodirectory=<directory>. The analyzer is also available as an Android skill for AI agents: android skills add r8-analyzer.

Each score is the share of code available for that transformation. For example, an optimization score of 66% means R8 can optimize only 66% of classes, fields and methods, and keep rules block the rest.

Step 2. Find the rules that block the most

In the Keep Rule Analysis section, the report shows for every rule how many classes, fields and methods it excludes from optimization and which optimizations it prevents. It also shows where each rule comes from: your code, a library or the default AGP rules. Leave the default AGP rules alone; they are written to allow maximum optimization.

The workflow:

  1. Sort rules by the amount of code they block.
  2. For the rules at the top, check whether all the kept code is really accessed through reflection or JNI.
  3. Narrow the rule to the specific classes and members that reflection touches.
  4. Run the tests that cover that code and rerun the analyzer.

Also look for subsumed rules: if you have -keep class com.example.package.** { *; } next to -keep class com.example.package.MyClass, the first makes the second redundant and blocks far more than needed.

For libraries there is one more tool, the android analyze command in the Android CLI. For AAR and JAR files it reports the keep ratio, kept versus total classes, methods and fields, used and unused package-wide rules with their impact radius, and warnings about anti-patterns. For AAB and APK files it reports the AGP version, DEX size and R8 percentages. The --format=json flag gives machine-readable output for CI.

Step 3. When a third-party library is to blame

The Android documentation lays out a clear sequence.

  • Update the library. Newer versions often ship narrower consumer keep rules.
  • Measure the potential gain locally. Copy the library's rules into your own file, filter out the bundled rules and remove the broad ones from your copy. In AGP 8.4 and newer the filter looks like this: optimization.keepRules { it.ignoreFrom("com.somelibrary:somelibrary") } (for AGP 7.3–8.3 use ignoreExternalDependencies). Rerun the analyzer and compare the scores.
  • Report it to the maintainer. Include the numbers from the report in the issue: how many classes and methods their wildcard rule keeps. Check for an existing issue first.

An important caveat: Google does not recommend filtering library rules in production builds. If the next SDK version starts using reflection somewhere new, your manual copy of the rules will not know, and the app will crash in release even though the build succeeds. Filtering is a measurement tool and an argument for the conversation with the maintainer, not a permanent fix.

If you build a game on Unity or Unreal, the engine manages the R8 configuration and plugin keep rules. Android has separate guides for both.

If you ship an SDK yourself

Many product teams have internal libraries: a design system, an analytics module, an SDK for partners. For them the November guide gives concrete rules.

  • Codegen over reflection. Reflection hides code usage from R8, which then needs keep rules. R8 can see and optimize generated code. Typical cases are serialization and dependency injection; Hilt, for example, uses code generation.
  • If reflection is unavoidable, keep only entry points. It is enough to specify the class or method accessed dynamically, and R8 builds the rest of the graph. This also applies to libraries already optimized on their side: during the app build R8 can optimize them again based on what is actually used.
  • Conditional rules for optional features. The -if <condition> <keep rule> syntax keeps code only when the condition holds. Google recommends it for ad mediation adapters, so features are kept only if the app includes the matching adapter. A classic example from the documentation is a Gson rule that keeps only classes with @SerializedName fields.
  • Jetpack Keep annotations. Instead of a separate rules file you annotate the function that performs reflection, for example with @UsesReflectionToConstruct. The Gradle plugin generates a rule, and it is conditional: the reflection target is kept only if the calling code is kept. Lint flags unannotated reflective calls. The library is still in alpha, so new versions may change the generated rules, and release-build tests should catch that.
  • Ship rules in the bundle, not in the docs. If customers have to copy rules from a README, they will accumulate stale rules with every SDK update.
  • Do not use -keepnames just for readable stack traces. Crashlytics, Play Console and Logcat in Android Studio deobfuscate stack traces using the mapping file. SDK authors can upload their mapping to the Google Play SDK Console.

Before a library release, Google suggests testing the rules in a sample app with R8 enabled, running the analyzer on it and comparing android analyze results across versions to catch regressions.

Common traps

  • You narrowed the rules and got a crash only in release. This is the main risk. Verify every keep rule change on a release build with R8 and walk through critical flows, ideally with automated tests.
  • You only look at local numbers. The final score is the one in Play Console, and it can differ slightly from your local build.
  • You wait until February. SDK updates, back-and-forth with vendors and stabilizing after narrowing rules take weeks. Start the audit now.
  • You think it is only about size. Google directly links DEX optimization to faster startup, lower memory use and fewer ANRs, metrics that already affect visibility on Google Play.

One-sprint checklist

  • Check DEX size and the three optimization percentages for your latest release in Play Console.
  • Upgrade AGP to 9.3 or temporarily add a recent R8 and generate an R8 Configuration Analyzer report.
  • List the top 10 keep rules by blocked code and note the source of each: your code or a library.
  • Narrow your own broad rules, remove subsumed ones and verify everything on a release build.
  • Update the libraries with the worst rules; for the rest, measure the gain with local filtering and contact the maintainers.
  • Add android analyze --format=json or analyzer report generation to CI to catch regressions in every PR.
  • For internal SDKs, move to codegen or Jetpack Keep annotations and ship rules inside the AAR.

Conclusion

Formally the Google Play requirement applies to the app, but you can only meet it together with your whole dependency ecosystem. The good news is that the tools are already here. R8 Configuration Analyzer shows exactly which rule from which library blocks optimization, the Android CLI provides the same data for CI, and Jetpack Keep removes the manual upkeep of rules. There are a few months left before February 2027, just enough to audit, update SDKs and stabilize the release build without rushing.

Journal

Ideas and products