Skip to content

AndroidNativeLibrary doesn't trigged JNI_OnLoad #7616

Open
@thenextman

Description

@thenextman

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VS 2022 17.4, Xamarin.Android 13.1.0.1

Description

I have a shared library (.so) written in C. The library exports JNI_OnLoad and JNI_OnUnload, confirmed with readelf:

478: 00000000002ff2f0 132 FUNC GLOBAL DEFAULT 13 JNI_OnLoad
3111: 00000000002ff380 297 FUNC GLOBAL DEFAULT 13 JNI_OnUnload

I reference the .so properly in my Xamarin.Android application using AndroidNativeLibrary.

However, at runtime JNI_OnLoad is never invoked.

Steps to Reproduce

See above

Did you find any workaround?

I can invoke JNI_OnLoad manually by first using reflection to get the java_vm pointer:

[DllImport("MyLibrary", CallingConvention = CallingConvention.Cdecl)]
private static extern int JNI_OnLoad(IntPtr vm, IntPtr reserved);

static MyClass()
{
    var field = typeof(Android.Runtime.JNIEnv).GetField("java_vm", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    JNI_OnLoad((IntPtr)field.GetValue(null), IntPtr.Zero);
}

But this feels like an ugly workaround.

Relevant log output

N/A

Metadata

Metadata

Assignees

Labels

Area: App RuntimeIssues in `libmonodroid.so`.bugComponent does not function as intended.

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions