简体   繁体   中英

Using JNI to load another JNI library?

I need to implement a native method, let's say "public native void someFunc();". I have two libraries, libabc.so and libdef.so. Java uses System.loadLibrary(); to load libabc.so (which does not implement the method), but the JNI implementation is in libdef.so. Currently, I'm doing the following in libabc.so.

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved){
JNIEnv *env;
jclass cls;
jmethodID get_load_id;
jstring name;

jvm->GetEnv((void**)&env, JNI_VERSION_1_4);
cls = env->FindClass("java/lang/System");
get_load_id = env->GetStaticMethodID(cls, "load", "(Ljava/lang/String;)V");
name = env->NewStringUTF("/lib/libdef.so");
env->CallStaticVoidMethod(cls, get_load_id, name);

return JNI_VERSION_1_4;
}

However, I'm getting an error (from android logcat) "JNI_OnLoad returned bad version (-1) in /lib/libdef.so" If I load libdef.so directly from Java, I don't get this error. In addition, if I make another native method "loadDef()" and implement it with the same code, it also works. The problem, I think, is using jvm->GetEnv() but I'm not sure. Also, I don't even know if this would allow me to achieve what I want (use one JNI library to load another to implement). The reason I'm doing this is complicated, but there are no alternatives.

While I still don't have a solution for the problem of loading another JNI library in JNI_OnLoad, I found the first native function that is called, replaced it, and in that function, I run the loadLibrary code. Then I call the native method again from JNI and it runs the new version of the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM