阿里熱修復AndFix的使用和注意事項
前言
這段時間項目的問題比較多,公司就有了熱修復的想法,於是就測試了阿里和微信的,發現阿里的集成過程很便捷。於是就決定就用它啦。
1. 準備工作
1.1 瀏覽官方文檔
點擊查看AndFix文檔
這邊簡單引用一些官方內容。
- AndFix is a solution to fix the bugs online instead of redistributing Android App.
譯: AndFix解決在線修復bug,不用再去發佈版本了。- AndFix supports Android version from 2.3 to 7.0, both ARM and X86 architecture, both Dalvik and ART runtime, both 32bit and 64bit.
譯: AndFix支持安卓版本2.3到7.0,ARM,X86,Dalvik ,ART,32bit and 64bit。- The implementation principle of AndFix is method body’s replacing.
譯: AndFix的實現原理是方法的替換- 總結
看到這裏大家有清楚了修復的概念,方法的替換,前提是方法,也就是佈局文件,實體類暫定,(估計不行)
1.2 工具下載
就在官方文檔裏邊,找到這個。
2. 依賴
compile 'com.alipay.euler:andfix:0.5.0@aar'
3. 代碼
簡單描述一下代碼,在Application 中做初始化,兩個按鈕,一個做打補丁,一個彈吐司,還有一個textview,做修改佈局測試
public class MainApplication extends Application {
public static PatchManager mPatchManager;
@Override
public void onCreate() {
super.onCreate();
// 初始化patch管理類
mPatchManager = new PatchManager(this);
// 初始化patch版本,這邊版本和當前版本一致
mPatchManager.init("1.0");
// 加載已經添加到PatchManager中的patch
mPatchManager.loadPatch();
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//添加patch,只需指定patch的路徑即可,補丁會立即生效
String storageDir = Environment.getExternalStorageDirectory() + "/AndFix";
File dir = new File(storageDir);
if (!dir.exists()) {
dir.mkdir();
} else {
System.out.println(dir.getAbsolutePath());
}
String path = storageDir+"/fix.apatch";
try {
MainApplication.mPatchManager.addPatch(path);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Button bt_toast = (Button) findViewById(R.id.toast);
bt_toast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"打補丁後",Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.yp.andfixdemo.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hou!"/>
<Button
android:id="@+id/button"
android:text="補丁"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/toast"
android:text="toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
4. 生成補丁
操作說明:
- 安裝好當前軟件
- 生成apk文件,改名old.apk
- 修改toast內容,比如“修改前”,“修改後”
- 修改textview默認,比如qian,hou
- 生成apk文件,改名new.apk
- 做好這些就到之前下載的工具,解壓zip文件
將兩個apk,和打包用的keystore文件到放在裏面
output文件就是我們後面生成的了
在當前位置打開命令行,輸入apkpatch.bat -f new.apk -t old.apk -o output -k debug.keystore -p android -a androiddebugkey -e android
解釋:對應文件夾中的文件,相信大家,已經很明白了,下面簡單說明,
命令行apkpatch.bat
-f 最新的apk
-t 上次發佈的apk
-o 生成文件位置
-k 項目用的keystore
-p keystore password(後三項可參考AS打包時Generate SignedApk)
-a key alias 別名
-e key password5.測試
正常的項目使用,當然會用到推送,或者登錄實時請求網絡等方式,強制打補丁,或者提示等方式啦。在這裏爲了驗證,當然這個例子都是官方提供的,感覺也沒什麼問題。大概就是自己在Sd卡里建一個文件夾,把修改了名稱的補丁,放在裏面,然後打開舊版的Apk,點擊補丁按鈕,再點擊Toast,發現內容變化了。但是TextView默認沒有變化。在代碼中寫textView.setText(“變化”),這種方式也就能改變了。
6.測試結果
6.注意事項
凡事都要謹慎,打完補丁,也是需要先測試的,所以你會發現一些問題,爲什麼沒效果,或者直接報錯了。一般有幾種可能,一個是上面操作中哪一步做錯了,另一個就是不支持這麼操作咯,注意熱修復只修復方法,像數據庫加個字段,就別想了,直接發佈版本吧。修改界面,如果能代碼實現,那也是沒問題的。