Commit d5b2ece
[Android.Runtime] Use JniTypeManager to register natives (#1550)
The new code uses `JniTypeManager`'s `RegisterNativeMembers()` method
to register native marshaling methods. The type manager itself uses
the register method generated by the `jnimarshalmethod-gen.exe` tool
or method(s) on the given type, which have the
`[Java.Interop.JniAddNativeMethodRegistrationAttribute]`
custom attribute.
When no such `__<$>_jni_marshal_methods.__RegisterNativeMembers()`
method exists, we fallback to the original (old) code.
Example register class generated by `jnimarshalmethod-gen.exe`:
partial class MainActivity {
partial class '__<$>_jni_marshal_methods' {
[JniAddNativeMethodRegistration]
public static void __RegisterNativeMembers (JniNativeMethodRegistrationArguments args)
{
args.AddRegistrations (new JniNativeMethodRegistration[] {
new JniNativeMethodRegistration (
"n_onCreate",
"(Landroid/os/Bundle;)V",
new Action<IntPtr, IntPtr, IntPtr> (MainActivity.__<$>_jni_marshal_methods.n_onCreate_Landroid_os_Bundle_))
});
}
public static void n_onCreate_Landroid_os_Bundle_ (IntPtr __jnienv, IntPtr __this, IntPtr bundle)
{
var __envp = new JniTransition (__this);
JniRuntime __jvm;
try {
__jvm = JniEnvironment.Runtime;
__jvm.ValueManager.WaitForGCBridgeProcessing();
var __this_val = __jvm.ValueManager.GetValue<MainActivity>(__this);
var __bundle_val = __jvm.ValueManager.GetValue<Bundle>(bundle);
__this_val.OnCreate (__bundle_val);
}
catch (Exception __e) if (__jvm.ExceptionShouldTransitionToJni(__e)) {
__envp.SetPendingException (__e);
}
finally {
__envp.Dispose ();
}
}
}
}
Note that `jnimarshalmethod-gen.exe` will generate at
*packaging-time* what is currently done at *runtime* within
`JNINativeWrapper` using `System.Reflection.Emit`. This avoids the
need to use `System.Reflection.Emit` during process startup, so long
as the type contains `jnimarshalmethod-gen.exe`-generated types.
Note that `AndroidValueManager` needs to override `PeekPeer()`, as
the `__jvm.ValueManager.GetValue<T>()` invocation uses
`AndroidValueManager.PeekPeer()` as part of its operation:
android.runtime.JavaProxyThrowable: System.NotImplementedException: The method or operation is not implemented.
at Android.Runtime.AndroidValueManager.PeekPeer (Java.Interop.JniObjectReference reference) [0x00001]
at Java.Interop.JniRuntime+JniValueManager.PeekValue (Java.Interop.JniObjectReference reference) [0x0002f]
at Java.Interop.JniRuntime+JniValueManager.GetValue[T] (Java.Interop.JniObjectReference& reference, Java.Interop.JniObjectReferenceOptions options, System.Type targetType)
at Java.Interop.JniRuntime+JniValueManager.GetValue[T] (System.IntPtr handle) [0x0000a]
at xatemplateaot.MainActivity+__<$>_jni_marshal_methods.n_onCreate_Landroid_os_Bundle_ (System.IntPtr __jnienv, System.IntPtr __this, System.IntPtr savedInstanceState) [0x0001e]
at md58018c7d08c228c8e2a3e03723c59ca27.MainActivity.n_onCreate(Native Method)
at md58018c7d08c228c8e2a3e03723c59ca27.MainActivity.onCreate(MainActivity.java:29)1 parent efe81f5 commit d5b2ece
File tree
2 files changed
+67
-59
lines changed- src/Mono.Android/Android.Runtime
2 files changed
+67
-59
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
225 | 227 | | |
226 | 228 | | |
227 | 229 | | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
228 | 286 | | |
229 | 287 | | |
230 | 288 | | |
| |||
246 | 304 | | |
247 | 305 | | |
248 | 306 | | |
249 | | - | |
| 307 | + | |
250 | 308 | | |
251 | 309 | | |
252 | 310 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | 16 | | |
23 | 17 | | |
24 | 18 | | |
| |||
61 | 55 | | |
62 | 56 | | |
63 | 57 | | |
| 58 | + | |
| 59 | + | |
64 | 60 | | |
65 | 61 | | |
66 | 62 | | |
| |||
116 | 112 | | |
117 | 113 | | |
118 | 114 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | 115 | | |
140 | 116 | | |
141 | 117 | | |
| |||
156 | 132 | | |
157 | 133 | | |
158 | 134 | | |
159 | | - | |
| 135 | + | |
| 136 | + | |
160 | 137 | | |
161 | | - | |
162 | | - | |
163 | | - | |
| 138 | + | |
| 139 | + | |
164 | 140 | | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 141 | + | |
192 | 142 | | |
193 | 143 | | |
194 | 144 | | |
| |||
241 | 191 | | |
242 | 192 | | |
243 | 193 | | |
244 | | - | |
| 194 | + | |
245 | 195 | | |
246 | 196 | | |
247 | 197 | | |
| |||
0 commit comments