动态加载so,稍微修改了一下
This commit is contained in:
@@ -82,6 +82,7 @@ repositories {
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation project(':lib_so')
|
||||
annotationProcessor rootProject.ext.dependencies["arouter-compiler"]
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
|
||||
api files('libs/jcc-bate-0.7.3.jar')
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.yunbao.common.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.pdlive.lib_so.DynamicSoLauncher;
|
||||
import com.yunbao.common.CommonAppContext;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
@@ -30,41 +32,24 @@ import okhttp3.Response;
|
||||
|
||||
public class LoadSoUtil {
|
||||
|
||||
public static void loadSo(Context context, onLoadSoListener listener) {
|
||||
try {
|
||||
String path = context.getFilesDir().getAbsolutePath() + "/dynamic_so";
|
||||
File file = new File(path);
|
||||
AssetManager assetManager = context.getAssets();
|
||||
String[] files = assetManager.list("libso");
|
||||
if (!file.exists()) {
|
||||
boolean b = file.mkdir();
|
||||
Log.i("mLog", "创建文件 " + b);
|
||||
}
|
||||
if (file.listFiles().length != files.length) {//说明之前已经保存了
|
||||
file.delete();
|
||||
file.mkdir();
|
||||
Log.i("mLog", "需要拷贝so文件 ");
|
||||
// 获取AssetManager对象
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
loadLibrary(context, "libso/" + files[i], files[i]);
|
||||
}
|
||||
}
|
||||
listener.ok();
|
||||
Log.i("mLog", "拷贝so成功------------------------");
|
||||
} catch (IOException e) {
|
||||
Log.e("mLog", "拷贝so错误++++++++++++++++++++");
|
||||
e.printStackTrace();
|
||||
listener.error();
|
||||
}
|
||||
}
|
||||
//so保存的路径
|
||||
public static final String so_path = CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + "/" + LoadSoUtil.so_path;
|
||||
|
||||
public static void loadSo2(Context context, onLoadSoListener listener) {
|
||||
/**
|
||||
* 先加载so文件,再去做初始化的操作
|
||||
* 1.正常的流程,判断手机的类型 加载不同的so(SUPPORTED_ABIS[0].contains("64"),代码中还未实现 )
|
||||
* 2.通过不同的url去下载对应的so的zip文件
|
||||
* 3.解压zip到指定位置
|
||||
* 4.加载解压完成的so文件
|
||||
*/
|
||||
public static void downloadSoAndLoad(Context context, onLoadSoListener listener) {
|
||||
//检查本地是否创建了zip的缓存文件
|
||||
String path = context.getFilesDir().getAbsolutePath() + "/so_zip";
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
file.mkdir();
|
||||
} else if (isLoadSo(context)) {
|
||||
listener.ok();
|
||||
} else if (isLoadSo()) {//这里去验证so文件是否已经下载过了
|
||||
loadSo(listener);
|
||||
return;
|
||||
}
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
||||
@@ -92,7 +77,7 @@ public class LoadSoUtil {
|
||||
@Override
|
||||
public void ok() {
|
||||
//下载完成开始解压 解压完成之后 区初始化声网数据
|
||||
zip(context, path + "/" + "libSo.zip", listener);
|
||||
zip(path + "/" + "libSo.zip", listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,13 +87,16 @@ public class LoadSoUtil {
|
||||
});
|
||||
}
|
||||
|
||||
private static void zip(Context context, String path, onLoadSoListener listener) {
|
||||
/**
|
||||
* 下载完成解压zip文件
|
||||
*/
|
||||
private static void zip(String path, onLoadSoListener listener) {
|
||||
ZipInputStream zis = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
zis = new ZipInputStream(new FileInputStream(path));
|
||||
ZipEntry ze;
|
||||
String soPath = context.getFilesDir().getAbsolutePath() + "/dynamic_so";
|
||||
String soPath = so_path;
|
||||
File file = new File(soPath);
|
||||
if (!file.exists()) {
|
||||
file.mkdir();
|
||||
@@ -125,7 +113,7 @@ public class LoadSoUtil {
|
||||
fos.close();
|
||||
zis.closeEntry();
|
||||
}
|
||||
listener.ok();
|
||||
loadSo(listener);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
listener.error();
|
||||
@@ -138,6 +126,10 @@ public class LoadSoUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 去下载so的zip文件
|
||||
* 可以在下载之前去做zip是否已经下载的验证,还可以根据版本做区分(是否是最新版本,要不要做断点续传)
|
||||
*/
|
||||
private static void downloadFile(OkHttpClient mOkHttpClient, String url, File file, onLoadSoListener listener) {
|
||||
Request request = new Request.Builder()
|
||||
.get()
|
||||
@@ -181,9 +173,11 @@ public class LoadSoUtil {
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean isLoadSo(Context context) {
|
||||
String pathSo = context.getFilesDir().getAbsolutePath() + "/dynamic_so";
|
||||
File fileSo = new File(pathSo);
|
||||
public static boolean isLoadSo() {
|
||||
File fileSo = new File(so_path);
|
||||
if (!fileSo.exists()) {
|
||||
fileSo.mkdir();
|
||||
}
|
||||
if (fileSo.listFiles().length == 22) {//说明之前已经保存了
|
||||
Log.i("mLog", "已经下载了so 所有直接加载就行 ");
|
||||
return true;
|
||||
@@ -191,44 +185,19 @@ public class LoadSoUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean loadLibrary(Context context, String oldFileName, String libName) throws IOException {
|
||||
// 获取应用的私有模式的libs目录(真实的目录是app-libs)
|
||||
String path = context.getFilesDir().getAbsolutePath() + "/dynamic_so/";
|
||||
// 可以获取到assets资源文件的数据流
|
||||
InputStream open = context.getAssets().open(oldFileName);
|
||||
String new_file_name = path + libName;
|
||||
// 把资源文件的数据流写入到app-libs目录下
|
||||
if (!copyLibrary(open, new_file_name)) {
|
||||
return false;
|
||||
/**
|
||||
* 调用自己实现的加载so的方式
|
||||
*/
|
||||
public static void loadSo(onLoadSoListener listener) {
|
||||
File fileSo = new File(so_path);
|
||||
if (!fileSo.exists()) {
|
||||
fileSo.mkdir();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//从assets资源目录下复制到app-libs目录下
|
||||
public static boolean copyLibrary(InputStream fileInputStream, String new_file) {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
File file = new File(new_file);
|
||||
fos = new FileOutputStream(file);
|
||||
int dataSize;
|
||||
byte[] dataBuffer = new byte[2048];
|
||||
|
||||
while ((dataSize = fileInputStream.read(dataBuffer)) != -1) {
|
||||
fos.write(dataBuffer, 0, dataSize);
|
||||
}
|
||||
fos.flush();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("mLog", e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (fos != null) fos.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
File file[] = fileSo.listFiles();
|
||||
for (int i = 0; i < file.length; i++) {
|
||||
DynamicSoLauncher.INSTANCE.loadSoDynamically(file[i]);
|
||||
}
|
||||
return false;
|
||||
listener.ok();
|
||||
}
|
||||
|
||||
public interface onLoadSoListener {
|
||||
|
||||
Reference in New Issue
Block a user