Skip to content

[release/11.0.1xx] [Xamarin.Android.Build.Tasks] Generate R8 rules without trimming - #13558

Merged
jonathanpeppers merged 1 commit into
release/11.0.1xxfrom
jonathanpeppers-repro-maui-40138-r8-crash
Nov 17, 2026
Merged

jonathanpeppers merged 1 commit into
release/11.0.1xxfrom
jonathanpeppers-repro-maui-40138-r8-crash

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

🤖 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=r8 and PublishTrimmed=false crashes at startup. The build reports XA4304 because proguard_project_references.cfg is missing. Without these rules, R8 removes Java types that managed bindings use through JNI (for example com.microsoft.maui.PlatformDispatcher).

Differences from the main change

release/11.0.1xx uses the LLVM IR typemap by default. On this branch, _PrepareLinking sets _ProguardProjectConfiguration only for trimmed builds. So this backport applies the fix to Microsoft.Android.Sdk.TypeMap.LlvmIr.targets:

  • In untrimmed inner RID builds, the new _AndroidSetUntrimmedProguardConfiguration target sets the rules path.
  • _PrepareLinkedAssembliesForProguard and _GenerateProguardConfiguration run after ComputeFilesToPublish in untrimmed inner RID builds.
  • Trimmed builds are unchanged.

Other differences:

  • TypeMapProguardTargetsTests.cs is not on this branch, so this backport does not include it.
  • Microsoft.Android.Sdk.TypeMap.Trimmable.CoreCLR.targets is unchanged. The trimmable typemap is opt-in on this branch.
  • The test checks the timestamp of the .cfg file because this branch has no .stamp file.
  • D8andR8.md gets the same paragraph, in the matching section of this branch's version of the document.

Validation

Commands and results:

  • build.cmd (Prepare), dotnet-local.cmd build Microsoft.Android.slnx, and dotnet-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).
  • The same test with publishTrimmed: false, using the original LlvmIr.targets in the local SDK: failed with Expected: file exists. So the test detects the problem.
  • Manual check on a Pixel 7 (API 37) with the App built with R8 enabled crashes at start up maui#40138 repro (net11.0-android, MAUI 11.0.10, workload 37.2.31, with the patched targets): no XA4304, 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.

…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]>
Copilot AI balanced review requested due to automatic review settings November 13, 2026 19:22

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.

🟢 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 ComputeFilesToPublish in 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

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

Copy link
Copy Markdown
Member Author

@dalexsoto review

@jonathanpeppers
jonathanpeppers enabled auto-merge (squash) November 16, 2026 22:09

@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 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. PublishTrimmed gets its Release default from DefaultProperties.targets, which is imported long before TypeMap.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 GenerateProguardConfiguration over untrimmed inputs is safe. It skips assemblies that don't reference Mono.Android, files with no metadata, and unreadable images.
  • Incremental Inputs/Outputs match 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.

@jonathanpeppers
jonathanpeppers merged commit 80cd1ce into release/11.0.1xx Nov 17, 2026
45 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-repro-maui-40138-r8-crash branch November 17, 2026 00:14
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.

4 participants