Skip to content

[Dont merge] Add R8 regression & library shrinking gate with android analyze - #218

Closed
AjeshRPai wants to merge 3 commits into
mainfrom
r8-regression-gate-demo
Closed

AjeshRPai wants to merge 3 commits into
mainfrom
r8-regression-gate-demo

Conversation

@AjeshRPai

@AjeshRPai AjeshRPai commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

[Closing - check #219 for the correct implementation]

R8 Regression & Library Shrinking Gate (android analyze)

This directory contains check_r8_regression.py, a zero-dependency Python presubmit and post-build gate that uses the Android CLI's android analyze command to prevent R8 shrinking, optimization, obfuscation, and DEX size regressions across both Application Bundles (.aab) and Library Archives (.aar / .jar).


1. How It Works

The script invokes the Android CLI analyzer with JSON output:

android analyze --analysis=optimization --format=json <artifact>

App Bundle Checks (.aab)

Compares the release App Bundle against the committed r8-baseline.json:

  • Shrinking Score (shrinkingRatio): Fails if the score drops by more than --max-score-drop (default: 2.0 percentage points).
  • Optimization Score (optimizationRatio): Fails if the score drops by more than --max-score-drop.
  • Obfuscation Score (obfuscationRatio): Fails if the score drops by more than --max-score-drop.
  • Uncompressed DEX Size (uncompressedDexSizeBytes): Fails if DEX size grows by more than --max-dex-growth-pct (default: 5.0%) or --max-dex-growth-kb (default: 500 KB).

Library Shrinking Percentage Checks (.aar / .jar)

When passed --library <path> or --scan-libraries, android analyze compiles the library in isolation with D8 and R8 to evaluate its bundled consumer keep rules (consumer-rules.pro / proguard.txt):

  • Library DEX Shrinking %: Computed as (1 - r8DexSizeBytes / d8DexSizeBytes) * 100 (100% - Keep Ratio). Measures how much of the library R8 can strip when the library is unused by the host app. Fails if below --min-lib-shrinking-pct (default: 90.0%).
  • Class / Method / Field Shrinking %: Reports (total - kept) / total * 100 for classes, methods, and fields.
  • Package-Wide Consumer Rules: Counts broad usedPackageWideKeepRules and unusedPackageWideKeepRules (e.g., -keep class org.bouncycastle.** { *; }). Fails when --fail-on-package-wide-rules is enabled.
  • Blocked Consumer Keep Rules: Fails if any blockedKeepRules are present.

2. Local Usage

Check App Bundle Against Baseline

# Default: checks app/build/outputs/bundle/release/app-release.aab against r8-baseline.json
./scripts/check_r8_regression.py

# Build :app:bundleRelease first, then run the gate
./scripts/check_r8_regression.py --build

Check Library Shrinking Percentage Only (--skip-app)

# Check specific module or third-party AARs/JARs
./scripts/check_r8_regression.py --skip-app \
  --library watchface/build/outputs/aar/watchface-release.aar \
  --library core/network/build/outputs/aar/network-release.aar \
  --library data/build/outputs/aar/data-release.aar \
  --min-lib-shrinking-pct 90.0 \
  --fail-on-package-wide-rules

# Auto-discover and check all *-release.aar modules in the repo
./scripts/check_r8_regression.py --skip-app --scan-libraries --fail-on-package-wide-rules

Run Combined App Bundle + Library Shrinking Gate

./scripts/check_r8_regression.py \
  --scan-libraries \
  --min-lib-shrinking-pct 90.0 \
  --fail-on-package-wide-rules \
  --markdown-summary .gemini/r8_summary.md

Record / Update the Baseline

./scripts/check_r8_regression.py --save-baseline

3. GitHub Actions Workflow (.github/workflows/r8_regression_check.yml)

The GitHub Actions workflow automates this check on pull requests and workflow_dispatch:

  1. Triggers: Runs when any *.pro, build.gradle.kts, gradle/libs.versions.toml, gradle.properties, or r8-baseline.json file is modified in a PR, or manually via workflow_dispatch (with optional pr_number, max_score_drop, max_dex_growth_pct, and min_lib_shrinking_pct inputs).
  2. Build & Analyze:
    • Installs the android CLI and adds the r8-analyzer skill (android skills add --agent=gemini --project=. r8-analyzer).
    • Builds the release App Bundle and module AARs (./gradlew :app:bundleRelease assembleRelease).
    • Runs check_r8_regression.py across the release .aab and all module *-release.aar artifacts, writing the Markdown scorecard to both $GITHUB_STEP_SUMMARY and .gemini/r8_summary.md.
  3. AI-Assisted Keep-Rule Diagnosis on Failure:
    • If the gate fails (steps.r8_gate.outputs.passed == 'false'), the workflow invokes google-github-actions/run-gemini-cli with the r8-analyzer skill to inspect .gemini/pr_diff.patch and explain which keep rule or dependency change caused the regression.
  4. PR Comment & Artifact Upload:
    • Posts the combined R8 scorecard and r8-analyzer findings as a comment on the pull request via gh pr comment.
    • Uploads app/build/reports/r8/r8-config-analyzer-release.html as a workflow artifact (r8-config-analyzer-report).
  5. Automatic Baseline Ratification on Merge (main):
    • When a PR passes the gate and merges into main (on: push: branches: ['main']), the workflow automatically runs check_r8_regression.py --save-baseline and commits any updated metrics to r8-baseline.json via stefanzweifel/git-auto-commit-action.

4. Presubmit Hooks Integration

You can enforce this gate automatically before code lands on main using:

Native Git Hooks (pre-commit & pre-push)

Located in scripts/git-hooks/:

  • pre-commit: Triggers when staged files modify *.pro, *.gradle.kts, gradle/libs.versions.toml, or gradle.properties.
    1. Immediately blocks staged diffs that disable R8 Full Mode (android.enableR8.fullMode=false) or add unrestricted package-wide rules (-keep class pkg.** { *; }).
    2. Runs check_r8_regression.py --scan-libraries --fail-on-package-wide-rules (set R8_GATE_BUILD=1 to rebuild :app:bundleRelease first).
  • pre-push: Runs check_r8_regression.py --scan-libraries --fail-on-package-wide-rules before pushing any branch with R8/build changes relative to origin/main.

Enable in your local clone:

git config core.hooksPath scripts/git-hooks

5. CLI Reference

Flag Default Description
--aab <path> app/build/outputs/bundle/release/app-release.aab Path to the release .aab to analyze
--skip-app false Skip the .aab check and only run library (.aar / .jar) checks
--baseline <path> r8-baseline.json Path to the baseline JSON file
--save-baseline false Save current .aab android analyze output to --baseline and exit
--build false Run ./gradlew :app:bundleRelease before analyzing
--max-score-drop <pts> 2.0 Max allowed drop in R8 shrinking/optimization/obfuscation score (% pts)
--max-dex-growth-pct <%> 5.0 Max allowed uncompressed DEX growth (%)
--max-dex-growth-kb <KB> 500.0 Max allowed uncompressed DEX growth (KB)
--library <path> [] Path to a .aar or .jar library to check (repeatable)
--scan-libraries false Auto-discover and analyze all **/build/outputs/aar/*-release.aar files
--min-lib-shrinking-pct <%> 90.0 Minimum required library DEX shrinking percentage (100% - Keep Ratio)
--fail-on-package-wide-rules false Fail if any analyzed library exports package-wide consumer keep rules
--markdown-summary <path> None Write Markdown summary table to <path> (in addition to $GITHUB_STEP_SUMMARY)
@AjeshRPai
AjeshRPai removed the request for review from tiwiz October 23, 2026 16:27
@AjeshRPai AjeshRPai changed the title Add R8 regression & library shrinking gate with android analyze Oct 23, 2026
@AjeshRPai
AjeshRPai removed the request for review from riggaroo October 23, 2026 16:28
@github-actions

Copy link
Copy Markdown

🛡️ R8 Optimization & Shrinking Gate (android analyze)

📦 App Bundle R8 Scores & DEX Size

  • Package: com.android.developers.androidify
  • Compiler: AGP 9.2.1 / R8 9.2.14 (Full Mode: True)
  • Baseline: r8-baseline.json
Metric Baseline Current Delta Threshold Status
Shrinking Score 51.15% 50.36% -0.79% pts max -2.00% pts ✅ PASS
Optimization Score 50.81% 50.02% -0.79% pts max -2.00% pts ✅ PASS
Obfuscation Score 51.11% 50.33% -0.78% pts max -2.00% pts ✅ PASS
Uncompressed DEX Size 18.32 MB 17.90 MB -0.42 MB (-2.3%) max +5.0% / +500KB ✅ PASS

📚 Library Shrinking Percentage (Min Threshold: 90.0%)

Library (.aar / .jar) D8 DEX R8 Kept DEX DEX Shrinking % Classes Shrunk Methods Shrunk Pkg-Wide Rules Status
network-release.aar 64.46 KB 0.00 KB 100.00% 100.0% (49/49) 100.0% (250/250) 0 ✅ PASS
testing-release.aar 29.17 KB 0.00 KB 100.00% 100.0% (17/17) 100.0% (119/119) 0 ✅ PASS
theme-release.aar 210.10 KB 0.00 KB 100.00% 100.0% (51/51) 100.0% (405/405) 0 ✅ PASS
util-release.aar 44.73 KB 0.00 KB 100.00% 100.0% (24/24) 100.0% (111/111) 0 ✅ PASS
xr-release.aar 44.78 KB 0.00 KB 100.00% 100.0% (13/13) 100.0% (64/64) 0 ✅ PASS
data-release.aar 55.16 KB 0.00 KB 100.00% 100.0% (54/54) 100.0% (193/193) 0 ✅ PASS
camera-release.aar 291.37 KB 0.00 KB 100.00% 100.0% (93/93) 100.0% (550/550) 0 ✅ PASS
creation-release.aar 344.70 KB 0.00 KB 100.00% 100.0% (86/86) 100.0% (610/610) 0 ✅ PASS
home-release.aar 187.15 KB 0.00 KB 100.00% 100.0% (47/47) 100.0% (310/310) 0 ✅ PASS
results-release.aar 604.11 KB 0.00 KB 100.00% 100.0% (200/200) 100.0% (1127/1127) 0 ✅ PASS
watchface-release.aar 72.22 KB 2.69 KB 96.28% 95.7% (44/46) 96.1% (246/256) 11 ⚠️ WARN
common-release.aar 45.42 KB 0.00 KB 100.00% 100.0% (31/31) 100.0% (229/229) 0 ✅ PASS

✅ Gate Passed! All R8 scores, library shrinking ratios, and DEX sizes are within thresholds.

@AjeshRPai

Copy link
Copy Markdown
Contributor Author

Outdated PR, see #219 for the correct implementation

@AjeshRPai AjeshRPai closed this Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant