热更新 AndFix 源码解析

阿里老框架热更新:https://github.com/alibaba/AndFix

使用

0.命令行生成拆分包 :

1
apkpatch.bat -f 修复的apk -t 老的apk -o 输出 -k 签名文件 -p 签名密码 -a 签名别名 -e 签名别名密码

1.PatchManager.init()初始化

2.PatchManager.addPatch(path);

3.PatchManager.loadPatch();

源码分析

1.初始化,检测当前app的patch目录是否存在,并且检测当前版本是否与本地相匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void init(String appVersion) {
if (!mPatchDir.exists() && !mPatchDir.mkdirs()) {// make directory fail
Log.e(TAG, "patch dir create error.");
return;
} else if (!mPatchDir.isDirectory()) {// not directory
mPatchDir.delete();
return;
}
SharedPreferences sp = mContext.getSharedPreferences(SP_NAME,
Context.MODE_PRIVATE);
String ver = sp.getString(SP_VERSION, null);
if (ver == null || !ver.equalsIgnoreCase(appVersion)) {
cleanPatch();
sp.edit().putString(SP_VERSION, appVersion).commit();
} else {
initPatchs();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//将应用目录下apatch目录下的后缀名为.apatch的文件添加到线程安全的数据结构中保存
SortedSet<Patch> mPatchs = new ConcurrentSkipListSet<Patch>();
private Patch addPatch(File file) {
Patch patch = null;
if (file.getName().endsWith(SUFFIX)) {
try {
patch = new Patch(file);
mPatchs.add(patch);
} catch (IOException e) {
Log.e(TAG, "addPatch", e);
}
}
return patch;
}

2.加载

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器