Skip to content

[release/11.0.1xx] [Xamarin.Android.Build.Tasks] Keep only JNI-reachable ACW members in R8 rules - #13864

Merged
jonathanpeppers merged 1 commit into
release/11.0.1xxfrom
jonathanpeppers-narrow-acw-keep-rules-legacy-r8
Dec 2, 2026
Merged

jonathanpeppers merged 1 commit into
release/11.0.1xxfrom
jonathanpeppers-narrow-acw-keep-rules-legacy-r8

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 0dc443873043fa2c1e6e13196dfe2a4ce2bbdcaf

Dimension Assessment Rationale
Change confidence 8/10 ↑ Same change on main: Mono.Android.NET-Tests Release with R8 on Pixel 7, 1154 passed, 0 failed.
↑ MAUI and android template apps start and work on device.
↑ Rubber-duck review (Gemini 4 Carbon) found no blocking issues.
↓ Not built or tested on release/11.0.1xx locally; CI is pending.
↓ Fields from non-public [ExportField] and private members read by app JNI or reflection are no longer kept.

Context: #13849

The <R8/> task writes proguard_project_primary.cfg from acw-map.txt. This is the default path for .NET 11 Release builds with R8. For every Java callable wrapper and bound Java type, it wrote:

-keep class crc64b5e713d400f589b7.MauiDrawable { *; }

{ *; } keeps every member, including private fields. R8 also applies member rules to superclass members, so private fields of library base classes are kept as well. In a dotnet new maui app, R8 -whyareyoukeeping shows that the private field com.microsoft.maui.PlatformDrawable.strokeThickness (in maui.aar) is kept by this file. The file also had each line about 3 times, because acw-map.txt has several keys for each Java name.

Google Play calculates R8 obfuscation, optimization, and shrinking scores from BUNDLE-METADATA/com.android.tools/r8.json. These rules are a large part of the restrictions in that file.

Now the file has one shared rule and one rule for each distinct Java type:

-keepclassmembers class * implements mono.android.IGCUserPeer { private <methods>; }
-keep class crc64b5e713d400f589b7.MauiDrawable { public protected *; native <methods>; }

This is the same design as #13849, which changes the CoreCLR trimmable-typemap path on main. Bindings use only public and protected Java members. Java callable wrappers call managed code through native methods, and their constructors, __md_methods, monodroidAddReference (), and monodroidClearReferences () are public. The first rule keeps private [Export] methods, which JNI can call by name. All Java callable wrappers implement IGCUserPeer. The rule keeps only methods, because R8 also applies field rules to superclass fields. User @(AndroidJavaSource) types still get { *; }.

Limitations: fields generated for a non-public [ExportField], and private or package-private members that app code reads by JNI or reflection, are no longer kept. These need app ProGuard rules.

Results

r8.json stats are the percentage of items that R8 is not allowed to rename, optimize, or remove. Lower is better.

Release .aab, android-arm64, CoreCLR, $(AndroidLinkTool)=r8, local SDK from main with $(_AndroidEnableTypemapR8Trimming)=false (this uses the same acw-map code path as release/11.0.1xx), Microsoft.Maui.Controls 11.0.0, R8 9.5.33:

App DEX bytes noObfuscation % noOptimization % noShrinking %
dotnet new maui before 5,054,180 74.61 41.28 38.50
after 4,905,740 (-148,440) 69.43 35.19 32.34
dotnet new android before 16,484 90.78 90.78 90.78
after 16,484 89.36 89.36 89.36

The same MAUI app built with the shipped .NET 11 SDK 11.0.100 (Android workload 37.2.31, R8 9.5.26). I re-ran R8 with the same response file and only replaced proguard_project_primary.cfg with the new rules:

DEX bytes noObfuscation % noOptimization % noShrinking %
before 5,166,352 71.93 37.51 34.79
after 5,119,396 (-46,956) 69.75 35.08 32.50

Validation

I tested the same change on main, with $(_AndroidEnableTypemapR8Trimming)=false so that the acw-map path is used:

  • dotnet test --project src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj --filter FullyQualifiedName~Xamarin.Android.Build.Tests.R8Tests: 32 passed.
  • Mono.Android.NET-Tests, -c Release -p:_AndroidEnableTypemapR8Trimming=false (R8, $(TrimMode)=full, CoreCLR), on Pixel 7: 1154 passed, 0 failed, 1 skipped.
  • Pixel 7: the MAUI app starts, and the "Click me" button updates its text. The android template app starts. No errors in logcat.

I did not build release/11.0.1xx locally. Its Prepare step replaces the shared Android SDK platform-tools on the build machine. CI covers this branch.


Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

