-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathJavaInteropRuntime.cs
More file actions
68 lines (59 loc) · 2.24 KB
/
JavaInteropRuntime.cs
File metadata and controls
68 lines (59 loc) · 2.24 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
using Android.Runtime;
using Java.Interop;
using System.Runtime.InteropServices;
namespace Microsoft.Android.Runtime;
static partial class JavaInteropRuntime
{
static JniRuntime? runtime;
[UnmanagedCallersOnly (EntryPoint="JNI_OnLoad")]
static int JNI_OnLoad (IntPtr vm, IntPtr reserved)
{
try {
AndroidLog.Print (AndroidLogLevel.Info, "JavaInteropRuntime", "JNI_OnLoad()");
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 ();
}
// 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)
{
JniTransition transition = default;
try {
var settings = new DiagnosticSettings ();
settings.AddDebugDotnetLog ();
var typeManager = new ManagedTypeManager ();
var options = new NativeAotRuntimeOptions {
EnvironmentPointer = jnienv,
ClassLoader = new JniObjectReference (classLoader),
TypeManager = typeManager,
ValueManager = new ManagedValueManager (),
UseMarshalMemberBuilder = false,
JniGlobalReferenceLogWriter = settings.GrefLog,
JniLocalReferenceLogWriter = settings.LrefLog,
JniAddNativeMethodRegistrationAttributePresent = true,
};
runtime = options.CreateJreVM ();
// Entry point into Mono.Android.dll
JNIEnvInit.InitializeJniRuntime (runtime);
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 ();
}
}