Skip to content

[TrimmableTypeMap] Keep the activation constructor of open generic peers - #13655

Merged
jonathanpeppers merged 1 commit into
mainfrom
dev/simonrozsival/typemap-open-generic-ctor
Nov 19, 2026
Merged

jonathanpeppers merged 1 commit into
mainfrom
dev/simonrozsival/typemap-open-generic-ctor

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Context: #13618

A Java array or collection of a closed generic Java peer, such as JNIEnv.GetArray<GenericPeer<int>> (), fails to activate its elements in trimmed apps: CoreCLR with $(TrimMode)=full and NativeAOT both throw NotSupportedException, while Debug and partial trimming work.

The trimmable type map can't generate a proxy for every closed instantiation, so TrimmableTypeMap.CreateInstance () activates closed generic peers by reflecting over the leaf type's (IntPtr, JniHandleOwnership) constructor. The open generic definition's generated proxy is kept, but nothing references that constructor, so ILLink and ILC remove it and GetConstructor () returns null.

The generated CreateInstance () of an open generic definition's proxy now carries [DynamicDependency ("#ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)", typeof (Target<>))], so the constructor is kept for exactly as long as the proxy is. It is only emitted for leaf types with an XamarinAndroid-style activation constructor, the only shape the reflection path looks for. Keeping the method on the generic definition covers every instantiation for ILLink; ILC honors the same attribute.

The ConstructorParity device tests convert Java arrays of a closed generic peer with reference-type and value-type arguments and fail without the fix.

Validation

  • ./dotnet-local.sh test src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Microsoft.Android.Build.Tasks.Tests.csproj --filter "FullyQualifiedName~GenerateTrimmableTypeMapTests": 64 passed, including the 8 new OpenGenericProxiesKeepTheReflectionActivationConstructor cases, which fail without the emitter change.
  • Mono.Android.NET-Tests in Release ($(TrimMode)=full) with -p:IncludeCategories=ConstructorParity on an API 35 arm64 emulator: 4 passed. Both new tests failed without the fix, on CoreCLR full trimming and on NativeAOT.
  • The GenericPeerArrayElement case added later in this stack checks the same scenario in a standalone app, in all three trimming configurations. It also checks that the [DynamicDependency] doesn't over-preserve: a generic peer the app never uses is removed, and NativeAOT compiles only the instantiations the app uses (GenericArrayElement<int> and <string>, not <long>, <object> or <MainActivity>). ILLink keeps or removes generic definitions, so it can't keep extra instantiations, and the proxy carrying the attribute is only kept when its target type is used.

Context: #13618

A Java array or collection of a closed generic Java peer, such as `JNIEnv.GetArray<GenericPeer<int>> ()`, fails to activate its elements in trimmed apps: CoreCLR with `$(TrimMode)`=full and NativeAOT both throw `NotSupportedException`, while Debug and partial trimming work.

The trimmable type map can't generate a proxy for every closed instantiation, so `TrimmableTypeMap.CreateInstance ()` activates closed generic peers by reflecting over the leaf type's `(IntPtr, JniHandleOwnership)` constructor. The open generic definition's generated proxy is kept, but nothing references that constructor, so ILLink and ILC remove it and `GetConstructor ()` returns null.

The generated `CreateInstance ()` of an open generic definition's proxy now carries `[DynamicDependency ("#ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)", typeof (Target<>))]`, so the constructor is kept for exactly as long as the proxy is. It is only emitted for leaf types with an XamarinAndroid-style activation constructor, the only shape the reflection path looks for. Keeping the method on the generic definition covers every instantiation for ILLink; ILC honors the same attribute.

The `ConstructorParity` device tests convert Java arrays of a closed generic peer with reference-type and value-type arguments and fail without the fix.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 13:28

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 are identified for the targeted constructor-preservation change supporting closed generic Java peer activation.

No open issues 🎉

What changed in this PR

This PR fixes closed generic Java peer activation in fully trimmed and NativeAOT apps by preserving the constructor used by the trimmable type map’s reflection path.

Changes:

  • Emit a targeted DynamicDependency for leaf activation constructors on open generic peers.
  • Add eight generator test cases covering when the dependency should be emitted.
  • Add device regression tests for Java arrays containing generic peers with reference-type and value-type arguments.
File Description
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JnienvTest.cs Tests closed generic peer activation through Java array conversion.
src/​Microsoft.Android.Sdk.TrimmableTypeMap/​Generator/​TypeMapAssemblyEmitter.cs Preserves eligible generic peer constructors with DynamicDependency.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​GenerateTrimmableTypeMapTests.cs Verifies dependency emission conditions and attribute contents.

Review effort: Balanced

@jonathanpeppers

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13655

@github-actions github-actions Bot 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.

Code verdict: looks good — 0 errors, 0 warnings, 1 optional suggestion inline.

Reviewed all three changed files in full and traced the model, attribute-blob encoding, and runtime reflection fallback. The dependency is narrowly scoped to the leaf Xamarin.Android activation constructor on open generic definitions, matching the constructor lookup in TrimmableTypeMap. The metadata test covers the eligibility matrix, and the device regressions exercise both reference-type and value-type generic arguments without directly invoking the activation constructor.

CI: All 39 GitHub checks report success, including the aggregate Azure dotnet-android check for build 1690143. Direct Azure build/timeline verification was blocked because az devops invoke requires authentication in this environment, so that additional verification remains incomplete. I did not rerun the build or device tests locally.

Generated by Android PR Reviewer for #13655 · copilot · gpt61 · 39.1 AIC · ⌖ 7.48 AIC · ⊞ 20.3K
Comment /review to run again

{
// TrimmableTypeMap.CreateInstance () activates closed instantiations of an open generic peer by reflecting
// over the leaf type's (IntPtr, JniHandleOwnership) constructor, so the generated proxy must keep it.
string managedTypeName = generic ? "Example.Peer`1" : "Example.Peer";

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.

🤖 💡 Testing — Consider adding a nested generic peer case, such as Example.Outer+Peer1, and checking the decoded dependency target's declaring type and assembly scope. The emitter serializes the dependency's target as an assembly-qualified name rather than using ResolveTypeRef`, so this would cover the nested-type naming boundary that the current top-level peer cases do not exercise.

{Rule: Test edge cases; verify serialized names at the consumer.}

@jonathanpeppers
jonathanpeppers merged commit 78ef747 into main Nov 19, 2026
39 checks passed
@jonathanpeppers
jonathanpeppers deleted the dev/simonrozsival/typemap-open-generic-ctor branch November 19, 2026 21:49
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.

3 participants