…R8 rules

The R8 task wrote `-keep class X { *; }` to proguard_project_primary.cfg for every Java type in acw-map.txt. This keeps all members, including private fields. R8 also applies member rules to superclass members, so private fields of library base classes are kept too, for example `com.microsoft.maui.PlatformDrawable.strokeThickness` in maui.aar.

Now the file has one shared rule and one rule per distinct Java type:

    -keepclassmembers class * implements mono.android.IGCUserPeer { private <methods>; }
    -keep class X { public protected *; native <methods>; }

This is the same design as #13849. Bindings use only public and protected Java members, and Java callable wrappers call managed code through native methods. The first rule keeps private [Export] methods. Duplicate lines are also removed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 18:04
@jonathanpeppers

Copy link
Copy Markdown
Member Author

/azp run Xamarin.Android-PR

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Dropping default retention for non-public ExportField members and private JNI targets requires maintainer compatibility sign-off and release-branch runtime validation, since reported device tests used main.

No open issues 🎉

What changed in this PR

Narrows R8 keep rules generated from acw-map.txt in .NET 11 Release builds, extending the approach from #13849 to this code path so R8 can remove more unused Java members.

Changes:

  • Keeps public, protected, and native members, plus private methods on Java callable wrappers; user-authored Java source retains full-member protection.
  • Deduplicates Java type rules while preserving deterministic ordering.
  • Adds a regression test for the generated rules, ordering, and deduplication.
File Description
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​R8Tests.cs Tests narrowed member rules and sorted, duplicate-free type entries.
src/​Xamarin.Android.Build.Tasks/​Tasks/​R8.cs Generates narrower keep rules and deduplicates Java types.

Review effort: Balanced

@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Dec 2, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@dalexsoto review

@jonathanpeppers

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13864

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Validation pending — no actionable code findings

Reviewed both changed files in full and traced the rules against the legacy wrapper generator and shared ProGuard configuration. 0 errors, 0 warnings, 0 suggestions. Ordinal deduplication preserves deterministic ordering; generated Java constructors remain public, private [Export] methods are covered, and user-authored Java sources retain their existing keep rules. The non-public field/JNI compatibility limitations are explicitly acknowledged in the description.

CI is not yet green: Xamarin.Android-PR is in progress ([build 16181767]((devdiv.visualstudio.com/redacted) the two GitHub checks returned are successful. This was a static review; the author-reported build/device results were not independently rerun. No merge-ready verdict until branch CI completes.

Generated by Android PR Reviewer for #13864 · copilot · gpt61 · ~33 AIC · ⌖ ~12 AIC · ⊞ 22.3K
Comment /review to run again

@dalexsoto

Copy link
Copy Markdown
Member

In response to @jonathanpeppers's request: I reviewed both changed hunks and the new R8 test at 0dc4438, with no confirmed blocker in that portion. Before a verdict I still need to trace Java.Interop wrapper visibility and IGCUserPeer generation, proguard_xamarin.cfg interactions, LoadMapFile nested-type names, the release-branch target delta, and the remaining R8.cs/R8Tests.cs context, then run a separate static final pass. This is a reviewer coverage gap, not a defect in the PR; the bounded review window ended before those reads. The accepted limitations are not being raised as findings.

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed both changed files, generated Java-wrapper visibility and default code-generation target, shared ProGuard rules, map loading, and the target-branch integration. The documented keep-rule limitations remain accepted; no actionable blocking issues found. Static review only; no builds, tests, generators or devices were run.

@jonathanpeppers

Copy link
Copy Markdown
Member Author

Internal Xamarin.Android-PR build 16181767 failed. I reviewed the failures, and none of them appear to be caused by this PR. They can be ignored:

Failure Why it can be ignored
Task: MAUI Integration / Run MAUI R2R APKs on Helix Fails on every release/11.0.1xx build.
Test: BuildTest2.Plugin_Maui_Audio(CoreCLR) (Windows > Tests > MSBuild 1) Network error in the log: No such host is known (cflvsblobprodcus383.vsblob.vsassets.io:443).
Task: macOS > Tests > MSBuild 3 No test failed (72 passed, 7 skipped). The job stopped at its 3-hour time limit because the agent was slow: basic builds took about 8 minutes each. The R8 tests in this slice passed and were not slow, for example BuildProguardEnabledProject (0.5 min).

The public CI (dnceng-public) is offline, so this internal build is the only CI result.

@jonathanpeppers
jonathanpeppers merged commit fd9e7ad into release/11.0.1xx Dec 2, 2026
2 of 3 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-narrow-acw-keep-rules-legacy-r8 branch December 2, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants