是否有必要明确调用的System.loadLibrary访问的NativeActivity的子类中的本地方法? [英] Is it necessary to call system.loadLibrary explicitly for accessing the native methods in a NativeActivity sub class?
问题描述
我有从NativeActivity的衍生这样一个活动的Android应用:
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(...) ...
}
在C / C ++的一面,我有
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...");
}
的libMyNatAct.so库由NativeActivity的类自动加载和确实android_main()和一切有或没有的System.loadLibrary()线正常运行。然而,JNI_OnLoad()将永远不会被调用和TellNativeSide()方法也无法在Java端,除非
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必须加载两次。一旦在init块由NativeActivity的类使用的所有本机方法,并得到JNI_OnLoad()调用,其他时间但不通过的System.loadLibrary()?
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()?
这是正确的行为?
推荐答案
这是正确的。您必须显式调用的System.loadLibrary()
有绑定到.so文件的导出函数的本地Java方法
That's right. You must explicitly call system.loadLibrary()
to have the native Java methods bound to exported functions of the .so file
这篇关于是否有必要明确调用的System.loadLibrary访问的NativeActivity的子类中的本地方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!