Libraries 6 min read

Consumer Keep Rules for Library Authors: Unblocking R8 and DEX Code Optimization in Your SDK

The Android Developers Blog published a guide on Nov 17, 2026 aimed at library providers: how to ship consumer keep rules that do not block R8 from optimizing the apps that depend on you. The reason it matters now is that Google Play introduced DEX code optimization thresholds for apps and games, and those metrics reflect the combined effect of the app's rules and every rule bundled in its libraries. If your SDK ships a package-wide keep, every customer pays for it. Everything below comes from the official post and the docs it links, fetched today.

Consumer rules
bundled in AARs and JARs, applied during whole-app R8
Entry points
keep only what you reflect on; R8 follows the graph
analyze
Android CLI command to audit an AAR or JAR

1. Why your rules are everyone's problem

Consumer keep rules tell R8 which parts of your library it must not optimize during the app's compilation. They exist so that reflection targets and native interfaces survive without the app developer hunting for them. The trade-off is that broad rules preserve unused code, which the post links to slower startup, larger downloads, more memory use and higher ANR rates. Worse, performance-minded apps may write custom rules that replace the ones bundled with your library, which discourages them from adopting your future releases.

2. Prefer codegen over reflection

Runtime reflection hides code usage from R8, so it forces keep rules. Code generation happens at build time, so R8 sees the real call graph and needs minimal rules. The post names serialization and dependency injection as good fits, with Hilt as an example of a codegen-based library. Where reflection is unavoidable, such as optional dependencies, pair it with narrow rules protecting only the required entry points.

// Optional dependency resolved reflectively: only this one entry point is reached
fun tryCreateAdapter(): Adapter? = try {
    Class.forName("com.example.sdk.optional.AnalyticsAdapter")
        .getDeclaredConstructor()
        .newInstance() as Adapter
} catch (e: ClassNotFoundException) {
    null
}
# consumer-rules.pro: narrow, names exactly what the reflection above touches
-keep class com.example.sdk.optional.AnalyticsAdapter {
    <init>();
}

# Avoid: forces everything under the package to survive in every app
# -keep class com.example.sdk.** { *; }

The rule above is illustrative, written to show the pattern the guide describes: name the entry point and let R8 build the reachable graph from there. It is not copied from the post.

3. Do not blanket-keep third-party packages

The post calls out this common pattern:

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

If an app also uses another GMS library and has its own rule to limit what is kept, your broad rule supersedes it. It can also subsume the carefully scoped consumer rules that library already ships. When calling into external libraries, keep only the specific elements you access reflectively.

4. Conditional rules for ad mediation adapters

For ad libraries loaded through mediation adapters, the guide recommends conditional keep rules so features and formats are preserved only when the app includes the matching adapter. Everything else stays shrinkable. The alternative it offers is to annotate reflective calls in source with Jetpack Keep annotations (currently alpha) and have the build generate the conditional consumer rules, including for optional add-on modules.

# Illustrative shape of a conditional rule: keep the format loader
# only if the adapter class is present in the final app
-if class com.example.mediation.BannerAdapter
-keep class com.example.sdk.banner.BannerLoader { <init>(); }

5. Native code and JNI

If your SDK talks to native code, as in games and photo or video editors, provide rules for every class used through JNI. Game engines such as Unity and Unreal compile to native code and call into the Android framework over JNI, so these classes must be protected explicitly.

6. Ship rules inside the artifact, not in docs

Bundled consumer rules have been supported in AARs and JARs for years. Documenting rules for customers to paste adds setup friction, and every refactor or rename means remembering to update the docs. Apps that upgrade across several versions of your library accumulate legacy rules that are no longer needed, which is more bloat. This also holds if you pre-optimize or obfuscate your own build: because your rules apply during the app's compile step, R8 can still optimize your code further based on what the app actually uses.

7. Do not keep names just to read stack traces

It is tempting to use keepnames so obfuscated traces from customers are readable. That permits shrinking but blocks renaming in the consuming app, which is exactly what costs optimization. You do not need it: Firebase Crashlytics, the Play Console and Logcat in Android Studio deobfuscate for app developers, and uploading your library's mapping file to the SDK Console gives you deobfuscated traces for your own SDK.

8. Testing narrow rules before you ship them

Library audit checklist

  1. Grep your consumer rules for ** wildcards and { *; } bodies, and justify each one.
  2. Replace reflection with codegen where the use case allows (serialization, DI).
  3. For the reflection that remains, keep only the entry point you instantiate or call.
  4. Remove any package-wide keeps on third-party dependencies.
  5. Make adapter-specific rules conditional, or annotate with Jetpack Keep.
  6. Delete rules-in-documentation and ship them as consumer rules.
  7. Run the Configuration Analyzer and analyze on every release candidate.

For app developers: if your R8 Configuration Analyzer report shows blocked optimization from a dependency, the culprit may be a consumer rule you never wrote. Open an issue with the library maintainer pointing at the specific rule rather than overriding it locally, since local overrides can make you reluctant to upgrade.

The app-side half of this story, phased R8 rollout with measured ANR, size and startup wins, is in the Grab case study.

Source: "Ship smarter keep rules: How library providers can unblock app-level DEX code optimization" by Breana Tate, Android Developers Blog, Nov 17, 2026, and the linked R8 library optimization docs, retrieved Nov 18, 2026.

Comments 0

No comments yet. Be the first to leave one!

Leave a comment