[TrimmableTypeMap] Generate NativeAOT ProGuard rules from DGML#11449
Open
simonrozsival wants to merge 6 commits into
Open
[TrimmableTypeMap] Generate NativeAOT ProGuard rules from DGML#11449simonrozsival wants to merge 6 commits into
simonrozsival wants to merge 6 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a trimmable NativeAOT + R8 workflow where ProGuard/R8 keep rules are generated from NativeAOT ILC DGML scan output intersected with acw-map.txt, allowing R8 to remove unused generated Java wrappers and significantly reduce classes.dex size.
Changes:
- Generate NativeAOT-specific ProGuard rules after
IlcCompilefrom*.scan.dgml.xml+acw-map.txt, and adjust R8 configuration generation to avoid broad keep rules in this mode. - Extend the trimmable typemap pipeline to classify framework peers, selectively emit array typemap entries only when referenced from non-framework assemblies, and persist the list of generated typemap assemblies to stabilize incremental builds.
- Add runtime feature plumbing for
IsNativeAotRuntimeand adjust NativeAOT JNI initialization ordering/inputs to provide required Java peer marker classes.
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestFixtures/TestTypes.cs | Adds framework-like SSL and network peer types to exercise framework JCW/peer scanning scenarios. |
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Scanner/JavaPeerScannerTests.cs | Adds test coverage for framework peer marking and array-entry emission behavior. |
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TypeMapModelBuilderTests.cs | Tests framework ACW conditionality and array-entry emission rules; updates anchor visibility assertions. |
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TrimmableTypeMapGeneratorTests.cs | Verifies generator can emit expected framework JCW Java sources. |
| src/Xamarin.Android.Build.Tasks/Xamarin.Android.D8.targets | Plumbs UseTrimmableNativeAotProguardConfiguration into the R8 task invocation. |
| src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets | Adds _AndroidTrimmableTypeMapMaxArrayRank to the property cache for incremental invalidation. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64XFormsDotNet.MonoVM.apkdesc | Updates expected APK contents/sizes after build output changes. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64SimpleDotNet.MonoVM.apkdesc | Updates expected APK contents/sizes after build output changes. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/TrimmableTypeMapBuildTests.cs | Adds incremental/build validation for array-rank changes and NativeAOT/CoreCLR typemap behaviors. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/GenerateTrimmableTypeMapTests.cs | Adds task-level tests for generated typemap assembly list file and framework JCW emission. |
| src/Xamarin.Android.Build.Tasks/Tasks/R8.cs | Adds a trimmable NativeAOT mode that alters generated ProGuard config inputs and common rules. |
| src/Xamarin.Android.Build.Tasks/Tasks/GenerateTrimmableTypeMap.cs | Adds framework assembly classification and writes a generated-assemblies list file. |
| src/Xamarin.Android.Build.Tasks/Tasks/GenerateProguardConfiguration.cs | Adds DGML+ACW-map based ProGuard rule generation for NativeAOT trimmable builds. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.targets | Persists typemap-generated assembly list and uses it for downstream item population/incremental correctness. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.NativeAOT.targets | Hooks DGML-based ProGuard rule generation and adjusts ILC inputs for trimmable typemap NativeAOT. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.LlvmIr.targets | Adds opt-out switch to skip linked-assembly ProGuard configuration generation when replaced by DGML flow. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets | Enables DGML generation for R8 builds, configures skip/alternate ProGuard generation, and sets runtime feature. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.MonoVM.targets | Sets IsNativeAotRuntime=false runtime feature for MonoVM. |
| src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.CoreCLR.targets | Sets IsNativeAotRuntime=false runtime feature for CoreCLR. |
| src/native/nativeaot/host/host.cc | Ensures NativeAOT host provides global refs for required Java peer marker classes during init. |
| src/Mono.Android/Microsoft.Android.Runtime/RuntimeFeature.cs | Adds RuntimeFeature.IsNativeAotRuntime AppContext switch. |
| src/Mono.Android/Android.Runtime/JNIEnvInit.cs | Refactors JNI initialization to share common state init and add NativeAOT-specific runtime initialization entrypoint. |
| src/Mono.Android/Android.Runtime/JNIEnv.cs | Routes unhandled exception propagation consistently for NativeAOT. |
| src/Mono.Android/Android.Runtime/AndroidRuntimeInternal.cs | Treats NativeAOT unhandled exceptions like the CoreCLR path. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapGenerator.cs | Passes framework assembly names into scanning and tightens JCW generation filtering logic. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.cs | Tracks framework peers and controls array-entry emission based on cross-assembly references. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerInfo.cs | Adds IsFrameworkAssembly and GenerateArrayEntries to peer model. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/AssemblyIndex.cs | Indexes referenced types by referenced assembly to support framework peer reference detection. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/TypeMapAssemblyEmitter.cs | Updates documentation around __ArrayMapRank{N} anchors. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/RootTypeMapAssemblyGenerator.cs | Adds no-array-map initialization paths when max array rank is 0. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/ModelBuilder.cs | Adds additional unconditional types and prevents unconditional rooting for framework ACWs; gates array emission. |
| src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs | Uses the refactored JNIEnvInit initialization path and reuses common type/value manager creation helpers. |
Base automatically changed from
dev/simonrozsival/11052-nativeaot-typemap-init
to
main
May 22, 2026 14:26
7b41d33 to
158bce0
Compare
Use NativeAOT scan DGML to identify retained managed types, intersect them with acw-map.txt, and emit concrete R8 keep rules for trimmable typemap NativeAOT builds. This avoids keeping the broad generated Java wrapper set when R8 is enabled. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move DGML-based NativeAOT ProGuard generation into a separate MSBuild task so GenerateProguardConfiguration remains focused on linked assembly scanning. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep NativeAOT.targets free of ProGuard coordination properties. Let LlvmIr targets hardcode linked-assembly ProGuard timing for ILLink and NativeAOT, and keep the trimmable NativeAOT R8 mode flag with the trimmable NativeAOT typemap targets. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move the trimmable NativeAOT common R8 rules to an embedded ProGuard resource, keep GenerateProguardConfiguration unchanged, add a shared trimmable typemap ProGuard target name with a CoreCLR stub, and restore the existing linked-assembly ProGuard target wiring. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Default NativeAOT trimmable typemap builds to AndroidLinkTool=r8 so the generated DGML-based keep rules are active without requiring users to opt in manually. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tolerate the no-input typemap generation case by creating an empty assembly list, avoid nested framework reference scans, and disable XML resolver use when reading ILC DGML. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
158bce0 to
3d4e1cd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IlcCompileby intersecting retained managed type metadata from ILC scan DGML withacw-map.txtGenerateNativeAotProguardConfigurationtask, leaving the existing linked-assemblyGenerateProguardConfigurationpath unchangedStacked on #11292
Related to dotnet/runtime#120204
Part of #10790
Part of #11052
Results
HelloWorld NativeAOT
android-arm64Release with trimmable typemap. Before is the existing default without R8 Java shrinking; after is the new default R8 behavior from this PR.classes.dexsizeclasses.dexin APKThe HelloWorld ILC scan DGML input is 24,044,488 bytes and
acw-map.txthas 27,234 lines. The generated ProGuard configuration has 49 rules.20-run measurement on Apple M1 for the isolated
GenerateNativeAotProguardConfigurationtask:Validation
MSBUILDDISABLENODEREUSE=1 ./dotnet-local.sh build src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj -c Debug -nr:false --nologo -v minimalMSBUILDDISABLENODEREUSE=1 ./dotnet-local.sh build src/Microsoft.Android.Sdk.TrimmableTypeMap/Microsoft.Android.Sdk.TrimmableTypeMap.csproj -c Debug -nr:false --nologo -v minimalMSBUILDDISABLENODEREUSE=1 ./dotnet-local.sh build samples/HelloWorld/HelloWorld/HelloWorld.DotNet.csproj -t:SignAndroidPackage -c Release -p:PublishAot=true -p:_AndroidTypeMapImplementation=trimmable -p:RuntimeIdentifier=android-arm64 -p:AndroidPackageFormat=apk -nr:false --nologo -v minimal