From bae53be2d53cf67d895c03fdbbe3d64e488ef42a Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Fri, 21 Feb 2025 14:41:56 -0600 Subject: [PATCH] [Mono.Android] use `GetJniTypeNameFromInstance()` (#9820) Context: https://github.com/dotnet/android/pull/9747 A .NET MAUI + NativeAOT project fails with: E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.DllNotFoundException: DllNotFound_Linux, xa-internal-api, E AndroidRuntime: dlopen failed: library "xa-internal-api.so" not found E AndroidRuntime: dlopen failed: library "libxa-internal-api.so" not found E AndroidRuntime: dlopen failed: library "xa-internal-api" not found E AndroidRuntime: dlopen failed: library "libxa-internal-api" not found E AndroidRuntime: E AndroidRuntime: at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x4c E AndroidRuntime: at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0xe4 E AndroidRuntime: at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x40 E AndroidRuntime: at Android.Runtime.RuntimeNativeMethods.monodroid_TypeManager_get_java_class_name(IntPtr) + 0x2c E AndroidRuntime: at Java.Interop.TypeManager.GetClassName(IntPtr) + 0x10 E AndroidRuntime: at Android.Runtime.JNIEnv.GetClassNameFromInstance(IntPtr) + 0x1c E AndroidRuntime: at Android.Runtime.JNIEnv.GetConverter[TValue](Dictionary`2, Type, IntPtr) + 0x6c E AndroidRuntime: at Android.Runtime.JNIEnv.CopyArray[T](IntPtr, T[]) + 0x90 E AndroidRuntime: at Android.Graphics.Drawables.LayerDrawable..ctor(Drawable[] layers) + 0x1a4 E AndroidRuntime: at Microsoft.Maui.Platform.MauiRippleDrawableExtensions.UpdateMauiRippleDrawableBackground(View, Paint, IButtonStroke, Func`1, Func`1, Action) + 0x2ac E AndroidRuntime: at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler, IElement) + 0x64 E AndroidRuntime: at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler) + 0x124 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement, IMauiContext) + 0x160 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement, IMauiContext) + 0x28 E AndroidRuntime: at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView) + 0xd4 E AndroidRuntime: at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler) + 0x124 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement, IMauiContext) + 0x160 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement, IMauiContext) + 0x28 E AndroidRuntime: at Microsoft.Maui.Handlers.ScrollViewHandler.UpdateInsetView(IScrollView, IScrollViewHandler, ICrossPlatformLayout) + 0x34 E AndroidRuntime: at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler, IElement) + 0x64 E AndroidRuntime: at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler) + 0x124 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement, IMauiContext) + 0x160 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement, IMauiContext) + 0x28 E AndroidRuntime: at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler) + 0xb4 E AndroidRuntime: at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler, IElement) + 0x64 E AndroidRuntime: at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView) + 0x18 E AndroidRuntime: at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler) + 0x124 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement, IMauiContext) + 0x160 E AndroidRuntime: at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement, IMauiContext) + 0x28 E AndroidRuntime: at Microsoft.Maui.Controls.Platform.Compatibility.ShellFragmentContainer.OnCreateView(LayoutInflater, ViewGroup, Bundle) + 0x64 E AndroidRuntime: at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) + 0x104 E AndroidRuntime: at crc640ec207abc449b2ca.ShellFragmentContainer.n_onCreateView(Native Method) E AndroidRuntime: at crc640ec207abc449b2ca.ShellFragmentContainer.onCreateView(ShellFragmentContainer.java:38) E AndroidRuntime: at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3119) E AndroidRuntime: at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:577) E AndroidRuntime: at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:286) E AndroidRuntime: at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2214) E AndroidRuntime: at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2109) E AndroidRuntime: at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:2002) E AndroidRuntime: at androidx.fragment.app.BackStackRecord.commitNow(BackStackRecord.java:317) E AndroidRuntime: at androidx.viewpager2.adapter.FragmentStateAdapter.placeFragmentInViewHolder(FragmentStateAdapter.java:342) E AndroidRuntime: at androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(FragmentStateAdapter.java:273) E AndroidRuntime: at androidx.viewpager2.adapter.FragmentStateAdapter.onViewAttachedToWindow(FragmentStateAdapter.java:73) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchChildAttached(RecyclerView.java:8377) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$5.addView(RecyclerView.java:954) E AndroidRuntime: at androidx.recyclerview.widget.ChildHelper.addView(ChildHelper.java:131) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:9430) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:9388) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:9375) E AndroidRuntime: at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1676) E AndroidRuntime: at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1622) E AndroidRuntime: at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:687) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4645) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4348) E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4919) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:535) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:149) E AndroidRuntime: at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43) E AndroidRuntime: at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:2365) E AndroidRuntime: at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:953) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891) E AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1729) E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1638) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1273) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891) E AndroidRuntime: at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1880) E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1640) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1891) E AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1729) E AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1638) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) E AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:270) E AndroidRuntime: at com.android.internal.policy.DecorView.onLayout(DecorView.java:807) E AndroidRuntime: at android.view.View.layout(View.java:25159) E AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:6460) E AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:4562) E AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3830) E AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2718) E AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9937) E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1406) E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1415) E AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:1015) E AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:945) E AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1389) E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:959) E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:100) 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) Update `Android.Runtime.JNIEnv.GetClassNameFromInstance()` to call `JniEnvironment.Types.GetJniTypeNameFromInstance()` instead of `Java.Interop.TypeManager.GetClassName()`. This avoids the P/Invoke, fixing the `DllNotFoundException`. --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index 3f6b49e5d1b..01fb5c03572 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -396,12 +396,7 @@ public static unsafe IntPtr NewObject (IntPtr jclass, IntPtr jmethod, params JVa public static string GetClassNameFromInstance (IntPtr jobject) { - IntPtr jclass = GetObjectClass (jobject); - try { - return Java.Interop.TypeManager.GetClassName (jclass); - } finally { - DeleteLocalRef (jclass); - } + return JniEnvironment.Types.GetJniTypeNameFromInstance (new JniObjectReference (jobject)); } [MethodImplAttribute(MethodImplOptions.InternalCall)]