Repository navigation
[release/11.0.1xx] Preserve generic peers during collection CopyTo - #13803
Open
simonrozsival wants to merge 1 commit into
Open
simonrozsival wants to merge 1 commit into
simonrozsival wants to merge 1 commit into
Conversation
simonrozsival
marked this pull request as ready for review
November 26, 2026 18:19
Copilot stopped reviewing on behalf of
simonrozsival due to an error
November 26, 2026 18:22
Contributor
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Copilot review overview
4 open issues
TheDynamicDependencyAttributemember signature for constructors is typically expressed as… · NewGetDynamicDependencyAttrCtorRef()defines theDynamicDependencyAttributeconstructor overload… · NewGetDynamicDependencyAttrCtorRef()defines theDynamicDependencyAttributeconstructor overload… · New TheDynamicallyAccessedMembers(Constructors)usage relies on aConstructorssymbol being in… · New
What changed in this PR
This PR updates Android runtime/type-map activation to better support trimming and generic peer activation, while improving JNI local reference safety.
Changes:
- Add trimming annotations/suppressions to enable creating peers for closed generic element types via
TrimmableTypeMap.CreateInstance() - Ensure JNI local references are always released in
JavaCollection.CopyTo() - Update the trimmable type-map generator to preserve activation constructors for generic definitions via
DynamicDependency
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Resources/LayoutBinding.cs | Adds DynamicallyAccessedMembers annotations to fragment lookup to improve trimming correctness. |
| src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMap.cs | Removes a specialized CreateInstance path that avoided reflection fallback. |
| src/Mono.Android/Java.Interop/JavaConvert.cs | Switches array element activation to TrimmableTypeMap.CreateInstance() with a trimming-warning suppression. |
| src/Mono.Android/Android.Runtime/JavaCollection.cs | Wraps JNI array handling in try/finally to always delete the local ref. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/TypeMapAssemblyEmitter.cs | Emits DynamicDependency to keep activation constructors for generic definitions and refactors CreateInstance body emission to return a handle. |
Review effort: Lite
| if (proxy.ActivationCtor is { IsOnLeafType: true, Style: ActivationCtorStyle.XamarinAndroid }) { | ||
| _pe.Metadata.AddCustomAttribute (createInstance, GetDynamicDependencyAttrCtorRef (), | ||
| _pe.BuildAttributeBlob ( | ||
| "#ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)", |
Comment on lines
+873
to
+876
| _pe.Metadata.AddCustomAttribute (createInstance, GetDynamicDependencyAttrCtorRef (), | ||
| _pe.BuildAttributeBlob ( | ||
| "#ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)", | ||
| $"{proxy.TargetType.ManagedTypeName}, {proxy.TargetType.AssemblyName}")); |
Comment on lines
+880
to
895
| MemberReferenceHandle GetDynamicDependencyAttrCtorRef () | ||
| { | ||
| if (_dynamicDependencyAttrCtorRef.IsNil) { | ||
| var metadata = _pe.Metadata; | ||
| var attrRef = metadata.AddTypeReference (_pe.SystemRuntimeRef, | ||
| metadata.GetOrAddString ("System.Diagnostics.CodeAnalysis"), metadata.GetOrAddString ("DynamicDependencyAttribute")); | ||
| _dynamicDependencyAttrCtorRef = _pe.AddMemberRef (attrRef, ".ctor", | ||
| sig => sig.MethodSignature (isInstanceMethod: true).Parameters (2, | ||
| rt => rt.Void (), | ||
| p => { | ||
| p.AddParameter ().Type ().String (); | ||
| p.AddParameter ().Type ().Type (_systemTypeRef, false); | ||
| })); | ||
| } | ||
| return _dynamicDependencyAttrCtorRef; | ||
| } |
Comment on lines
+111
to
+114
| protected T FindFragment< | ||
| [DynamicallyAccessedMembers (Constructors)] | ||
| T | ||
| > (int resourceId, global::AndroidX.Fragment.App.Fragment? __ignoreMe, ref T? cachedField) |
|
Reviewed as PeppersMuse. No issues found in the change itself. What I checked:
|
7 tasks
This branch has not been deployed
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.


Context: #13795
Context: #13655
Backport of #13795 to .NET 11, without its new tests (the
TrimmingTestsproject does not exist on this branch, and the AndroidX regression build test is dropped as requested).Closed-generic collection CopyTo
Non-generic
JavaCollection.CopyToandJavaList.CopyToreceive the destination's runtime element type. With the trimmable type map, closed-generic Java peer element types were rejected withNotSupportedException. Activate them throughTrimmableTypeMap.CreateInstance, and release the temporary JNI array reference if conversion fails.On this branch,
CreateInstancereflects over the closed type's(IntPtr, JniHandleOwnership)constructor and requires a DAM-annotated type. The array element type carries no annotation, so the call goes through a helper with an IL2067 suppression. The suppression relies on the emitter change from #13655, also backported here without its tests: the generatedCreateInstanceof an open generic definition's proxy carries[DynamicDependency]on that constructor, so ILLink and ILC keep it for as long as the proxy is kept. Without it, the constructor is trimmed and activation returns null, which surfaces as the existingNotSupportedException.AndroidX layout-binding fragments
The AndroidX
FindFragment<T>overload uses a genericJavaCast<T>result but did not propagate the constructor-preservation requirement toT, producing IL2091 for layout bindings. Propagate the public and non-public constructor annotation.Validation
dotnet build src/Microsoft.Android.Sdk.TrimmableTypeMap/Microsoft.Android.Sdk.TrimmableTypeMap.csproj: succeeded, 0 warnings.dotnet test tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj: 1080 passed.Mono.Android) and device tests, pending shared build/emulator admission. Onmain, the same runtime change passed the closed-genericCopyTocase on CoreCLR full trimming, CoreCLR partial trimming, and NativeAOT ([Mono.Android] Preserve generic peers during collection CopyTo #13795).