Commit aa0f6d9
[TrimmableTypeMap] Implement runtime TypeManager, ValueManager, and JavaConvert (#10967)
Fixes: #10791
## Summary
Adds the runtime-side support for the trimmable typemap: type resolution, peer creation, native method registration, and AOT-safe collection marshaling. All behind `RuntimeFeature.TrimmableTypeMap` (defaults to `false`).
### New runtime types
- **`JavaPeerProxy` / `JavaPeerProxy<T>`** — AOT-safe proxy attribute base. Generated proxy types extend this and provide `CreateInstance()` for peer creation and `GetContainerFactory()` for collection marshaling.
- **`IAndroidCallableWrapper`** — `RegisterNatives(JniType)` interface for ACW proxy types to register JNI native methods.
- **`JavaPeerContainerFactory`** — AOT-safe factories for arrays, lists, collections, and dictionaries without `MakeGenericType()`.
- **`TrimmableTypeMap`** — Central class owning the `TypeMapping` dictionary. Encapsulates all proxy attribute access: peer creation, invoker resolution, container factories, and native method registration bootstrap.
- **`TrimmableTypeMapTypeManager`** — `JniTypeManager` subclass delegating type lookups to `TrimmableTypeMap`. `RegisterNativeMembers` throws `UnreachableException` (JCW static blocks handle registration).
### Type resolution
- `GetProxyForManagedType(Type)` resolves managed type → JNI name via `IJniNameProviderAttribute` (shared interface for `[Register]` and `[JniTypeSignature]`) → TypeMap dictionary → proxy. Results cached in `ConcurrentDictionary`.
- `TryGetJniNameForType` shared between `TrimmableTypeMap` and `TrimmableTypeMapTypeManager`.
- `ActivateInstance` uses JNI `GetObjectClass` → `GetJniTypeNameFromClass` → TypeMap lookup for constructor activation (proxy types have self-applied attribute, not the target type).
### TypeMap dictionary initialization
- The runtime's `GetOrCreateExternalTypeMapping<Java.Lang.Object>()` handles assembly resolution automatically via the `System.Runtime.InteropServices.TypeMappingEntryAssembly` runtimeconfig property (set in `Trimmable.targets`). No manual `Assembly.Load` pre-loading is needed — the runtime walks `TypeMapAssemblyTargetAttribute` attributes from the entry assembly recursively.
### Native interop refactoring
- **`RegisterJniNatives` passed via init args** — `Initialize` sets `args->registerJniNativesFn` (null when `TrimmableTypeMap=true`). Eliminates the `create_delegate` call for `RegisterJniNatives`, letting the trimmer remove it cleanly in the trimmable path.
- **C++ `registerNatives` stub** — `Host::Java_mono_android_Runtime_registerNatives` no-op for the trimmable path (managed code handles registration via `Initialize`).
- **Null guard** on `jnienv_register_jni_natives` call site.
- `Initialize` is now the **single** `create_delegate` entry point from native code.
### RegisterNatives bootstrap
- `mono.android.Runtime.registerNatives(Class)` Java native method added
- `TrimmableTypeMap.Initialize()` registers the JNI callback during init (behind explicit `if (RuntimeFeature.TrimmableTypeMap)` guard for trimmer compatibility)
- When Java loads a JCW class, `OnRegisterNatives` resolves the proxy and calls `IAndroidCallableWrapper.RegisterNatives()` to bind UCO function pointers
### AOT-safe JavaConvert
- `JavaConvert.GetJniHandleConverter()` uses `JavaPeerContainerFactory` for `IList`, `ICollection`, `IDictionary`
- `JNIEnv.ArrayCreateInstance()` uses factory path
### Peer creation
- `JavaMarshalValueManager` (renamed from `ManagedValueManager`) overrides `CreatePeer` for the trimmable path — calls `proxy.CreateInstance()` directly, bypassing `GetUninitializedObject`
- `GetSimpleReferences` walks base type chain for managed-only subclasses without `[Register]`
### Wiring
- `RuntimeFeature.TrimmableTypeMap` feature switch with ILLink substitutions
- `JNIEnvInit` (CoreCLR) and `JavaInteropRuntime` + `JreRuntime` (NativeAOT) create the new managers when the feature is on
- `JavaMarshalValueManager` (renamed from `ManagedValueManager`) gets proxy-based peer creation in `TryConstructPeer`
### Dependencies
- dotnet/java-interop#1391 (RegisterNatives with raw function pointers — follow-up optimization)
- Manifest generator: #10991
- Build pipeline: #10980
### Test coverage
- 255/255 generator tests pass
- End-to-end HelloWorld app runs on device with Activity, layout XML, FindViewById\<Button\>, Click events
- Runtime tests require device integration (tracked by #10793)
### Trimmer size regression prevention
- Add ILLink substitutions for `IsMonoRuntime` and `IsCoreClrRuntime` — enables trimmer to eliminate runtime-specific branches
- `JavaMarshalValueManager`: remove `TrimmableTypeMap?` field, use `TrimmableTypeMap.Instance` singleton
- `TrimmableTypeMap`: private ctor, `Initialize()` static method, `Instance` throws if not initialized
- `TrimmableTypeMapTypeManager`: no constructor parameter, uses singleton
- No `TrimmableTypeMap` type references outside feature guards — clean trimming for MonoVM/NativeAOT
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>1 parent 7f6ed40 commit aa0f6d9
File tree
19 files changed
+695
-57
lines changed- src
- Microsoft.Android.Runtime.NativeAOT
- Android.Runtime.NativeAOT
- Java.Interop
- Mono.Android
- Android.Runtime
- ILLink
- Java.Interop
- Microsoft.Android.Runtime
- Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base
- java-runtime/java/mono/android
- native
- clr
- host
- include/host
- common/include
19 files changed
+695
-57
lines changedLines changed: 13 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
67 | | - | |
68 | | - | |
| 69 | + | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| |||
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
89 | 100 | | |
Lines changed: 11 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
78 | 87 | | |
79 | 88 | | |
80 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
32 | 37 | | |
| 38 | + | |
33 | 39 | | |
34 | 40 | | |
35 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
85 | 89 | | |
86 | 90 | | |
87 | 91 | | |
| |||
130 | 134 | | |
131 | 135 | | |
132 | 136 | | |
133 | | - | |
134 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
135 | 142 | | |
136 | 143 | | |
137 | 144 | | |
138 | 145 | | |
139 | 146 | | |
140 | 147 | | |
141 | 148 | | |
142 | | - | |
| 149 | + | |
| 150 | + | |
143 | 151 | | |
144 | 152 | | |
145 | 153 | | |
| |||
152 | 160 | | |
153 | 161 | | |
154 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
155 | 167 | | |
156 | 168 | | |
157 | 169 | | |
| |||
165 | 177 | | |
166 | 178 | | |
167 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
168 | 184 | | |
169 | 185 | | |
170 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | 7 | | |
12 | 8 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
82 | 90 | | |
83 | 91 | | |
84 | 92 | | |
| |||
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
104 | 158 | | |
105 | 159 | | |
106 | 160 | | |
| |||
Lines changed: 114 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
0 commit comments