-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathJavaInteropRuntime.cs
More file actions
89 lines (75 loc) · 3.38 KB
/
JavaInteropRuntime.cs
File metadata and controls
89 lines (75 loc) · 3.38 KB
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
using Android.Runtime;
using Java.Interop;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Microsoft.Android.Runtime;
static partial class JavaInteropRuntime
{
static JniRuntime? runtime;
[LibraryImport ("xa-internal-api")]
[UnmanagedCallConv (CallConvs = new[] { typeof (CallConvCdecl) })]
private static partial int XA_Host_NativeAOT_JNI_OnLoad (IntPtr vm, IntPtr reserved);
[UnmanagedCallersOnly (EntryPoint="JNI_OnLoad")]
static int JNI_OnLoad (IntPtr vm, IntPtr reserved)
{
try {
AndroidLog.Print (AndroidLogLevel.Info, "JavaInteropRuntime", "JNI_OnLoad()");
XA_Host_NativeAOT_JNI_OnLoad (vm, reserved);
// This must be called before anything else, otherwise we'll see several spurious GC invocations and log messages
// similar to:
//
// 09-15 14:51:01.311 11071 11071 D monodroid-gc: 1 outstanding GREFs. Performing a full GC!
//
JNIEnvInit.NativeAotInitializeMaxGrefGet ();
LogcatTextWriter.Init ();
return (int) JniVersion.v1_6;
}
catch (Exception e) {
AndroidLog.Print (AndroidLogLevel.Error, "JavaInteropRuntime", $"JNI_OnLoad() failed: {e}");
return 0;
}
}
[UnmanagedCallersOnly (EntryPoint="JNI_OnUnload")]
static void JNI_OnUnload (IntPtr vm, IntPtr reserved)
{
AndroidLog.Print(AndroidLogLevel.Info, "JavaInteropRuntime", "JNI_OnUnload");
runtime?.Dispose ();
}
[LibraryImport ("xa-internal-api")]
[UnmanagedCallConv (CallConvs = new[] { typeof (CallConvCdecl) })]
private static partial void XA_Host_NativeAOT_OnInit (IntPtr language, IntPtr filesDir, IntPtr cacheDir, ref JNIEnvInit.JnienvInitializeArgs initArgs);
// symbol name from `$(IntermediateOutputPath)obj/Release/osx-arm64/h-classes/net_dot_jni_hello_JavaInteropRuntime.h`
[UnmanagedCallersOnly (EntryPoint="Java_net_dot_jni_nativeaot_JavaInteropRuntime_init")]
static void init (IntPtr jnienv, IntPtr klass, IntPtr classLoader, IntPtr language, IntPtr filesDir, IntPtr cacheDir)
{
JniTransition transition = default;
try {
var initArgs = new JNIEnvInit.JnienvInitializeArgs ();
// This needs to be called first, since it sets up locations, environment variables, logging etc
XA_Host_NativeAOT_OnInit (language, filesDir, cacheDir, ref initArgs);
JNIEnvInit.InitializeJniRuntimeEarly (initArgs);
var settings = new DiagnosticSettings ();
settings.AddDebugDotnetLog ();
var options = new NativeAotRuntimeOptions {
EnvironmentPointer = jnienv,
ClassLoader = new JniObjectReference (classLoader, JniObjectReferenceType.Global),
TypeManager = new ManagedTypeManager (),
ValueManager = ManagedValueManager.Instance,
UseMarshalMemberBuilder = false,
JniGlobalReferenceLogWriter = settings.GrefLog,
JniLocalReferenceLogWriter = settings.LrefLog,
};
runtime = options.CreateJreVM ();
// Entry point into Mono.Android.dll. Log categories are initialized in JNI_OnLoad.
JNIEnvInit.InitializeJniRuntime (runtime, initArgs);
transition = new JniTransition (jnienv);
var handler = Java.Lang.Thread.DefaultUncaughtExceptionHandler;
Java.Lang.Thread.DefaultUncaughtExceptionHandler = new UncaughtExceptionMarshaler (handler);
}
catch (Exception e) {
AndroidLog.Print (AndroidLogLevel.Error, "JavaInteropRuntime", $"JavaInteropRuntime.init: error: {e}");
transition.SetPendingException (e);
}
transition.Dispose ();
}
}