Repository navigation
[release/11.0.1xx] [Xamarin.Android.Build.Tasks] Keep only JNI-reachable ACW members in R8 rules - #13864
Conversation
…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>
|
/azp run Xamarin.Android-PR |
|
No pipelines are associated with this pull request. |
There was a problem hiding this comment.
🔵 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
|
@dalexsoto review |
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
⚠️ 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
|
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
left a comment
There was a problem hiding this comment.
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.
|
Internal
The public CI (dnceng-public) is offline, so this internal build is the only CI result. |
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
0dc443873043fa2c1e6e13196dfe2a4ce2bbdcaf↑ 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 writesproguard_project_primary.cfgfromacw-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:{ *; }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 adotnet new mauiapp, R8-whyareyoukeepingshows that the private fieldcom.microsoft.maui.PlatformDrawable.strokeThickness(in maui.aar) is kept by this file. The file also had each line about 3 times, becauseacw-map.txthas 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:
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
nativemethods, and their constructors,__md_methods,monodroidAddReference (), andmonodroidClearReferences ()are public. The first rule keeps private[Export]methods, which JNI can call by name. All Java callable wrappers implementIGCUserPeer. 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.jsonstats 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:dotnet new mauidotnet new androidThe 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.cfgwith the new rules: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.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.mdworkflow documentation, and in particular should include: