Commit aba2726
authored
Context: xamarin/monodroid@ed984a3
Context: dotnet/java-interop#23
Update `JNIEnv.FindClass()` to use `JniEnvironment.Types.FindClass()`.
This avoids the problem of `JNIEnvInit.mid_Class_forName` being
`null` in a NativeAOT context:
E AndroidRuntime: Process: net.dot.hellonativeaot, PID: 30744
E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.TypeInitializationException: TypeInitialization_Type_NoTypeAvailable
E AndroidRuntime: ---> System.ArgumentNullException: ArgumentNull_Generic Arg_ParamName_Name, method
E AndroidRuntime: at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference, JniMethodInfo, JniArgumentValue*) + 0x1c8
E AndroidRuntime: at Android.Runtime.JNIEnv.FindClass(String) + 0xb4
E AndroidRuntime: at Java.Lang.Class..cctor() + 0x7c
E AndroidRuntime: at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb4
E AndroidRuntime: Exception_EndOfInnerExceptionStack
E AndroidRuntime: at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x160
E AndroidRuntime: at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnNonGCStaticBase(StaticClassConstructionContext*, IntPtr) + 0x14
E AndroidRuntime: at Android.Runtime.JNIEnv.FindClass(String) + 0xc0
E AndroidRuntime: at Android.App.Application.get_Context() + 0x50
E AndroidRuntime: at Microsoft.Maui.Hosting.EssentialsExtensions.<>c.<UseEssentials>b__0_0(ILifecycleBuilder life) + 0x18
E AndroidRuntime: at Microsoft.Maui.LifecycleEvents.LifecycleEventService..ctor(IEnumerable`1) + 0x94
E AndroidRuntime: at Microsoft.Maui.LifecycleEvents.MauiAppHostBuilderExtensions.<>c.<ConfigureLifecycleEvents>b__0_0(IServiceProvider sp) + 0x3c
E AndroidRuntime: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite, RuntimeResolverContext) + 0x68
E AndroidRuntime: at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier) + 0x180
E AndroidRuntime: at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0x11c
E AndroidRuntime: at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier, ServiceProviderEngineScope) + 0x44
E AndroidRuntime: at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type) + 0x48
E AndroidRuntime: at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type) + 0x48
E AndroidRuntime: at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider) + 0x3c
E AndroidRuntime: at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.<GetLifecycleEventDelegates>d__3`1.MoveNext() + 0x38
E AndroidRuntime: at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[TDelegate](IServiceProvider, Action`1) + 0x68
E AndroidRuntime: at Microsoft.Maui.MauiApplication.OnCreate() + 0xb0
E AndroidRuntime: at Android.App.Application.n_OnCreate(IntPtr jnienv, IntPtr native__this) + 0xb0
E AndroidRuntime: at my.MainApplication.n_onCreate(Native Method)
E AndroidRuntime: at my.MainApplication.onCreate(MainApplication.java:24)
E AndroidRuntime: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1386)
E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7398)
E AndroidRuntime: at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2379)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)
E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:232)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:317)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8592)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
This also allows us to remove these fields that are now unused:
* `JnienvInitializeArgs.Class_forName`
* `JNIEnv.BinaryName()`
TODO? While `JNIEnv.FindClass()` was using [`Class.forName()`][0],
`JniEnvironment.Types.FindClass()` uses [`ClassLoader.loadClass()`][1].
`JNIEnv.FindClass()` originally used `ClassLoader.loadClass()`, but
was changed to use `Class.forName()` in xamarin/monodroid@ed984a3a as
part of supporting the Xamarin.Android Designer.
Updating `JNIEnv.FindClass()` to use `JniEnvironment.Types.FindClass()`
effectively reverts that original change. This is "fine" for now --
the Xamarin.Android Designer is no longer supported -- but in the
future we may want to update `JniEnvironment.Types.FindClass()` to
use `Class.forName()`; see dotnet/java-interop#23.
[0]: https://developer.android.com/reference/java/lang/Class#forName(java.lang.String)
[1]: https://developer.android.com/reference/java/lang/ClassLoader#loadClass(java.lang.String)
1 parent a9299a2 commit aba2726
File tree
4 files changed
+2
-51
lines changed- src
- Mono.Android/Android.Runtime
- native/mono/monodroid
4 files changed
+2
-51
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
| 283 | + | |
287 | 284 | | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
| 285 | + | |
329 | 286 | | |
330 | 287 | | |
331 | 288 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
| |||
46 | 45 | | |
47 | 46 | | |
48 | 47 | | |
49 | | - | |
50 | 48 | | |
51 | 49 | | |
52 | 50 | | |
| |||
100 | 98 | | |
101 | 99 | | |
102 | 100 | | |
103 | | - | |
104 | | - | |
105 | 101 | | |
106 | 102 | | |
107 | 103 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | 92 | | |
94 | 93 | | |
95 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
860 | 860 | | |
861 | 861 | | |
862 | 862 | | |
863 | | - | |
864 | 863 | | |
865 | 864 | | |
866 | 865 | | |
| |||
0 commit comments