最近公司框架刚移植完成,由于框架程序要调用子程序,每个子程序都是一个so文件,有好几百个,把所有的so和apk打包不现实,及时可以升级维护也很麻烦。所以需要放SD卡中。考虑两种方式
1
放到设备中的 /data/app-lib/包名/so文件 (还有一个默认路径不记得了)程序程序初始化的时候 复制到这个路径中,loadlibrary时会从两个默认路径中加载。
这种方式设备需要root,效率比较低,如果应用被卸载了所有的so都没了。
2
直接放到SD卡中 通过dlopen 打开SD卡中的so 然后dlsym 查找函数地址
#include "dlfcn.h"//dlopen 头文件 void (*pf_threadcannback)();//声明函数指针类型 void (*pf_program)(); void *handle; bool getMethod() { handle = dlopen("//storage//emulated//0//lib//libprogram.so", RTLD_NOW); // dlopen("/storage/emulated/0/lib/libtwolib-second.so",RTLD_NOW); if (!handle) { __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Your params is null:%s", dlerror()); } else { __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Your params is GOOD2"); } pf_program = dlsym(handle, "<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">myFunc</span>"); //指向so中你想要调用的函数名称 pf_threadcannback = dlsym(handle, "ThreadCallBack"); if (dlerror()) { __android_log_print(ANDROID_LOG_INFO, "JNIMsg", "dlsym no error"); return false; } return true; // 释放资源 建议程序退出时调用 // dlclose(handle); }找到了函数地址接下来直接执行
比如想调用so中的 myFunc()函数 执行 pf_program()就ok了
原文:http://blog.csdn.net/xxmbaobao1/article/details/43529621