Add R8 -checkdiscard verification for disabled-flag dead code#295
Open
kirich1409 wants to merge 1 commit into
Open
Add R8 -checkdiscard verification for disabled-flag dead code#295kirich1409 wants to merge 1 commit into
kirich1409 wants to merge 1 commit into
Conversation
Local boolean flags (default = false) can now declare discard(...) class specs. The plugin emits R8 -checkdiscard rules that *verify* the flag-guarded code was actually stripped from the optimized release binary — the missing verification half of the -assumevalues dead-code-elimination story. Unit tests see the pre-R8 classpath and androidTest runs without minify, so without -checkdiscard nothing checks the final DEX; a forgotten DI reference, manifest entry, reflection, or over-broad -keep that keeps a "disabled" feature alive is now caught at build time (Discard checks failed) instead of shipping. - discard(classSpec) on FlagSpec; rejected on non-boolean, default=true, and remote flags (a value never pinned at build time can't be guaranteed). - CheckDiscardRulesGenerator + generateFeaturedCheckDiscardRules task, wired into R8 like -assumevalues: app proguardFiles and library/KMP consumer rules. - Real-R8 shrinker test proving the check passes when code is eliminated and fails the build when a checked class survives. - Unit + registration + DSL-validation tests; docs in both CLAUDE.md files. https://claude.ai/code/session_01HqnfagV6KswrzeqKLywpaS
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The plugin already causes dead-code elimination with generated
-assumevaluesrules, but nothing verifies the disabled-flag code actually left the final binary. As noted in the discussion that prompted this: unit tests see the full pre-R8 classpath,androidTestis assembled withoutminify, so nothing checks the final DEX. R8's built-in-checkdiscarddirective is exactly the missing piece — it lists feature classes and fails the build if they survive optimization (Discard checks failed+ the retention path; diagnose with-whyareyoukeeping).Paired with
-assumevaluesthis closes the loop: the flag is pinned tofalse, its branch and classes are stripped, and-checkdiscardconfirms their absence. A forgotten DI reference, manifest entry, reflection, or over-broad-keepthat keeps a "disabled" feature alive is now caught at build time instead of shipping.What
A new opt-in
discard(...)on the flag DSL plus a dedicated task, wired into R8 exactly like the existing-assumevaluesmachinery.featured { localFlags { boolean("new_checkout", default = false) { discard("com.example.checkout.newflow.**") } } }generates:
Surface
FlagSpec.discard(classSpec)— repeatable; rejected on non-boolean flags, ondefault = trueflags, and on remote flags (a value never pinned at build time can't be guaranteed). Validation fails fast in the DSL with a precise message.CheckDiscardRulesGenerator— pure, testable rule generator (header + one-checkdiscardline per target).generateFeaturedCheckDiscardRulestask →build/featured/proguard-featured-checkdiscard.pro. Reads discard targets straight from the DSL (config-cache safe, participates in the@Inputfingerprint). The output is blank (a harmless no-op) unless a flag opts in.-assumevalues: into the app's own R8 (proguardFiles) for application modules, and as consumer ProGuard rules for library / KMP-library modules so the check runs in the consuming app's R8 — wherever the matching-assumevaluesrule applies.Tests
CheckDiscardRulesGeneratorTest— output shape, trimming, blank-skipping, one rule per target.FlagDiscardDslTest— recording, descriptor serialization, and all three validation rejections.GenerateCheckDiscardRulesTaskRegistrationTest— task registration / group / wiring.R8CheckDiscardTest(real R8, infeatured-shrinker-tests) — proves the check passes when the flag-guarded class is eliminated and fails the build (CompilationFailedException) when a checked class survives.Docs
Updated root
CLAUDE.mdandfeatured-gradle-plugin/CLAUDE.md(DSL example, task table,discard(...)semantics and wiring notes).Notes / scope
-checkdiscarddoes not verify resource shrinking (call out in docs; resources still needaapt2/apkanalyzerinspection).-assumevaluesgeneration and wiring are untouched; the empty check-discard file is the same already-handled case as an empty-assumevaluesfile for flag-less modules.Targets
developper the project's branching convention.Generated by Claude Code