Skip to content

Add R8 regression check workflow - #219

Merged
AjeshRPai merged 18 commits into
mainfrom
r8-modular-regression-gate
Nov 25, 2026
Merged

AjeshRPai merged 18 commits into
mainfrom
r8-modular-regression-gate

Conversation

@AjeshRPai

@AjeshRPai AjeshRPai commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Adds CI (GitHub Actions) regression gates powered by android analyze to catch R8 configuration regressions, unshrunk libraries, and DEX size bloat before merge.


1. Prerequisites & Setup Requirements

To adopt this workflow in a repository, the following baseline file, secrets, build settings, and tools are required:

A. Initial R8 Baseline File (r8-baseline.json)

  • Why it is needed: check_app_r8_score.py compares every PR's release App Bundle (.aab) against a committed r8-baseline.json file in the repository root. This file stores the reference android analyze output (shrinkingRatio, optimizationRatio, obfuscationRatio, and uncompressedDexSizeBytes).
  • How to generate it the first time:
    # 1. Build the release App Bundle
    ./gradlew :app:bundleRelease
    
    # 2. Run android analyze and save the output to r8-baseline.json
    python3 scripts/check_app_r8_score.py \
      --aab app/build/outputs/bundle/release/app-release.aab \
      --baseline r8-baseline.json \
      --save-baseline
    
    # 3. Commit r8-baseline.json to your default branch
    git add r8-baseline.json && git commit -m "Add initial R8 baseline"
  • Ongoing updates: Once committed, the GitHub Actions workflow automatically refreshes and commits r8-baseline.json whenever a passing PR merges to main.

B. GitHub Actions Secrets & Permissions

Configure the following in Settings → Secrets and variables → Actions:

  1. Workflow Token Permissions (GITHUB_TOKEN / Optional PAT):
    • The workflow declares contents: write, pull-requests: write, and issues: write permissions so it can post scorecard comments on PRs (gh pr comment) and auto-commit updated r8-baseline.json files on main.
    • Optional PAT Secret: If your main branch has branch protection rules that block github.token from pushing commits, add a Personal Access Token secret named PAT with repo / contents: write scope.
  2. Project Build Secrets (e.g., DEBUG_GOOGLE_SERVICES_JSON_BASE64):
    • Any secrets required to build :app:bundleRelease in CI (in Androidify, if DEBUG_GOOGLE_SERVICES_JSON_BASE64 is not set, the workflow falls back to copying test-google-services.json to app/google-services.json).

C. Android Gradle Plugin (AGP) & R8 Build Configuration

  1. AGP 9.0+ with Release Minification Enabled (isMinifyEnabled = true):
    • In app/build.gradle.kts, the release build type must have isMinifyEnabled = true.
    • AGP 9.0+ embeds BUNDLE-METADATA/com.android.tools/r8.json inside app-release.aab, which android analyze reads to calculate shrinking, optimization, and obfuscation scores.
  2. Library Module Consumer Rules (consumerProguardFiles):
    • For multi-module checks (check_library_shrinking.py --scan-libraries), library modules must build release AARs (./gradlew assembleRelease) so android analyze can inspect their packaged proguard.txt consumer rules and run isolated D8 vs. R8 shrinking comparisons.

D. Android CLI

  • Android CLI (android):
    • Installed automatically in CI via https://dl.google.com/android/cli/latest/linux_x86_64/install.sh (or locally on macOS via darwin_arm64/install.sh), providing the android analyze --analysis=optimization command.

2. Modular Check Scripts (scripts/)

Teams can adopt both checks or pick only the one that fits their workflow:

Script Target What It Checks Typical Stage
check_library_shrinking.py .aar & .jar modules/libraries Runs an isolated R8 dry-run ((1 - r8DexSize / d8DexSize) * 100) and flags consumer proguard.txt rules with package-wide wildcards CI
check_app_r8_score.py Release App Bundle (.aab) Compares shrinkingRatio, optimizationRatio, obfuscationRatio (0–100) and uncompressedDexSizeBytes against r8-baseline.json CI

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

Triggers & Inputs

  • pull_request / push (main): Runs automatically whenever ProGuard/R8 files (**/*.pro), Gradle build scripts (**/build.gradle.kts, gradle/libs.versions.toml, gradle.properties), r8-baseline.json, or scripts/check_*.py change.
  • workflow_dispatch: Supports manual execution against any PR or commit with configurable thresholds:
    • pr_number: Optional PR number to checkout and comment on.
    • max_score_drop (default 2.0): Maximum allowed drop in R8 shrinking, optimization, or obfuscation scores (percentage points).
    • max_dex_growth_pct (default 5.0): Maximum allowed uncompressed DEX size increase (%).
    • min_lib_shrinking_pct (default 90.0): Minimum required isolated DEX shrinking percentage for module AARs (%).

