android将so打到jar包中并运行

1 篇文章 0 订阅

加载so有两种方法

System.load() 和System.loadLibrary(); 前者需传入库文件的绝对路径,后者只需传入库文件名。

首先我的jar包目录如下:

Loader是加载类:

 static {
        try {
            InputStream is = null;
            if(isCPUInfo64()) {
                is = Loader.class.getResource("arm64/libhellojni.so").openStream();
            }else {
                is = Loader.class.getResource("arm32/libhellojni.so").openStream();
            }
            File tempFile = File.createTempFile("hellojni", ".so");
            FileOutputStream fos = new FileOutputStream(tempFile);
            int i;
            byte[] buf = new byte[1024];
            while ((i = is.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
            is.close();
            fos.close();
            System.load(tempFile.getAbsolutePath());
            tempFile.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

private static boolean isCPUInfo64() {
        File cpuInfo = new File("/proc/cpuinfo");
        if (cpuInfo != null && cpuInfo.exists()) {
            InputStream inputStream = null;
            BufferedReader bufferedReader = null;
            try {
                inputStream = new FileInputStream(cpuInfo);
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 512);
                String line = bufferedReader.readLine();
                if (line != null && line.length() > 0 && line.toLowerCase(Locale.US).contains("arch64")) {
                    return true;
                } else {
                    return false;
                }
            } catch (Throwable t) {
                Log.d("isCPUInfo64", " error = " + t.toString());
            } finally {
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值