[TrimmableTypeMap] Implement reflection-free TrimmableTypeMapValueManager and TrimmableTypeMapTypeManager#11617
[TrimmableTypeMap] Implement reflection-free TrimmableTypeMapValueManager and TrimmableTypeMapTypeManager#11617simonrozsival wants to merge 108 commits into
Conversation
Generate the trimmable typemap once before trimming and reuse the generated Java sources instead of running GenerateTrimmableTypeMap again after ILLink. Keep the linked/R2R typemap assembly packaging path from the previous fix, and remove diagnostics that only supported the deleted post-trim Java copy path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
42a1b8e to
47450cf
Compare
|
Rebased onto #11622 (external/Java.Interop d7dbad5) and revalidated targeted Mono.Android build locally.\n\n/azp run |
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
Bumps [external/Java.Interop](https://github.com/dotnet/java-interop) from `b881d21` to `d7dbad5`. - [Commits](dotnet/java-interop@b881d21...d7dbad5) --- updated-dependencies: - dependency-name: external/Java.Interop dependency-version: d7dbad5e30a8f03743a508a95c4e9159fe1f6607 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com>
Split the Android JavaMarshal value manager into CoreCLR and trimmable implementations that share peer registration and GC bridge integration through a reusable helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the trimmable typemap value manager on the abstract JniValueManager base, sharing only peer registration and GC bridge state with the CoreCLR value manager. Leave value marshaling unsupported for now until Android has trimmable-specific marshalers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move TrimmableTypeMapTypeManager off ReflectionJniTypeManager and implement type lookup through explicit built-in mappings plus the generated trimmable typemap. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove newly added UnconditionalSuppressMessage attributes, propagate Requires annotations from reflection-backed managers, and carry DAM annotations through JavaPeerProxy/TrimmableTypeMap target type metadata. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace newly added suppressions on reflection-backed managers with RequiresUnreferencedCode and RequiresDynamicCode propagation. Leave trimmable value/type managers free of UnconditionalSuppressMessage attributes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace remaining UnconditionalSuppressMessage attributes in the reflection-backed Android manager implementations with RequiresUnreferencedCode/RequiresDynamicCode where appropriate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace invoker lookup and legacy TypeManager peer creation suppressions with RequiresUnreferencedCode/RequiresDynamicCode propagation. Keep GetObject suppression because adding DAM there breaks delegate/reflection table use sites. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Annotate runtime feature switches with FeatureGuard and structure manager factory branches so reflection-backed manager creation is guarded by the relevant runtime feature instead of broad Requires annotations on the factory methods. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove the single-use JavaMarshalReflectionValueManagerBase and keep the shared peer/GC bridge state in JavaMarshalPeerManager, directly delegated by the CoreCLR value manager. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Make both TrimmableTypeMapTypeManager RegisterNativeMembers overloads throw UnreachableException directly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Make unused trimmable JniTypeManager paths fail loudly, remove ManagedPeer from trimmable runtime artifacts, and add an initial AOT-safe value-marshaling implementation for the trimmable value manager. Update tests and trimmable runtime coverage to use feature switches via AppContext and enable the value-marshaling test bucket for follow-up triage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use the Java.Interop proxy and peerable value marshalers from the trimmable value manager instead of duplicating peerable marshaling locally. This also updates the Java.Interop submodule to the follow-up branch with the shared proxy marshaler and re-enables the trimmable tests now covered by the shared marshalers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…a-interop-1441-android # Conflicts: # src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64XFormsDotNet.MonoVM.apkdesc
Remove ManagedPeer from the trimmable Java runtime and disable Java.Interop ManagedPeer native registration for trimmable typemap builds. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…441-android' into dev/simonrozsival/java-interop-1441-android # Conflicts: # src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMapValueManager.cs
This reverts commit 6645710.
…al/java-interop-1441-android
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/review |
|
✅ Android PR Reviewer completed successfully! |
There was a problem hiding this comment.
🤖 Code Review — ⚠️ Needs Changes
Thorough, well-structured PR that splits the value-manager/type-manager responsibilities and keeps the trimmable typemap path reflection-free. The runtime wiring, generator, and MSBuild changes are coherent. One behavioral question on the legacy marshaling path should be resolved before merge; the other two items are minor.
Findings
⚠️ 1 warning —JavaConvert.GetJniHandleConverter: non-genericIDictionary/IListchecks are now nested under theIsGenericTypeguard, silently changing the returned wrapper (JavaDictionary/JavaList→JavaCollection) for non-generic targets on the Mono/CoreCLR path. Needs confirmation or a fix (inline).- 💡 2 suggestions — typo in a user-visible exception string (
"Unsupporte runtime."); duplicatedIsMonoRuntime/IsCoreClrRuntimebranches inCreateTypeManager(inline).
Strengths
- Clean error-code lifecycle.
XA4254/XA4255removal is consistent acrossResources.resx,Resources.Designer.cs, thedocs/files, andindex.md— no orphaned codes. - Better trimming annotations. Replacing blanket
[UnconditionalSuppressMessage]onAndroidTypeManager/AndroidValueManagerwith[RequiresDynamicCode]/[RequiresUnreferencedCode]is the correct, more honest annotation. - Solid incremental-build redesign. The stamp-based sentinel plus
DeleteStaleJavaSources(with the newExecute_DeletesStaleGeneratedJavaSourcestest) closes the stale-JCW gap, and removing the separate post-trim Java pass simplifies the CoreCLR targets. - Deterministic output. The
DeterministicHashBuilderrefactor andCreateDeterministicContentIdare backed byExecute_SameInputs_ProducesByteStableAssembliesand the MVID tests — good reproducibility coverage. - Careful port of the peer-tracking/GC-bridge code and good overall test coverage for the generator and task changes.
CI
Linux build ✅ and license/cla ✅; Windows and Mac builds are still in progress and the overall dotnet-android check is queued — CI is not yet fully green. Please confirm the remaining legs pass before merge.
Reviewed against the dotnet/android review guidelines. Verdict reflects the open behavioral question plus in-progress CI; not a blocker on the design, which looks sound.
Generated by Android PR Reviewer for issue #11617 · 2.5K AIC · ⌖ 85.4 AIC · ⊞ 37.9K
Comment /review to run again
Add association-based trim preservation for each array representation returned by the generated array proxies, including JavaArray/JavaObjectArray and primitive wrapper forms. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Emit primitive array proxy associations only from the Java.Interop typemap assembly to avoid duplicate proxy-map source entries, and restore non-generic JavaConvert list/dictionary conversion behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Emit array proxy preservation associations in the matching __ArrayMapRankN typemap group and request the corresponding proxy maps from the generated loader so trimming observes them without mixing array preservation into the normal Object universe. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace normal JavaPeerProxy TypeMapAssociation reverse lookup with managed-type external TypeMap entries so generated peer proxies are still controlled by the external typemap trim target. Keep alias-holder and array proxy associations unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep rank-scoped array proxy associations for alternate array representations, but make the rank external typemap entries trim on the actual array types so array-returning APIs keep their generated array proxies. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This reverts commit fc65df3.
|
/review |
|
❌ Android PR Reviewer failed. Please review the logs for details. Code review complete for the reflection-free TrimmableTypeMapValueManager/TrimmableTypeMapTypeManager change — no significant issues found. Reviewed all in-scope generator/scanner files under src/Microsoft.Android.Sdk.TrimmableTypeMap/ (DeterministicHashBuilder.cs [new], MetadataHelper.cs, ModelBuilder.cs, TypeMapAssemblyEmitter.cs, RootTypeMapAssemblyGenerator.cs, PEAssemblyBuilder.cs, Model/TypeMapAssemblyData.cs, Scanner/JavaPeerScanner.cs, Scanner/ScannerHashingHelper.cs, TrimmableTypeMapGenerator.cs). Verified: (1) DeterministicHashBuilder is deterministic — little-endian int writes, UTF8 byte-length-prefixed strings (collision-safe), idempotent ToHash(), SHA256 disposed; (2) all content-fingerprint/MVID hashing iterates deterministically ordered collections (SortedDictionary/SortedSet/sorted lists), with module name guaranteeing cross-assembly MVID uniqueness; (3) new array-proxy IL/metadata emission keeps MethodDef/Field lists contiguous, overrides are correctly emitted without NewSlot, and the JavaArrayProxy self-attribute pattern matches the established JavaPeerProxy convention; (4) RootTypeMapAssemblyGenerator's GetOrCreateProxyTypeMapping+Pop is intentional registration with a balanced IL stack; (5) _typeMapAssociationAttrOpenRef is initialized before use; (6) ScannerHashingHelper's CRC64 switch to the exactly-sized byte[] array overload preserves the hash value and is required for correctness (ArrayPool would over-hash); (7) removal of the unused generateTypeMapAssemblies parameter is safe. No banned conventions patterns (null-forgiving !, string.Empty, Array.Empty, static string.IsNullOrEmpty) were introduced. Note: external/Java.Interop submodule was not checked out, so referenced Java array-type assembly names could not be independently confirmed, but they follow existing consistent patterns. |
There was a problem hiding this comment.
This is probably not intentional?
There was a problem hiding this comment.
yeah, copilot changed it for some reason. that needs to be reset. only the java.interop submodule is supposed to be modified.
Avoid reusing cached Java peers that do not implement the collection interface requested by FromJniHandle(). This keeps non-generic and generic Java collection wrappers from casting incompatible bound Java peer types such as Java.Util.ArrayList. Skip generated array-proxy coverage when dynamic code is supported, because CoreCLR trimmable builds do not emit those proxies by default. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Goal
Integrate the Java.Interop value-manager/type-manager split into dotnet/android while keeping the trimmable typemap runtime path reflection-free, AOT-friendly, and covered by targeted tests.
This PR is specifically about making the Android runtime use the new Java.Interop abstractions safely. Broader cleanup such as removing ManagedPeer support or changing NativeAOT runtime policy is intentionally left to follow-up PRs.
Contributes significantly to #10794
Contributes significantly to #11012
Contributes to #8724
What changed
Java.Interop integration
JavaObjectArray<T>.SetElementAt():CreateLocalObjectReferenceArgument(Type type, object? value)returns an owned localJniObjectReferencefor element assignment. Callers must dispose the returned reference.JavaObjectArray<T>.Clear()now directly writes Java null into each slot; it no longer needs value-manager or value-marshaler state.GetValueMarshaler*()forJavaObjectArray<T>element assignment.Runtime manager wiring
ReflectionJniTypeManager/ReflectionJniValueManagerbase implementations.TrimmableTypeMapTypeManagerresolves generated proxy/type-map data without reflection fallback.TrimmableTypeMapValueManagerconstructs and marshals generated peers without the reflection-backed value manager.JavaConvertfallback cases are handled without reflection fallback.JavaMarshalValueManager.csimplementation into per-type files.SimpleValueManagerpath.Trimmable typemap build fixes
Microsoft.Android.Runtime.ManagedTypeMappingfor linker/type-map steps that still need it.[JniAddNativeMethodRegistrationAttribute]diagnostics focused on user assemblies; framework/runtime internal users are handled by generated replacements or intentionally unsupported runtime paths.RequiresUnreferencedCodeannotation fromExportFieldAttribute.[ExportField]is handled by generated trimmable type-map code and does not need to warn simply for constructing the attribute.Test strategy
TrimmableTypeMapUnsupportedvia test categories.[Export]/[ExportField]trimmable codegen,Review notes / intentional non-goals
Type→JniTypeSignaturemapping intentionally keeps the currentType.GetTypeCode+ explicit nullabletypeofchecks. Benchmarking showedNullable.GetUnderlyingType()allocates and should not be used on this path.Local validation
Validated locally on this branch with:
dotnet build external/Java.Interop/src/Java.Interop/Java.Interop.csproj \ -p:Configuration=Debug \ -m:1 \ -nodeReuse:false \ --no-restore \ -v:minimal dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj \ -v minimal \ --no-restoreResults:
562passed).dotnet build src/Mono.Android/Mono.Android.csproj -p:Configuration=Debug -p:AndroidSdkDirectory=/Users/simonrozsival/android-toolchain/sdk -m:1 -nodeReuse:false --no-restore -v:minimalcompiledMono.Android.Runtime.dll; the remaining local failure is Android SDK provisioning (extras/android/m2repository.staginganddocs.stagingmissing), not C# or trim-analyzer errors.Latest update (2026-06-22)
dotnet/androidmaininto this branch.external/xamarin-android-toolsto removeSystem.IO.Hashingfrom BaseTasks file hashing helpers. This avoids MSBuild task-host assembly binding failures when local/system SDKs load differentSystem.IO.Hashing/System.Buffers.Binaryversions.TrimmableJavaProxyObjectinstead of falling back toAndroid.Runtime.JavaObjectfor arbitrary managed objects.JavaConvertpath.TrimmableJavaProxyObjectand unwrap back to the original managed value.mono.android.jar; no duplicate manualjava_runtime_trimmable.jarclass is added.TrimmableJavaProxyObjectderives fromJava.Lang.Objectsojcw-genemits Java overrides forequals,hashCode, andtoStringwhich call back into the C# proxy implementation.Additional local validation:
mono.android.jarcontainsnet/dot/jni/internal/TrimmableJavaProxyObject.class.java_runtime_trimmable.jardoes not contain the trimmable proxy class after cleaning stale intermediates.JavaObjectArray<object>failures were gone. Remaining failures were known/unsupportedGetValueMarshalerCorecases plus one peer-count assertion (JavaObjectTest.UnregisterFromRuntime) for follow-up triage.pm list features/ install session broken pipe), not by compilation or duplicate-class errors.Latest update (2026-06-24): trimmable array proxy generation
The trimmable typemap array path now generates one
JavaArrayProxytype per JNI element name and array rank, and uses per-rank typemap groups to look them up without reflection.For example,
android/net/Networkarrays are emitted conceptually as:The external map key remains the bare JNI element name (
android/net/Network); the rank is encoded by the typemap group (__ArrayMapRank1,__ArrayMapRank2, etc.). The external entry trims on the generated array proxy itself instead of trimming directly onNetwork[], which avoids needing duplicate same-key external typemap entries for every supported managed array representation.Each generated array proxy is self-applied as a
JavaArrayProxyattribute and provides:CreateManagedArray(int length), emitted with directnewarrIL for the requested rank.GetArrayTypes(), emitted with directldtokenentries for every managed representation that should resolve to the same JNI array shape.To keep these proxies when any supported representation is used, the generator now also emits
TypeMapAssociationentries from every type returned byGetArrayTypes()back to the generated array proxy. For non-primitive arrays this includes the CLR array form plus Java.Interop wrappers, for example:Higher ranks use the corresponding nested shapes returned by
GetArrayTypes(), such asNetwork[][],JavaArray<Network>[], andJavaObjectArray<JavaObjectArray<Network>>. Primitive array proxies also get preservation associations forT[],JavaArray<T>,JavaPrimitiveArray<T>, and the concrete primitive wrappers such asJavaSByteArray, with the same rank expansion.At runtime:
JNIEnv.ArrayCreateInstance()callsTrimmableTypeMap.Instance.TryGetArrayProxy(elementType, additionalRank: 1)and thenCreateManagedArray()to allocateT[]withoutArray.CreateInstance()/MakeArrayType()on NativeAOT.TrimmableTypeMapTypeManager.GetTypes()calls the same array-proxy lookup and returnsGetArrayTypes()soT[],JavaArray<T>,JavaObjectArray<T>, primitive wrappers, and nested ranks all resolve through the generated metadata.Additional local validation for this update:
Results:
21passed).2passed).299passed).Mono.Android.NET-Testsproject built successfully.Latest update (2026-06-24): final array proxy trim shape + HelloWorld validation
The generated trimmable typemap code now uses separate mechanisms for normal peer proxies and generated array proxies:
Normal peer proxy lookup
Normal JNI → managed peer entries remain in the normal
Java.Lang.Objecttypemap universe, but managed-type → proxy lookup no longer relies on directTypeMapAssociation<Object>(typeof(ManagedType), typeof(GeneratedProxy))entries.Instead, the generator emits reverse external typemap entries keyed by the managed type name:
SingleUniverseTypeMap.TryGetProxyType()now resolves these__managed_type:keys from the external typemap. This keeps normal peer proxy preservation controlled by external typemap trim targets instead of proxy-map associations.Array proxy lookup and preservation
Array entries remain in per-rank typemap groups. The external array map entry trims on the generated array proxy, while rank-scoped
TypeMapAssociationentries preserve that proxy from every supported managed array representation:The generated root loader now requests both maps for each rank group:
This lets the trimmer observe rank-scoped associations without mixing array-only preservation into the normal
Objectuniverse. Primitive array proxies are emitted only from_Java.Interop.TypeMap, which avoids duplicate proxy-map source keys across shared-universe typemap assemblies.HelloWorld NativeAOT validation
The NativeAOT HelloWorld sample was updated locally to call
ConnectivityManager.GetAllNetworks()after startup. With the latest generated typemap code, the sample successfully marshalsNetwork[]and uses the returned network object:This validates the generated
Android_Net_Network_ArrayProxy1path end-to-end for NativeAOT: rank lookup,Network[]allocation, JNI array copy, and subsequent use of the returnedNetworkinstance.Additional local validation for the final shape:
ModelBuilderTests: passed (135).167).Mono.Android.NET-Testsproject build: passed.TypeMapAssociation<Object>(typeof(Network), typeof(Android_Net_Network_Proxy))is no longer emitted;TypeMap<Object>("__managed_type:Android.Net.Network, Mono.Android", ...)is emitted;TypeMapAssociation<__ArrayMapRank1>(typeof(Network[]), typeof(Android_Net_Network_ArrayProxy1))is emitted;0.