Skip to content

Commit f45700f

Browse files
Bump to dotnet/java-interop@9dea87dc; FindClass & TypeManager (#9812)
Changes: dotnet/java-interop@f30e420...9dea87d * dotnet/java-interop@9dea87dc: [Java.Interop] .GetTypeSignature() supports unsigned types (dotnet/java-interop#1312) * dotnet/java-interop@1cfb4f4d: [generator] Add support for emitting `[UnsupportedOSPlatform]` (dotnet/java-interop#1307) Context: #9811 The .NET MAUI template + NativeAOT currently crashes with: E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.InvalidProgramException: InvalidProgram_Specific, IntPtr Android.Runtime.JNIEnv.monodroid_typemap_managed_to_java(System.Type, Byte*) E AndroidRuntime: at Internal.Runtime.TypeLoaderExceptionHelper.CreateInvalidProgramException(ExceptionStringID, String) + 0x4c E AndroidRuntime: at Android.Runtime.JNIEnv.monodroid_typemap_managed_to_java(Type, Byte*) + 0x18 E AndroidRuntime: at Android.Runtime.JNIEnv.TypemapManagedToJava(Type) + 0x104 E AndroidRuntime: at Android.Runtime.JNIEnv.GetJniName(Type) + 0x1c E AndroidRuntime: at Android.Runtime.JNIEnv.FindClass(Type) + 0x38 E AndroidRuntime: at Android.Runtime.JNIEnv.NewArray(IJavaObject[]) + 0x28 E AndroidRuntime: at Android.Runtime.JNIEnv.NewArray[T](T[]) + 0x94 E AndroidRuntime: at Android.Graphics.Drawables.LayerDrawable..ctor(Drawable[] layers) + 0xd4 E AndroidRuntime: at Microsoft.Maui.Platform.MauiRippleDrawableExtensions.UpdateMauiRippleDrawableBackground(View, Paint, IButtonStroke, Func`1, Func`1, Action) + 0x2ac This appears to be related to array usage, such as `LayerDrawable.ctor(Drawable[])` in this example. I can reproduce the same crash using a `ColorStateList.ctor(int[][], int[])` in `samples/NativeAOT`: E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.InvalidProgramException: InvalidProgram_Specific, IntPtr Android.Runtime.JNIEnv.monodroid_typemap_managed_to_java(System.Type, Byte*) E AndroidRuntime: at Internal.Runtime.TypeLoaderExceptionHelper.CreateInvalidProgramException(ExceptionStringID, String) + 0x4c E AndroidRuntime: at Android.Runtime.JNIEnv.monodroid_typemap_managed_to_java(Type, Byte*) + 0x18 E AndroidRuntime: at Android.Runtime.JNIEnv.TypemapManagedToJava(Type) + 0x104 E AndroidRuntime: at Android.Runtime.JNIEnv.GetJniName(Type) + 0x1c E AndroidRuntime: at Android.Runtime.JNIEnv.FindClass(Type) + 0x38 E AndroidRuntime: at Android.Runtime.JNIEnv.NewArray[T](T[]) + 0xa8 E AndroidRuntime: at Android.Content.Res.ColorStateList..ctor(Int32[][], Int32[]) + 0xdc E AndroidRuntime: at NativeAOT.MainActivity.OnCreate(Bundle savedInstanceState) + 0xb8 Update `JNIEnv.FindClass(Type)` to go through `TypeManager` instead of using `TypemapManagedToJava`. This avoids the P/Invoke which is causing the crash (f800c1a). Note that we can't directly use `JniRuntime.JniTypeManager.GetTypeSignature()`, as the previous use of `JavaNativeTypeManager.ToJniName(Type)` would default to using `java/lang/Object` if there was no typemap entry for `type`. After this change, the sample works and prints a log message indicating `ColorStateList` is created successfully: D NativeAOT: MainActivity.OnCreate() ColorStateList: ColorStateList{mThemeAttrs=nullmChangingConfigurations=0mStateSpecs=[[0, 1]]mColors=[0, 1]mDefaultColor=0}
1 parent 32c8bf6 commit f45700f

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

samples/NativeAOT/MainActivity.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Android.Content.Res;
12
using Android.Runtime;
23
using Android.Util;
34
using System.Reflection;
@@ -17,5 +18,9 @@ protected override void OnCreate(Bundle? savedInstanceState)
1718

1819
// Set our view from the "main" layout resource
1920
SetContentView(Resource.Layout.activity_main);
21+
22+
// An example of an Android API that uses a Java array
23+
var list = new ColorStateList (new int[][] { [ 0, 1 ]}, [0, 1]);
24+
Log.Debug ("NativeAOT", "MainActivity.OnCreate() ColorStateList: " + list);
2025
}
2126
}

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,16 @@ public static IntPtr FindClass (System.Type type)
256256
{
257257
int rank = JavaNativeTypeManager.GetArrayInfo (type, out type);
258258
try {
259-
return FindClass (JavaNativeTypeManager.ToJniName (GetJniName (type), rank));
259+
var sig = JNIEnvInit.androidRuntime?.TypeManager.GetTypeSignature (type) ?? default;
260+
if (!sig.IsValid || sig.SimpleReference == null) {
261+
sig = new JniTypeSignature ("java/lang/Object");
262+
}
263+
sig = sig.AddArrayRank (rank);
264+
265+
JniObjectReference local_ref = JniEnvironment.Types.FindClass (sig.Name);
266+
IntPtr global_ref = local_ref.NewGlobalRef ().Handle;
267+
JniObjectReference.Dispose (ref local_ref);
268+
return global_ref;
260269
} catch (Java.Lang.Throwable e) {
261270
if (!((e is Java.Lang.NoClassDefFoundError) || (e is Java.Lang.ClassNotFoundException)))
262271
throw;

0 commit comments

Comments
 (0)