简体   繁体   English

使用JNI加载另一个JNI库?

[英]Using JNI to load another JNI library?

I need to implement a native method, let's say "public native void someFunc();". 我需要实现一个本机方法,让我们说“public native void someFunc();”。 I have two libraries, libabc.so and libdef.so. 我有两个库,libabc.so和libdef.so。 Java uses System.loadLibrary(); Java使用System.loadLibrary(); to load libabc.so (which does not implement the method), but the JNI implementation is in libdef.so. 加载libabc.so(它没有实现该方法),但JNI实现在libdef.so中。 Currently, I'm doing the following in libabc.so. 目前,我正在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. 但是,我收到一个错误(来自android logcat)“JlI_OnLoad在/lib/libdef.so中返回了坏版本(-1)”如果我直接从Java加载libdef.so,我不会收到此错误。 In addition, if I make another native method "loadDef()" and implement it with the same code, it also works. 另外,如果我创建另一个本机方法“loadDef()”并使用相同的代码实现它,它也可以工作。 The problem, I think, is using jvm->GetEnv() but I'm not sure. 我认为问题是使用jvm-> GetEnv(),但我不确定。 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). 此外,我甚至不知道这是否允许我实现我想要的(使用一个JNI库加载另一个来实现)。 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. 虽然我仍然没有解决在JNI_OnLoad中加载另一个JNI库的问题,但我发现了第一个被调用的本机函数,替换了它,在该函数中,我运行了loadLibrary代码。 Then I call the native method again from JNI and it runs the new version of the method. 然后我再次从JNI调用本机方法,并运行该方法的新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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