1、so加载机制
https://www.cnblogs.com/dasusu/p/9810673.html?share_token=80260666-442b-4b40-93b7-f6878fe1b169
调用指定路径so的方法
将so文件放到assets目录下,例如下面:
接下来将assets下的.so文件拷贝到文件里,及可以获取文件路径的文件下,而assets文件夹下的文件目前只能读取与写入没有获取文件路径的方法,所以只能将其拷贝放到能获取路径的文件夹下。本人是将其拷贝到了项目的FileDir目录下,拷贝代码如下:
boolean flag = CopyAssetUtils.copyAssetAndWrite(soFileName,Utils.getApp().getFilesDir().getPath());
import com.blankj.utilcode.util.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class CopyAssetUtils {
/**
* 将asset文件写入缓存
*/
public static boolean copyAssetAndWrite(String fileName,String outFileRootPath){
try {
File cacheDir= new File(outFileRootPath);
if (!cacheDir.exists()){
cacheDir.mkdirs();
}
File outFile =new File(cacheDir,fileName);
if (!outFile.exists()){
boolean res=outFile.createNewFile();
if (!res){
return false;
}
}else {
if (outFile.length()>10){//表示已经写入一次
return true;
}
}
InputStream is=Utils.getApp().getAssets().open(fileName);
FileOutputStream fos = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int byteCount;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
接下来通过System.load()该文件路劲即可,如果加载默认路径下的so,只需要通过System.loadLibrary()即可。
下面为本人工程下的代码:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.e("haha","走的低版本");
String soFileName="libjingle_peerconnection_so.so";
boolean flag = CopyAssetUtils.copyAssetAndWrite(soFileName,Utils.getApp().getFilesDir().getPath());
if(flag){
File dataFile=new File(Utils.getApp().getFilesDir(),soFileName);
System.load(dataFile.getAbsolutePath());
}
}else{
Log.e("haha","走的高版本");
System.loadLibrary(name);
}