前言:
很多時候測試完的產品上線後,突然發現一個小的bug。這時候考慮到用戶體驗、和時間成本,不能爲了一點點bug而重新發布新版本。於是就有了熱修復這個概念的產生!它可以在不發佈版本的情況下修復出bug的代碼。我們來一探究竟。
目前可能用的相對廣泛的熱修復框架有如下幾個:
https://github.com/dodola/HotFix
https://github.com/jasonross/Nuwa
https://github.com/bunnyblue/DroidFix
https://github.com/alibaba/AndFix
看完他們的簡介,基本可以瞭解到,他們都是基於:動態修復技術、Android Dex分包方案,這兩篇文章的原理,所以在使用前,是完全有必要去閱讀這兩篇文章的。
今天我就以阿里巴巴的熱修復框架(AndFix),來做個簡單的演示:
爲什麼會選擇這個來做演示?是因爲看了git上的更新記錄,其他的幾個要麼是用起來比較複雜、要麼就是N久已經沒有更新了,使用起來不安全。
1.使用方法:
添加依賴庫是必要的:
AndroidStudio:
compile 'com.alipay.euler:andfix:0.3.1@aar'
Eclipse直接導入AndFix工程項目即可:下載AndFix
然後在Application.onCreate() 中添加以下代碼
patchManager = new PatchManager(context);
patchManager.init(appversion);//current version
patchManager.loadPatch();
可以用這句話獲取appversionString appversion= getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
注意每次appversion變更都會導致所有補丁被刪除,如果appversion沒有改變,則會加載已經保存的所有補丁。然後在需要的地方調用PatchManager的addPatch方法加載新補丁,比如可以在下載補丁文件之後調用。
之後就是打補丁的過程了,首先生成一個apk文件,然後更改代碼,在修復bug後生成另一個apk。
通過官方提供的工具apkpatch
生成一個.apatch格式的補丁文件,需要提供原apk,修復後的apk,以及一個簽名文件。
可以直接使用命令apkpatch查看具體的使用方法。
使用示例:
生成差異文件命令 : apkpatch.bat -f new.apk -t old.apk -o output1 -k debug.keystore -p android -a androiddebugkey -e android
-f <new.apk> :新版本
-t <old.apk> : 舊版本
-o <output> : 輸出目錄
-k <keystore>: 打包所用的keystore
-p <password>: keystore的密碼
-a <alias>: keystore 用戶別名
-e <alias password>: keystore 用戶別名密碼
通過網絡傳輸或者adb push的方式將apatch文件傳到手機上,然後運行到addPatch的時候就會加載補丁。加載過的補丁會被保存到data/packagename/files/apatch_opt目錄下,所以下載過來的補丁用過一次就可以刪除了。
2.原理
apkpatch將兩個apk做一次對比,然後找出不同的部分。可以看到生成的apatch了文件,後綴改成zip再解壓開,裏面有一個dex文件。通過jadx查看一下源碼,裏面就是被修復的代碼所在的類文件,這些更改過的類都加上了一個_CF的後綴,並且變動的方法都被加上了一個叫@MethodReplace的annotation,通過clazz和method指定了需要替換的方法。
然後客戶端sdk得到補丁文件後就會根據annotation來尋找需要替換的方法。最後由JNI層完成方法的替換。
但是我看到AndFix已經做了驗證,如果補丁文件的證書和當前apk的證書不是同一個的話,就不能加載補丁。
官網還有一條,提示需要驗證optimize file的指紋,應該是爲了防止有人替換掉本地保存的補丁文件,所以要驗證MD5碼,然而SecurityChecker類裏面也已經做了這個工作。。但是這個MD5碼是保存在sharedpreference裏面,如果手機已經root那麼還是可以被訪問的。
4.一些缺點
無法添加新類和新的字段
需要使用加固前的apk製作補丁,但是補丁文件很容易被反編譯,也就是修改過的類源碼容易泄露。
使用加固平臺可能會使熱補丁功能失效(演示樣例時已經驗證過)。
如圖AndFix是上面鏈接下載的工程,AndFixDemo2是我自己的工程。
依賴都整理好,就開始編寫我們自己的工程代碼:
我寫了三個類:
AndFixSay是用來模擬出bug的類:
public class AndFixSay {
public String say(){
return "I'm not fix";
}
}
MainActivity是用來顯示修復前後結果的:public class MainActivity extends ActionBarActivity {
static TextView tv_say_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
tv_say_content = (TextView)rootView.findViewById(R.id.tv_say_content);
Button btn_say = (Button)rootView.findViewById(R.id.btn_say);
btn_say.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv_say_content.setText(new AndFixSay().say());
}
});
return rootView;
}
}
}
MainApplication是用來寫初始化AndFix的
ublic class MainApplication extends Application {
public PatchManager mPatchManager;
private static final String TAG = "euler";
private static final String APATCH_PATH = "/out.apatch";
@Override
public void onCreate() {
super.onCreate();
try {
//注意每次appversion變更都會導致所有補丁被刪除,如果appversion沒有改變,則會加載已經保存的所有補丁。
String appversion= getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
mPatchManager = new PatchManager(this);
mPatchManager.init(appversion);
mPatchManager.loadPatch();
//添加patch,只需指定patch的路徑即可,補丁會立即生效
// add patch at runtime
// .apatch file path
String patchFileString = Environment.getExternalStorageDirectory()
.getAbsolutePath() + APATCH_PATH;
mPatchManager.addPatch(patchFileString);
Log.d(TAG, "apatch:" + patchFileString + " added.");
} catch (NameNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
Log.e(TAG, "", e);
}
}
}
安裝應用到手機上,會有個按鈕,點擊後會顯示:I'm not fix。
然後我們修改兩個地方:
AndFix中添加方法sayFix();
public class AndFixSay {
public String say(){
return "I'm not fix";
}
public String sayFix(){
return "I'm fixed -- lala";
}
}
MainActivity我們修改如下:tv_say_content.setText(new AndFixSay().sayFix());
apkpatch.bat -f FixDemo_new.apk -t FixDemo_old.apk -o output1 -k *** -p *** -a *** -e ***
生成一個.apatch格式的補丁文件!(參數前面有介紹)
正式開發時補丁文件可以通過請求接口下載然後修復,這裏我就直接複製粘貼到sd卡上面,然後給個文件路徑,意思都差不多。最後顯示的結果是:I'm fixed -- lala
這個雖然簡單,但是有很多的侷限性,不能更換資源文件、不能添加類等!
完整項目下載地址:http://download.csdn.net/detail/caihongdao123/9588337