Is it necessary to call system.loadLibrary explici

2019-07-23 03:41发布

I have an Android app with an activity derived from NativeActivity like this:

public class MyNativeActivity extends android.app.NativeActivity 
{
    public native void TellNativeSide(int info);

    static {
        System.loadLibrary("MyNatAct");  // <--- is this necessary?
    }

    public int OtherMethods(...) ...
}

On the C/C++ side, I have

extern "C" void  
Java_mycom_nativity_MyNativeActivity_TellNativeSide(JNIEnv *env,
    jobjectactivityobj, jint info)
{
    ... do something
} // java native TellNativeSide() method //

extern "C" jint JNI_OnLoad(JavaVM *vm, void *)
{
    LOGI("***JNI_OnLoad called...");
}

The libMyNatAct.so library is loaded automatically by the NativeActivity class and indeed android_main() and everything runs correctly with or without the system.loadLibrary() line. However, JNI_OnLoad() would never be called and the TellNativeSide() method is also not available on the Java side unless the

system.loadLibrary("MyNatAct");

call is there in the static class init block.

So it seems that the native .so has to be loaded twice. Once in the init block to make available all the native methods and get JNI_OnLoad() called, and another time by the NativeActivity class but not through system.loadLibrary()?

Is this the correct behaviour?

1条回答
淡お忘
2楼-- · 2019-07-23 04:14

That's right. You must explicitly call system.loadLibrary() to have the native Java methods bound to exported functions of the .so file

查看更多
登录 后发表回答