Is it necessary to call system.loadLibrary explicitly for accessing the native methods in a NativeActivity sub class?

做~自己de王妃 提交于 2019-12-11 02:43:35

问题


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:


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



来源:https://stackoverflow.com/questions/29453755/is-it-necessary-to-call-system-loadlibrary-explicitly-for-accessing-the-native-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!