Skip to content

[trimmable-type-map] Avoid unused XML references - #12813

Draft
simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-trim-xml-assemblies
Draft

simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-trim-xml-assemblies

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Sep 17, 2026

Copy link
Copy Markdown
Member

Summary

  • create XML marshalling metadata references lazily in generated [Export] dispatch assemblies
  • avoid adding System.Xml.ReaderWriter to a generated type-map assembly when exported signatures do not use XML parser mappings
  • preserve the existing XML marshalling references when XmlPullParser or XmlResourceParser mappings are used

APK size and combined MAUI validation

The PR is draft because the stock MAUI sample-content application does not currently demonstrate an APK-size or packaged-assembly reduction from this change.

The first stock .NET 11 Android CoreCLR Release comparison produced an identical signed APK before and after this change:

Build Signed APK size
Baseline 23,300,175 bytes
Patched 23,300,175 bytes
Difference 0 bytes

Three independent MAUI trimming fixes were then applied together:

The combined MAUI changes reduced the sample-content signed APK by 81,920 bytes, removed XamlLoader, XamlParser, and LocalhostRegex, and reduced the retained XML/regex implementations. However, System.Xml.ReaderWriter remains rooted directly by source-generated XAML line-info calls in the application, and System.Private.Xml remains transitively and through additional framework/control references.

To exercise this PR's actual code path, the identical combined-MAUI input was then published twice with the trimmable Android type map explicitly enabled:

dotnet publish CombinedTrimApp.csproj \
  -c Release -f net11.0-android -r android-arm64 \
  -p:AndroidKeyStore=false \
  -p:PublishReadyToRun=false \
  -p:TrimMode=partial \
  -p:_AndroidTypeMapImplementation=trimmable \
  -p:AndroidPackageFormats=apk
Output Baseline generator Patched generator Difference
Signed APK 21,248,079 B 21,248,079 B 0 B
Unsigned APK 21,118,994 B 21,119,021 B +27 B packaging noise
Generated type-map DLLs 11,133,952 B 11,133,952 B 0 B
System.Xml.ReaderWriter.dll 5,120 B 5,120 B 0 B
System.Private.Xml.dll 381,440 B 381,440 B 0 B

All 86 generated type-map DLLs were byte-identical, and none had a System.Xml.ReaderWriter assembly reference. The sample-content app and its dependencies do not contain a non-XML [Export] dispatcher that triggers the bug fixed here. Its remaining System.Xml.ReaderWriter reference comes from generated XAML InitializeComponent line-info services (System.Xml.IXmlLineInfo), outside the Android type-map generator.

Therefore this change is valid generator cleanup and is covered by focused tests, but it does not currently reduce the stock MAUI sample-content APK or remove its XML assemblies. The benefit applies only to an app/library that generates a non-XML [Export] dispatcher and has no independent XML roots.

Tests

  • dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj --no-restore --nologo -v:minimal
  • targeted TypeMapAssemblyGeneratorTests.Generate_Export* coverage

Create XML marshalling metadata lazily so non-XML Export methods do not root System.Xml.ReaderWriter and System.Private.Xml.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 17, 2026 06:25

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.

Copilot review overview

🔵 Needs a closer look

Add coverage for XML parameter/return paths and all supported XML assembly identities.

Review effort: Lite
Findings: None

What changed in this PR

This PR lazily creates XML marshalling metadata to avoid unnecessary XML references in generated dispatch assemblies.

Changes:

  • Defers XML metadata reference creation until needed.
  • Preserves references for XML parser mappings.
  • Adds conditional-reference regression tests.
File Summary
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.Tests/​Generator/​TypeMapAssemblyGeneratorTests.cs Tests conditional XML reference emission.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​ExportMethodDispatchEmitterContext.cs Adds lazy XML metadata reference getters.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​ExportMethodDispatchEmitter.cs Uses lazy getters for XML marshalling emission.

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.

2 participants