跪拜 Guibai
← Back to the summary

Google's 2027 DEX Optimization Bar Turns Whole-Package `-keep` Rules into a Release Blocker


theme: smartblue

Google recently published 《Ship smarter keep rules》 as an example, and it's a fairly common issue in Android development: SDK authors, to avoid reflection and JNI crashes in Release builds, often casually add a -keep rule for the entire package.

Google specifically mentioned it because Google Play now requires new DEX optimization thresholds, starting February 2027:

Google Play type Uncompressed DEX size in AAB Shrinking / Optimization / Obfuscation
Regular App Reaching 10 MB All three at least 25%
Game Reaching 50 MB All three at least 25%

Play Console should already show the relevant metrics. The official position is roughly: from now on, if you don't meet the standards, it will affect Google Play visibility and publishing.

How does one consumer keep rule affect the whole app?

Say you develop a video SDK with an optional decoder. At runtime, it reads a class name from configuration and creates an instance via Class.forName(). Because R8 might not see this dynamic call at compile time, it could remove the class or constructor. Many SDKs handle this by putting in their consumer rules:

-keep class com.example.media.** { *; }

This looks convenient, and I've done it myself, but rules pointed to by consumerProguardFiles are packaged into the AAR, and when a downstream app builds Release, they are automatically merged into the R8 configuration.

The problem is that it doesn't just protect the SDK's own compilation result. For example, if this app only ever reflects to create OptionalDecoder, then for this simplified scenario, you only need to protect its class name and the invoked no-arg constructor:

-keep class com.example.media.OptionalDecoder {
	public <init>();
}

That way R8 keeps the dynamic entry point while still analyzing other classes, methods, and fields for deletion, renaming, inlining, or merging. The original package-level rule directly blocks these optimization opportunities, and it can even affect other SDKs:

The official example is a library that adds a full-package -keep for com.google.android.gms.**, in which case even if GMS itself provides finer rules, they get overridden by this broader rule.

Moreover, even if the SDK already obfuscated and shrunk the AAR before release, it's best not to keep the entire package in consumer rules, because only when a downstream app builds does R8 know which SDK code the app actually ends up using.

How to find which rule is blocking R8

We've covered this before: the R8 Configuration Analyzer. Besides displaying a keep file, it also brings together the three dimensions of shrinking, optimization, and obfuscation, and how many classes, fields, and methods each rule is retaining:

For example, the following case: a single -keep class org.bouncycastle.** { *; } retains 55268 code items, accounting for 18.0% of the sample, and at the same time limits shrinking, optimization, and obfuscation for that batch of code:

More interestingly, it can also identify subsumed rules. For instance:

You've written a fine-grained rule for org.apache.xerces.**, but another place has a full-package rule for org.apache.**. The fine-grained configuration then can't actually narrow the scope. In other words, modifying only the fine rule is useless; you must find which broader rule is coming from the app or which AAR.

When troubleshooting, you can work backward from the final artifact:

# Android CLI: view AAB's DEX size and final optimization ratios for all three dimensions
android analyze app-release.aab

# Android CLI: inspect an SDK AAR's built-in rules and retained code items
android analyze --analysis=optimization -f json library.aar

# AGP 9.3+: generate a standalone R8 keep-rule impact report without a full build
./gradlew :app:analyzeReleaseR8Config

The last Gradle task requires AGP 9.3+. If you're still on AGP 9.2, you can first use android analyze to check the AAB/AAR, or temporarily swap in a newer R8 per the official docs to produce a Configuration Analyzer report.

So this ultimately looks like a semi-mandatory item; at some point you won't get away without adopting R8 Full and optimizing your rules.