1.Andfix介紹#
想了解一個第三方框架,最好去官網(如果有的話)或者github的項目主頁仔細看下介紹。https://github.com/alibaba/AndFix,上面這個地址就是Andfix的主頁。
根據主頁介紹可知,Andfix是一個修復線上bug的解決方案,可以當做Android Library使用。Andfix是“Android hot-fix”的簡稱,它號稱能從android2.3支持到android7.0,并兼容ARM和X86架構,同時兼容Dalvik和ART虛擬機,同時兼容32bit和64bit。是不是很牛逼,感覺要上天了!但是理想與現實總是有差距的...如果你在開發過程中Andfix在那部手機上失效了,不妨換臺手機試試...
2.Andfix實現原理
Andfix能實現熱更新的原理是方法體的替換,如圖:
簡單實現原理就是這樣,在源碼篇詳細介紹(如果我能看懂的話...)
3.Andfix業務場景與流程#
4.Andfix配置#
在Android Studio中添加Andfix能力非常簡單,只要在build.gradle中添加依賴即可。
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.0' testCompile 'junit:junit:4.12' compile 'com.alipay.euler:andfix:0.5.0@aar'//添加andfix支持 }
5.Andfix api使用介紹#
1.初始化PatchManager
patchManager = new PatchManager(context); patchManager.init(appversion);//current version
2.加載patch
patchManager.loadPatch();
一般1,2兩步都是放在Application的onCreate中調用
3.添加patch
patchManager.addPatch(path);//path of the patch file that was downloaded
上層使用的api就這3個,然后我們簡單封裝下,創建一個AndFixManager的管理類,項目中直接使用這個類
`
/**
- Created by loubinfeng on 2017/6/10.
- andfix管理類
*/
public class AndFixManager {
private static AndFixManager mInstance;
private static PatchManager mPatchManager;
private AndFixManager(){}
public static AndFixManager getAndFixManager(){
if (mInstance == null){
synchronized (AndFixManager.class){
if (mInstance == null)
mInstance = new AndFixManager();
}
}
return mInstance;
}
public void initAndFix(Context context){
//step.1 init PatchManager
mPatchManager = new PatchManager(context);
mPatchManager.init(Utils.getVersionName(context));
//step.2 load patch
mPatchManager.loadPatch();
}
public void addPatch(String patchPath){
if (mPatchManager != null) {
try {
mPatchManager.addPatch(patchPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在Application中初始化
public class AndfixApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initAndfix();
}
/**
* init andfix
*/
private void initAndfix(){
AndFixManager.getAndFixManager().initAndFix(this);
}
}
`
ok,關于andfix的代碼集成的工作完成了,超級簡單...
6.模擬業務使用場景#
1.故意在代碼中的某個方法中寫錯代碼,然后用簽名文件打包一個release版本,我們稱為old.apk
2.然后修復這個bug,繼續用這個簽名文件打包一個新的release版本,我們稱為new.apk
3.下載并解壓apkpatch工具,下載地址[https://raw.githubusercontent.com/alibaba/AndFix/master/tools/apkpatch-1.0.3.zip]
然后在終端進入解壓文件目錄,輸入apkpatch命令生成.apatch文件。
usage: apkpatch -f <new> -t <old> -o <output> -k <keystore> -p <***> -a <alias> -e <***> -a,--alias <alias> keystore entry alias. -e,--epassword <***> keystore entry password. -f,--from <loc> new Apk file path. -k,--keystore <loc> keystore path. -n,--name <name> patch name. -o,--out <dir> output dir. -p,--kpassword <***> keystore password. -t,--to <loc> old Apk file path.
4.然后用adb push命令將這個apatch文件傳到手機中(模擬下載一樣),再點擊修復按鈕,這個按鈕綁定的代碼如下
public void handleAndfix(View view){ AndFixManager.getAndFixManager().addPatch(mPatchDir.concat("fix").concat(FILE_END)); }
5.最后你會發現之前那個old.apk的錯誤方法便變正常了,甚至沒有重啟app。
7.總結#
Andfix使用真的非常簡單,熱更新不需要重啟app,這就是他的優勢。不足也非常明顯,功能簡單局限性太大,只支持方法級別的替換,大大約束了他的使用場景。
Demo的地址:
[https://github.com/loubinfeng2013/AndfixDemo]