生成dex包
1、先通过修改环境变量的方法将jdk版本切换至1.7 使用cmd命令 javac 要编译的java文件生成class文件 javac -d ./ java文件(会自动根据包名生成目录)在java文件处执行
2、修改回原来的1.8版本 执行dx --dex --output=目标名.dex ./包名+上一步生成的.class (在java文件处执行)
生成dex文件
3、将其导入Raw目录(需要自己新建)中
4、将其写入存储
//解压RAW文件方法
public static String unzipRAWFile(Context context) {
String apkFilePath;
//资源管理
Resources resources = context.getResources();
//获取RAW文件的输出留
InputStream inputStream = resources.openRawResource(R.raw.repairclass);
//获取外部临时缓存区
File externalCacheDir = context.getExternalCacheDir();
//在外部缓存区生成文件
File file = new File(externalCacheDir.getAbsolutePath(), resources.getResourceEntryName(R.raw.repairclass) + ".dex");
//获取路径名
apkFilePath = file.getAbsolutePath();
//如果文件不存在
if (!file.exists()) {
BufferedOutputStream bufferedOutputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buffer = new byte[4 * 1024];
int size;
try {
while ((size = inputStream.read(buffer)) != -1) {
//将从资源获取的文件内容写入缓存
bufferedOutputStream.write(buffer, 0, size);
bufferedOutputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
if (bufferedOutputStream != null)
bufferedOutputStream.close();
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("", "文件解压完毕,路径地址为:" + apkFilePath);
} else {
Log.i("", "文件已存在,无需解压"+apkFilePath);
}
return apkFilePath;
}
5、从dex文件中加载类
private void loadClass(String apkPath) {
//获取类加载器
ClassLoader classLoader = getClassLoader();
//根据dex文件的路径创建File
File file = new File(apkPath);
//在这个目录下创建一个optimizedDirectory用来存放系统优化的dex包
File optimizedDirectoryFile = new File(file.getParentFile(), "optimizedDirectory");
if (!optimizedDirectoryFile.exists())
optimizedDirectoryFile.mkdir();
try {
/***
* apkpath:dex文件的路径
* optimizedDirectoryFile.getAbsolutePath() :优化后的存放地址
* "":引用的lib库,此处为空字符串
* classloader:上面创建的类加载器
*/
DexClassLoader dexClassLoadethisr = new DexClassLoader(apkPath, optimizedDirectoryFile.getAbsolutePath(), "",classLoader);
//加载的类名要与Dex包中类型一致
Class<?> aClass = dexClassLoadethisr.loadClass("RepairClass");
Object instance = aClass.newInstance();
//获取getName方法,没有参数
Method getNameMethod = aClass.getMethod("getName");
Object name = getNameMethod.invoke(instance);
Log.i("getName", "loadClass: "+name);
//获取getNum方法,有一个String的参数
Method method = aClass.getMethod("getNum", String.class);
Object num = method.invoke(instance, "Gosdwd!");
Log.i("getNum", "loadClass: "+num);
} catch (Exception e) {
e.printStackTrace();
}
}
热修复方案的实现,将修复完的类打包成一个dex文件(包含完整的类名),在加载时,使用DexClassLoader将dex包中的类加载,将其添加到系统生成的类列的前端,这样在查找类时便会先加载外部dex包的修复类
BaseDexClassLoader中有个pathList对象,pathList中包含一个DexFile的集合dexElements,类加载就是遍历这个集合,通过DexFile去寻找
所以我们需要取得外部dex包中dexElements将其放到系统的dexElements前面
public String inject(String apkPath) {
boolean hasBaseDexClassLoader = true;
File file = new File(apkPath);
File optimizedDirectoryFile = new File(file.getParentFile(), "optimizedDirectory");
if (!optimizedDirectoryFile.exists())
optimizedDirectoryFile.mkdir();
try {
//通过反射加载类加载器
Class.forName("dalvik.system.BaseDexClassLoader");
} catch (ClassNotFoundException e) {
hasBaseDexClassLoader = false;
}
if (hasBaseDexClassLoader) {
//获取系统生成的类加载器
PathClassLoader pathClassLoader = (PathClassLoader) getClassLoader();
//获取路径dex文件中的类加载器
DexClassLoader dexClassLoader = new DexClassLoader(apkPath, optimizedDirectoryFile.getAbsolutePath(), "", pathClassLoader);
try {
//将自定义导入dex文件中的类列表与默认的合并,将前者置于前列
//通过getPathList(pathClassLoader),拿到PathClassLoader中的pathList对象
// 在调用getDexElements通过pathList取到dexElements对象
Object dexElements = combineArray(getDexElements(getPathList(pathClassLoader)), getDexElements(getPathList(dexClassLoader)));
Object pathList = getPathList(pathClassLoader);
//将合并的类文件数组重新反射设置为pathClassLoader的类数组
setField(pathList, pathList.getClass(), "dexElements", dexElements);
return "SUCCESS";
} catch (Throwable e) {
e.printStackTrace();
return android.util.Log.getStackTraceString(e);
}
}
return "SUCCESS";
}
//获取dexElements属性
public Object getDexElements(Object obj) throws NoSuchFieldException, IllegalAccessException {
//通过反射获取dexElements属性值 obj为PathList的属性值
return getField(obj, obj.getClass(), "dexElements");
}
//获取PathList属性
public Object getPathList(Object obj) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
return getField(obj, Class.forName("dalvik.system.BaseDexClassLoader"), "pathList");
}
//获取反射属性的通用方法
private static Object getField(Object obj, Class cls, String str)
throws NoSuchFieldException, IllegalAccessException
{
//获取反射属性通用方法 obj为实体类 cls为类类型,str为属性名
Field declaredField = cls.getDeclaredField(str);
declaredField.setAccessible(true);
return declaredField.get(obj);
}
//将数组组合(外部dex文件中类数组加载到前面)
/***
*
* @param obj 内部的类数组
* @param obj2 外部的dex 为DexElements类型的反射
* @return 合成的数组
*/
public Object combineArray(Object obj, Object obj2) {
//将数组2添加到数组1前面
Class componentType = obj2.getClass().getComponentType();
int length = Array.getLength(obj2);
int length2 = Array.getLength(obj) + length;
Object newInstance = Array.newInstance(componentType, length2);
for (int i = 0; i < length2; i++)
{
if (i < length)
{
Array.set(newInstance, i, Array.get(obj2, i));
} else
{
Array.set(newInstance, i, Array.get(obj, i - length));
}
}
return newInstance;
}
//设置属性值(将合并的数组设置回属性)
public void setField(Object pathList, Class aClass, String fieldName, Object fieldValue) {
try {
Field declaredField = aClass.getDeclaredField(fieldName);
declaredField.setAccessible(true);
declaredField.set(pathList, fieldValue);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}