由于插件APK并没有被安装在设备上,因此系统并不会自动加载插件APK中的so库。要加载插件APK中的so库,需要先将so库文件解压缩到应用程序的可访问目录下,然后使用 System.load() 方法加载so库文件。
/*** @param apkPath 插件 APK 本地路径* @param cpuArchitecture CPU架构* @param soName so库名* @param outputDir 解压后 so库 的存放路径* @throws Exception 抛异常呗*/public static void loadPluginLibrary(String apkPath, String cpuArchitecture, String soName, File outputDir) throws Exception {if (!soName.startsWith("lib")) {soName = "lib" + soName;}if (!soName.endsWith(".so")) {soName = soName + ".so";}ZipFile zipFile = new ZipFile(apkPath);ZipEntry zipEntry = zipFile.getEntry("lib/" + cpuArchitecture + "/" + soName);InputStream inputStream = zipFile.getInputStream(zipEntry);FileOutputStream outputStream = new FileOutputStream(new File(outputDir, soName));byte[] buffer = new byte[1024];int count;while ((count = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, count);}inputStream.close();outputStream.close();System.load(outputDir.getPath() + "/" + soName);}
来一个简单的使用示例:
假设现在插件APK的 so库名是:libnative-lib.so,
手机的CPU架构是 arm64-v8a,
so库里的一个JNI方法名是executeMethod(当然JNI的方法是指向宿主Activity的)
extern "C" JNIEXPORT jstring JNICALL
Java_cn_wk_plugindemo_MainActivity_executeMethod(JNIEnv* env,jobject /* this */) {std::string hello = "我是插件中so库的资源";return env->NewStringUTF(hello.c_str());
}
宿主Activity:
package cn.wk.plugindemo;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String apkPath = getCacheDir().getPath() + "/xxx.apk";LoadUtils.loadPluginLibrary(apkPath, "arm64-v8a", "native-lib", getCacheDir());Log.i("TAG", executeMethod());}public native String executeMethod();
}
这里获取插件APK的步骤我省略了,可以参考:https://blog.csdn.net/weixin_47592544/article/details/128869676 当中的copyAssetAndWrite() 方法
可以看到这种加载so库的方式是直接把插件APK解压缩,然后复制一份so库出来再加载,加载完毕后在宿主APK自行调用 native 方法。
这样子实现运用大量的文件读写,除了这种实现方式,能不能直接读取插件APK中的so库呢?
上一篇:Redis的持久化操作
下一篇:【云原生】持久化存储之NFS