Workflow Pipeline Steps

  1. Prepare Review Context: Resolves the PR number to comment on, and checks out the PR on manual runs.
  2. Install Android CLI: Installs the latest android CLI (https://dl.google.com/android/cli/latest/linux_x86_64/install.sh).
  3. Build Release Artifacts: Runs ./gradlew :app:bundleRelease assembleRelease to produce the release App Bundle (app-release.aab) and module AARs (*-release.aar).
  4. Check App Bundle R8 Scores & DEX Size (app_r8_gate): Runs check_app_r8_score.py to compare the release .aab against r8-baseline.json and writes the scorecard to $RUNNER_TEMP/r8_summary.md and $GITHUB_STEP_SUMMARY.
  5. Check Module & Library Shrinking Percentage (lib_r8_gate): Runs check_library_shrinking.py (--scan-libraries --append-summary) across built module AARs to verify isolated R8 shrinking ratios and flag package-wide consumer keep rules.
  6. Auto-Ratify Baseline on main: On push to main when both gates pass, runs check_app_r8_score.py --save-baseline and auto-commits updated metrics to r8-baseline.json.
  7. PR Comment & HTML Report Upload: Posts the R8 scorecard directly to the PR via gh pr comment, uploads r8-config-analyzer-report (r8-config-analyzer-release.html), and fails the check if any regression threshold was breached.

Comment thread scripts/check_app_r8_score.py Resolved
Comment thread scripts/check_app_r8_score.py Resolved
Comment thread scripts/check_app_r8_score.py Resolved
Comment thread scripts/check_app_r8_score.py Resolved
Comment thread scripts/check_library_shrinking.py Resolved
Comment thread scripts/check_library_shrinking.py Resolved
Comment thread scripts/check_library_shrinking.py Resolved
Comment thread scripts/check_library_shrinking.py Resolved
Comment thread scripts/check_library_shrinking.py Resolved
Comment thread scripts/check_r8_regression.py Resolved
- check_library_shrinking.py: read the keys `android analyze` actually
  outputs (keptItemCount, configuration.used/unusedPackageWideKeepRules,
  blockedKeepRules[].source) and count rules with len(). Package-wide
  consumer rules were always reported as 0 before (watchface has 11).
  Drop the redundant keep ratio and rename format_kb to format_size.
- check_app_r8_score.py: single default AAB path, drop --build and the
  old JSON shape, read the package name from --analysis=basic.
- Remove ANDROID_CLI_ANALYZE (no longer needed) and
  --no-configuration-cache from the scripts and workflow.
- Always run the git hooks, the pre-commit config hooks and the CI
  workflow instead of filtering by changed files.
- Post one PR comment and edit it on later runs.
- Remove check_r8_regression.py, superseded by the split scripts.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🛡️ R8 Optimization & Shrinking Gate (android analyze)

📦 App Bundle R8 Scores & DEX Size

  • Package: com.android.developers.androidify
  • Compiler: AGP 9.3.2 / R8 9.3.16 (Full Mode: True)
  • Baseline: r8-baseline.json
Metric Baseline Current Delta Threshold Status
Shrinking Score 52.49% 51.64% -0.85% pts max -2.00% pts ✅ PASS
Optimization Score 52.15% 51.30% -0.85% pts max -2.00% pts ✅ PASS
Obfuscation Score 52.45% 51.61% -0.84% pts max -2.00% pts ✅ PASS
Uncompressed DEX Size 17.99 MB 17.53 MB -0.45 MB (-2.5%) max +5.0% / +500KB ✅ PASS

📚 Library Shrinking Percentage (Min Threshold: 90.0%)

  • Libraries Analyzed: 12 (✅ 11 passed, ⚠️ 1 with package-wide keep rules, ❌ 0 failed)

On every PR, post a GitHub-style diff of the keep rules that changed
compared with main, and the classes they keep. It covers rules that come
with library upgrades and existing wildcard rules that start keeping new
classes, neither of which shows up in the PR's own diff.

It doesn't build anything. With AGP 9.3.2, every release build writes
the keep rule .pb file (app/build/outputs/mapping/release/configanalyzer.pb)
by default, so the gate uploads the file from the build it already runs.

- keep_rule_diff.yml: reusable workflow, given the .pb file's path. On
  pushes to main it caches the file per commit; on PRs it diffs against
  main's cached file, writes the job summary and keeps one PR comment
  up to date.
- r8_regression_check.yml: upload the .pb file after the release build
  and call keep_rule_diff.yml in a second job, which also runs when the
  gate fails. Optionally skip the diff when the R8 scores didn't change
  (KEEP_RULE_DIFF_ONLY_IF_SCORES_CHANGED).
- scripts/keep_rule_diff.py and scripts/schema_pb2.py: compare two keep
  rule .pb files and write the Markdown report.

Needs AGP 9.3 or newer to write the .pb file; until then the diff is
skipped.
zizmor's excessive-permissions audit flags workflow-level write
permissions once a workflow has more than one job, which the new
keep-rule-diff job made it. Move them to the r8-regression-gate job;
keep-rule-diff already sets its own (contents: read, pull-requests: write).
@android android deleted a comment from github-actions Bot Oct 29, 2026
main doesn't run the keep rule diff yet, so the cache has no keep rules
from main to compare this PR against. A temporary job builds the PR's
base commit once, with AGP 9.3.2 so that it writes the keep rule .pb file,
and caches it under the key the keep-rule-diff job looks for.

To be removed once the diff has run.

@cwoodwar6 cwoodwar6 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if having the keep rule diff is worth the added proto complexity. It is nice to have when a library changes a keep rule, but for anything changed within the project, those changes are already visible in the git diff.

Comment thread scripts/schema_pb2.py Resolved
Comment thread scripts/keep_rule_diff.py Resolved
Drop the keep rule diff and the temporary job that seeded main's keep
rules for it. The R8 regression gate itself is unchanged.

- keep_rule_diff.yml, scripts/keep_rule_diff.py and
  scripts/schema_pb2.py: deleted.
- r8_regression_check.yml: remove the keep-rule-diff and
  seed-keep-rule-base jobs, the upload of the keep rule .pb file, and
  the optional check of whether the R8 scores changed
  (KEEP_RULE_DIFF_ONLY_IF_SCORES_CHANGED).
@android android deleted a comment from github-actions Bot Oct 30, 2026
Android CLI 1.1.16919734 writes the AAR and JAR DEX sizes as JSON
strings, because they're now int64 values. The library check already
reads them with int(), so add a comment to keep it that way.

An outdated CLI prints an update notice on stdout after the JSON, which
made json.loads fail with "Extra data". Parse only the JSON object.

Regenerate r8-baseline.json with the latest CLI from the AGP 9.3.2
release build. The old baseline came from an older CLI and an AGP 9.2.1
build.
Drop the Gemini CLI step and the r8-analyzer skill install, along with
the build file diff and the .gitignore edit that only the review used.
The scorecard now goes to the runner's temporary folder instead of
.gemini/, and the pull request comment contains only the scorecard.
AjeshRPai and others added 2 commits November 24, 2026 21:18
List only the libraries that regressed instead of a row for every library, to keep the PR comment short. The job log still prints every library.
@AjeshRPai
AjeshRPai requested a review from cwoodwar6 November 24, 2026 16:15
The table already marks the failing app metrics, so stop repeating them as a list below it. Failures without a table row, such as R8 full mode being turned off, are still listed. Also drop the lowest library shrinking line, which repeated the worst library's regression.
Comment thread scripts/git-hooks/pre-commit Resolved
Comment thread scripts/check_app_r8_score.py Outdated
"package_name": result.get("basic", {}).get("appInfo", {}).get("packageName", "N/A"),
"agp_version": bundle.get("agpVersion", "N/A"),
"r8_version": r8_json.get("version", "N/A"),
"r8_enabled": marker.get("r8Enabled", False),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R8 enablement is implied by the presence of r8Json (version is a decent proxy). I might write this as "r8_enabled": r8_version != "N/A" or marker.get("r8Enabled", False),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 02f28e1.

Comment thread scripts/check_app_r8_score.py Resolved
@AjeshRPai
AjeshRPai requested a review from riggaroo November 24, 2026 17:34
R8 enablement is implied by the presence of r8.json, and its options say whether full mode is on. Fall back to the DEX marker only when r8.json lacks them.
Drop the pre-commit and pre-push hooks, their pre-commit config, and the staged diff check that only the hooks ran. The GitHub Actions workflow is the gate.
@AjeshRPai
AjeshRPai merged commit 8e5a0ea into main Nov 25, 2026
11 checks passed
@AjeshRPai AjeshRPai changed the title [Dont merge] Add R8 regression check workflow Add R8 regression check workflow Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants