dlopen 是一个用于在系统中动态加载共享库(动态链接库)的函数。它的主要作用是在程序运行时动态地加载共享库,使得程序可以在不重新编译的情况下加载并使用库中的函数和变量。
具体来说,dlopen 函数可以接受一个共享库的路径作为参数,并返回一个句柄(handle),该句柄用于后续操作,例如通过 dlsym 获取库中的函数地址,以及通过 dlclose 关闭已加载的库。
dlopen 函数通常在需要根据特定条件加载不同的库时使用,或者在运行时动态加载插件、扩展或模块时使用。在很多情况下,它被用于实现插件化架构,允许程序在运行时根据需要加载额外的功能模块。
在 Android 开发中,dlopen 通常与 JNI(Java Native Interface)一起使用,以便在 C/C++ 层加载第三方库,并在 Java 层中调用这些库中的函数。
-
创建一个 JNI 接口方法,用于在 Java 和 C/C++ 之间进行通信
// DynamicLibraryLoader.java public class DynamicLibraryLoader { static { System.loadLibrary("native-lib"); } // 声明本地方法 public static native void invokeThirdPartyMethod(); }
-
在 C/C++ 代码中实现这个本地方法,并在其中使用 dlopen 加载第三方动态库,并调用其中的方法
// native-lib.cpp #include <jni.h> #include <dlfcn.h> extern "C" JNIEXPORT void JNICALL Java_com_example_dynamiclibraryloader_DynamicLibraryLoader_invokeThirdPartyMethod(JNIEnv *env, jclass clazz) { // 加载第三方动态库 void* handle = dlopen("/path/to/third_party_library.so", RTLD_LAZY); if (!handle) { // 处理库加载失败 return; } // 获取第三方库中的函数指针 typedef void (*ThirdPartyFunction)(); ThirdPartyFunction thirdPartyFunction = reinterpret_cast<ThirdPartyFunction>(dlsym(handle, "third_party_function")); if (!thirdPartyFunction) { // 处理函数指针获取失败 dlclose(handle); return; } // 调用第三方库中的函数 thirdPartyFunction(); // 关闭动态库句柄 dlclose(handle); }
-
加载动态库
static { //由于通过dlopen动态链接库,要先加载so库,然后在jni层用dlopen //加载顺序是按动态库依赖顺序反序加载的 System.load("/path/to/third_party_library2.so");//dlopen使用的库关联库 System.load("/path/to/third_party_library.so");//dlopen打开的库 System.loadLibrary("native-lib");//Java JNI库 }