您的位置:首页 > 移动开发 > Android开发

Android AndFix初体验

2016-11-14 18:27 260 查看
AndFix是阿里开源的一个热修复框架

github地址:https://github.com/alibaba/AndFix

如何使用:

1.依赖AndFix

compile 'com.alipay.euler:andfix:0.5.0@aar'
2.在项目的application类里面初始化:

@Override
public void onCreate() {
super.onCreate();
initLib();
}

private void initLib() {
initOkHttpUtils();
initAndFix();
}

/**
* 初始化andfix
*/
private void initAndFix() {
// 初始化patch管理类
mPatchManager = new PatchManager(this);
// 初始化patch版本
mPatchManager.init("1.0");
// 加载已经添加到PatchManager中的patch
mPatchManager.loadPatch();
}
3.旧版本和新版本比较 得到补丁文件
用到的软件为apkpatch,下载地址:https://raw.githubusercontent.com/alibaba/AndFix/master/tools/apkpatch-1.0.3.zip

4.把补丁文件放到服务器.前端进行下载.并修复

/**
* 下载补丁
*
* @param url 补丁路径
* @param filePath 补丁存放路径
* @param fileName 补丁名字
*/
private void downloadPatch(String url, String filePath, String fileName) {
OkHttpUtils//
.get()//
.url(url)//
.build()//
.execute(new FileCallBack(filePath, fileName)//
{
@Override
public void onError(Call call, Exception e, int id) {
Toast.makeText(getApplicationContext(), "补丁下载出错", Toast.LENGTH_SHORT).show();
}

@Override
public void onResponse(File response, int id) {
Toast.makeText(getApplicationContext(), "补丁下载成功,安装ing", Toast.LENGTH_SHORT).show();
if (null != response && response.exists()) {
try {
MyApp.getmPatchManager().addPatch(response.getAbsolutePath());
Toast.makeText(getApplicationContext(), "补丁安装成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "补丁安装失败", Toast.LENGTH_SHORT).show();
}
}
}
});
}完整代码地址:
https://github.com/imgod1/TestAndFix

注意点:

1.不要用run得到的old.apk以及new.apk(instant run在这里就是个坑)

2.在主线程进行addpatch
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息