问题
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