Repository navigation
[TrimmableTypeMap] Keep the activation constructor of open generic peers - #13655
Conversation
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>
There was a problem hiding this comment.
🟢 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
DynamicDependencyfor 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
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
🤖 💡 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.}
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 throwNotSupportedException, 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 andGetConstructor ()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
ConstructorParitydevice 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 newOpenGenericProxiesKeepTheReflectionActivationConstructorcases, which fail without the emitter change.Mono.Android.NET-Testsin Release ($(TrimMode)=full) with-p:IncludeCategories=ConstructorParityon an API 35 arm64 emulator: 4 passed. Both new tests failed without the fix, on CoreCLR full trimming and on NativeAOT.GenericPeerArrayElementcase 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.