Skip to content
Theme
OnDevice Tools

AndroidAndroid OS

DEX code optimization: Google urges narrower Android keep rules

Google's Android Developers Blog has told library providers to ship tightly scoped keep rules so R8 can optimize apps and meet new Google Play thresholds. Broad rules can slow startup and increase app size and memory use.

Illustration of Android code being trimmed by the R8 optimizer to shrink an app

Key takeaways

  • Google's Android Developers Blog wants library providers to ship narrow keep rules so R8 can optimize apps.
  • Google Play has new DEX code optimization thresholds aimed at faster startup and lower memory use.
  • Broad or package-wide keep rules preserve unused code, slowing startup and increasing app size and memory use.
  • Google advises avoiding reflection with codegen where possible and checking rules with the R8 Configuration Analyzer.
In this article
  1. What the guidance covers
  2. Why broad keep rules hurt performance
  3. Steps Google recommends for library makers
  4. Reflection, codegen and native code
  5. Why it matters for the wider ecosystem

What the guidance covers

R8 is Android's app optimizer. It removes code an app does not use, then optimizes and obfuscates the rest. Obfuscating means renaming code to make it smaller and harder to read. According to the Android Developers Blog, the new DEX code optimization thresholds from Google Play aim to improve how quickly apps start and cut memory use. They are also meant to stop apps from taking more memory than they need.

The blog explains that R8 can only work within the rules it is given. Some of those rules, called consumer keep rules, are bundled inside the libraries an app relies on. They tell R8 which parts of a library must be left untouched during app-level compilation. This keeps features such as reflection working.

Why broad keep rules hurt performance

Keep rules exist to prevent crashes. Reflection lets software inspect and call code by name while it runs. Removing that code by mistake can break a library. The trouble starts when providers ship rules that are too wide.

The Android Developers Blog says that broad rules, such as those covering a whole package, preserve code that is never used. That limits how much R8 can optimize the whole app. The result is slower startup, larger downloads, higher memory use, and more ANR events, when Android flags an app as unresponsive.

Steps Google recommends for library makers

The blog lists practical steps. It suggests testing changes in a sample app with R8 switched on, then checking that important user journeys still work. It also recommends the R8 Configuration Analyzer, a tool that shows how a library's keep rules affect the apps that use it.

Providers can run the analyze command from the Android command line tool to measure the optimization of their AAR or JAR file. These are the packaged formats for Android and Java libraries. The check shows which classes, methods and fields are kept, and comparing the figures across releases helps catch regressions. Google also points to the Google Play SDK Console to track library version usage and crashes.

Reflection, codegen and native code

Illustration: code, Android logo, Google logo, Google Play logo and the labels Android, Dex Code Optimization, Google Play

One of the blog's main tips is to avoid reflection where possible and use codegen instead. Codegen, short for code generation, produces code during the build. That lets R8 see how the code is used and optimize it safely with fewer keep rules. The blog names serialization and dependency injection as good fits, and cites Hilt as a library that uses codegen.

Reflection is still needed in some cases, such as optional dependencies. There, Google advises narrow keep rules that protect only the entry points a library actually uses, after which R8 works out the rest. Some SDKs, or software development kits, touch native code. For these, the blog says to provide rules for every class that uses JNI, the bridge between native code and Android. Many games built on engines such as Unity or Unreal fall into this group. It also notes that Jetpack Keep annotations, which are now in alpha, can help here by generating keep rules from annotated reflection usage at build time.

Why it matters for the wider ecosystem

The guidance treats optimization as a shared task across Android. With DEX code optimization insights now added to the Play Console, the figures developers see reflect the combined effect of rules from both their app and its libraries.

The blog warns that broad rules can backfire. Performance-focused apps may write their own rules to override a library's, which can put them off adopting future updates. Google also cautions that a package-wide rule can override the narrower rules shipped by other libraries, including Google Mobile Services. That adds to app bloat. For library providers, the advice is to ship tight, well-scoped rules and to consult the official R8 documentation and optimization guidance.

Sources

  1. Ship smarter keep rules: How library providers can unblock app-level DEX code optimizationAndroid Developers Blog

Tags

Spotted a mistake? See our corrections policy. Read how News by OnDevice works in our editorial policy.