Android 动态加载so

Android 动态加载so
   与平常我们调用SO库文件不同,我们使用 System.load(String pathName);来动态加载so:
注意:1.System.load只能加载两个目录路劲下的so文件:

  1、/system/lib ;需要root权限

  2、安装包的路径,即:/data/data/<packagename>/…

这里我们将网络下载的so库放在根目录下 /sdcard/ ,然后将so拷贝到 私有路径下

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.util.Log;

public class SoFileUtils {

	private static final String TAG = "TAG_soUtils"; 
	
	/**
	 * 复制文件
	 * @param context
	 * @param sourcePath	资源路径 /sdcard/libhellojin.so
	 * @param destPath		目标路径
	 * @return
	 */
	public static boolean copyFile(String sourcePath, String destPath){

		File sourceFile = new File(sourcePath);
		if(!sourceFile.exists()){
			Log.e(TAG, "so文件不存在,请检查sd卡上是否已下载");
			return false;
		}
		File destFile = new File(destPath);
		// 目标so存在,删除
		if(destFile.exists())
			destFile.delete();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null; 
		try {
			bis = new BufferedInputStream(new FileInputStream(sourceFile));
			bos = new BufferedOutputStream(new FileOutputStream(destFile));
			byte[] buff = new byte[1024 * 8];
			int len = 0;
			while((len = bis.read(buff)) > 0){
				bos.write(buff, 0, len);
			}
		} catch (IOException e) {
			return false;
		} finally {
			if (null != bos)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

			if (null != bis)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return true;
	}
}

然后在需要调用的地方执行:

try {
        String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+ "libHelloJni.so";
        Log.i("TAG", "sourcePath:"+sourcePath);
        String destPath = getDir("libs", Context.MODE_PRIVATE).getAbsolutePath()+ File.separator+ "libHelloJni.so";
        Log.i("TAG", "destPath:"+destPath);
        if(SoFileUtils.copyFile(sourcePath, destPath)){
        	System.load(destPath);
        	//  do what you want
        }
    } catch (Exception e) {
        e.printStackTrace();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值