Repository navigation
[release/11.0.1xx] [Xamarin.Android.Build.Tasks] Generate R8 rules without trimming - #13558
Conversation
…thout trimming (#13546) Context: dotnet/maui#40138 A Release MAUI app using R8 `private-members` and `PublishTrimmed=false` fails during startup with a `TypeInitializationException` for `Microsoft.Maui.Controls.VisualElement`. The build reports XA4304 because `proguard_project_references.cfg` is missing. Changing only `PublishTrimmed` to `true` avoids the failure. `PublishTrimmed` controls managed trimming, not Java shrinking. Explicitly selecting `AndroidLinkTool=r8` already enables Java shrinking independently of `PublishTrimmed`; this PR preserves that existing behavior rather than disabling Java shrinking when managed trimming is off. Disabling Java shrinking alone would also not protect JNI names and signatures from obfuscation or optimization, so managed bindings still need keep rules. Reference-rule generation was restricted to trimmed builds and hooked to ILLink. Untrimmed builds therefore omitted the rules protecting Java members accessed through managed bindings. Generate these rules after `ComputeFilesToPublish` in untrimmed inner RID builds, preserving the existing trimmed/retained-typemap pipeline and incremental recovery behavior. With `PublishTrimmed=true`, the updated condition reduces to the same eligibility checks as before. The reported scenario is a Release build with R8 explicitly enabled. Ordinary Debug builds still use D8: `AndroidLinkTool` and the generated reference-rule path remain empty, so this change neither enables R8 nor adds the reference-rule scan to those builds. A Debug build that explicitly opts into R8 can also use the corrected untrimmed path; the fix is not restricted to the configuration name. Extend target and packaging regressions to cover untrimmed builds, unchanged inputs, and deleted-rule recovery. This targets current `main`; release backports will follow separately. The earlier `UnsatisfiedLinkError` in the issue remains unreproduced. Build-time measurements on an M5 Max MacBook showed no significant adverse overall impact for the MAUI sample-content app with managed trimming disabled and R8 enabled. Across six alternating before/after rounds, clean-build medians were approximately 51 seconds in both variants; the added rule-generation task took about 80 ms, implementation-edit builds increased by about 0.15 seconds at the median, and steady no-change builds remained approximately 1.5 seconds with both rule generation and R8 skipped. These are warm-cache, restore-excluded measurements on a shared machine, so small differences are within observed noise; the pre-fix variant builds but omits the required JNI rules. Backport of #13546 to release/11.0.1xx. release/11.0.1xx uses the LLVM IR typemap by default, and only sets `_ProguardProjectConfiguration` in `_PrepareLinking` for trimmed builds. This backport applies the fix to `Microsoft.Android.Sdk.TypeMap.LlvmIr.targets`: untrimmed inner RID builds set the rules path and generate the rules after `ComputeFilesToPublish`. Trimmed builds are unchanged. The test uses the rules file timestamp because this branch has no `.stamp` file. (cherry picked from commit 1013d2e) Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the backport enabling R8 keep-rule generation in untrimmed builds.
No open issues 🎉
What changed in this PR
Backports the R8 keep-rule fix to the release branch’s default LLVM IR typemap pipeline, protecting Java types accessed through managed bindings when PublishTrimmed=false.
Changes:
- Sets the reference-rule path and generates rules after
ComputeFilesToPublishin untrimmed inner RID builds, leaving trimmed builds unchanged. - Extends regression coverage to trimmed and untrimmed builds, unchanged-build timestamps, and recovery after rule-file deletion.
- Documents why R8 still needs keep rules when managed trimming is disabled.
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs | Adds untrimmed R8 coverage and incremental rule-file checks. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.LlvmIr.targets | Enables reference-rule generation for untrimmed inner RID builds. |
| Documentation/guides/D8andR8.md | Explains keep-rule generation without managed trimming. |
Review effort: Balanced
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed 989ac583 against release/11.0.1xx at 4fbdd893 (merge-base e5cdeb4e). This was a static, read-only review. I didn't build or run anything.
LGTM. In untrimmed builds that use r8 or proguard, the outer build already passes R8 the per-RID proguard_project_primary.cfg path (AssemblyResolution.targets, consumed by Common.targets and D8.targets). Nothing generated that file before, which is where XA4304 came from. The new _AndroidSetUntrimmedProguardConfiguration and _GenerateProguardAfterTargets path generates it at the same path in the inner RID build.
- Trimmed builds are unchanged.
PublishTrimmedgets its Release default fromDefaultProperties.targets, which is imported long beforeTypeMap.LlvmIr.targets. - NativeAOT is unaffected. It is always trimmed and uses its own
_GenerateProguardAfterTargets. - Builds with no link tool are unaffected because of the
AndroidLinkTool != ''check. - Running
GenerateProguardConfigurationover untrimmed inputs is safe. It skips assemblies that don't referenceMono.Android, files with no metadata, and unreadable images. - Incremental
Inputs/Outputsmatch the trimmed path, and the test covers both the no-op rebuild and recreating a deleted file. - On the branch tip, the files this touches are identical to the merge-base.
The trimmable typemap targets are mutually exclusive with TypeMap.LlvmIr.targets and unchanged here. Their untrimmed r8 gap predates this PR and is out of scope, as the description says.
🤖 Manual backport of #13546 to
release/11.0.1xx. The automatic backport failed with conflicts: #13546 (comment)Context: dotnet/maui#40138
A Release app that uses
AndroidLinkTool=r8andPublishTrimmed=falsecrashes at startup. The build reportsXA4304becauseproguard_project_references.cfgis missing. Without these rules, R8 removes Java types that managed bindings use through JNI (for examplecom.microsoft.maui.PlatformDispatcher).Differences from the
mainchangerelease/11.0.1xxuses the LLVM IR typemap by default. On this branch,_PrepareLinkingsets_ProguardProjectConfigurationonly for trimmed builds. So this backport applies the fix toMicrosoft.Android.Sdk.TypeMap.LlvmIr.targets:_AndroidSetUntrimmedProguardConfigurationtarget sets the rules path._PrepareLinkedAssembliesForProguardand_GenerateProguardConfigurationrun afterComputeFilesToPublishin untrimmed inner RID builds.Other differences:
TypeMapProguardTargetsTests.csis not on this branch, so this backport does not include it.Microsoft.Android.Sdk.TypeMap.Trimmable.CoreCLR.targetsis unchanged. The trimmable typemap is opt-in on this branch..cfgfile because this branch has no.stampfile.D8andR8.mdgets the same paragraph, in the matching section of this branch's version of the document.Validation
R8PreservesInstanceIntFieldTypewithpublishTrimmed: trueandfalse.Commands and results:
build.cmd(Prepare),dotnet-local.cmd build Microsoft.Android.slnx, anddotnet-local.cmd build Microsoft.Android.slnx -t:PackDotNet: passed.dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter Name~R8PreservesInstanceIntFieldType: passed (2 of 2).publishTrimmed: false, using the originalLlvmIr.targetsin the local SDK: failed withExpected: file exists. So the test detects the problem.net11.0-android, MAUI 11.0.10, workload 37.2.31, with the patched targets): noXA4304, the rules file is generated, and the app starts. A no-change build does not generate the file again, and the build restores the file after it is deleted.