Android 加载插件 APK 的 so 库

由于插件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 {

    @Override
    protected 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库呢?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值