Android之热修复实战

InputStream is = null;

try {

fos = new FileOutputStream(hackFile);

is = context.getAssets().open(“hack.dex”);

int len;

byte[] buffer = new byte[2048];

while ((len = is.read(buffer)) != -1) {

fos.write(buffer, 0, len);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (is != null) {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return hackFile;

}

/**

  • 1、获取程序的PathClassLoader对象

  • 2、反射获得PathClassLoader父类BaseDexClassLoader的pathList对象

  • 3、反射获取pathList的dexElements对象 (oldElement)

  • 4、把补丁包变成Element数组:patchElement(反射执行makePathElements)

  • 5、合并patchElement+oldElement = newElement (Array.newInstance)

  • 6、反射把oldElement赋值成newElement

  • @param application

  • @param patch

*/

public static void installPatch(Application application, File patch) {

File hackDex = initHack(application);

List patchs = new ArrayList<>();

patchs.add(hackDex);

if (patch.exists()) {

patchs.add(patch);

}

//1、获取程序的PathClassLoader对象

ClassLoader classLoader = application.getClassLoader();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

try {

ClassLoaderInjector.inject(application, classLoader, patchs);

} catch (Throwable throwable) {<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值