在独立原生库中调用原生库方法

Call native library method in independent native library

我正在尝试实施此 Whosebug post 中所述的解决方案。

正如解决方案所建议的那样,我创建了一个独立的本机库。到目前为止,这就是我实现该库的方式。

#include "zoom_Main_VideoPlayer.h"
#include <dlfcn.h>

void *handle;
typedef int (*func)(int); // define function prototype
func myFunctionName; // some name for the function

JNIEXPORT void JNICALL Java_zoom_render_RenderView_naClose(JNIEnv *pEnv, jobject pObj) {

    handle = dlopen("path to nativelibrary1.so", RTLD_LAZY);
    myFunctionName = (func)dlsym(handle, "Close");
    myFunctionName(1); // passing parameters if needed in the call
    dlclose(handle);
    return;
}

根据解决方案,再构建一个独立的原生库(实用程序库)来加载和卸载其他库。因此,我试图在此独立库中加载和卸载 nativelibrary1。比如这个other native库中的native方法是这样的

// native method in nativelibrary1
JNIEXPORT void JNICALL Java_zoom_render_RenderView_naClose(JNIEnv *pEnv, jobject pObj) {

    if (!is) {
        do_exit(is);
    }
    else {
        do_exit(NULL);
    }

    LOGI(0, "Clean-up done");
}

我不确定应该如何修改 nativelibrary1,因为它们不再是直接在 java 代码中调用的本地方法(nativelibrary1 库没有直接加载到静态库中java 代码块 )。

另外,我是否应该更改 typedef int (*func)(int); // define function prototype 以适应 nativelibrary1 中的方法类型?

有点不清楚你想让函数做什么,但如果我理解,你希望你的Java-可调用库像下面这样(假设"C" 不是 C++)

#include "zoom_Main_VideoPlayer.h"
#include <dlfcn.h>
void *handle;
typedef void (*func)(); // define function prototype
func myFunctionName; // some name for the function
JNIEXPORT void JNICALL Java_zoom_render_RenderView_naClose(JNIEnv *pEnv, jobject pObj) {

    handle = dlopen("path to nativelibrary1.so", RTLD_LAZY);
    myFunctionName = (func)dlsym(handle, "Close");
    myFunctionName(); // passing parameters if needed in the call
    dlclose(handle);
    return;
}

而您的其他图书馆将是:

// native method in nativelibrary1
void Close() {
    if (!is) {
        do_exit(is);
    }
    else {
        do_exit(NULL);
    }
    LOGI(0, "Clean-up done");
}

因为你的 Close() 没有任何参数。您还可以将 Java 方法设为静态,这样虚假的 pObj 就不会添加到本机方法签名中。

如果你描述了这些方法应该做什么,我可以提出更好的建议。