1. 程式人生 > >基於AndFix的熱修復 成功後簡單的總結總結錯失

基於AndFix的熱修復 成功後簡單的總結總結錯失

-a 需求 style not alt eth 20px extends ble

技術分享首先了解熱修復是什麽東西??

就我自己簡單的理解:就是不須要又一次打包 公布到市場 然後再讓用戶又一次下載就能夠把一些小bug和需求通過補丁的形式進行改動。

然後如今的熱修復方式有大致的三種:

1.dexposed github https://github.com/alibaba/dexposed

2.andfix github https://github.com/alibaba/AndFix

3.bsdiff http://blog.csdn.net/lazyer_dog/article/details/47173013

可是我如今僅僅先搞定了一種 ,andfix。

。。以後再慢慢來

首先AndroidStudio的導入路徑:compile ‘com.alipay.euler:andfix:0.3.1@aar

在程序的入口處初始化PatchManager 一般在BaseApp裏面進行初始化:

public class MainApplication extends Application {
    private static final String TAG = "euler";

    private static final String APATCH_PATH = "/out.apatch"
; private static final String DIR = "apatch";//補丁目錄 /** * patch manager */ private PatchManager mPatchManager; @Override public void onCreate() { super.onCreate(); // initialize mPatchManager = new PatchManager(this); mPatchManager.init("1.0"
); //載入已經有了的路徑 mPatchManager.loadPatch(); try { String patchFileString = Environment.getExternalStorageDirectory() .getAbsolutePath() + APATCH_PATH; //加入新的路徑 mPatchManager.addPatch(patchFileString); //復制且載入補丁成功後,刪除下載的補丁 File f = new File(this.getFilesDir(), DIR + APATCH_PATH); if (f.exists()) { new File(patchFileString).delete(); } } catch (IOException e) { Log.e(TAG, "", e); } } }

對了 ,記得一定要在清單文件中面註冊哦 ~ 不要粗心

補丁文件保存的路徑就是mPatchManager.addPatch("");的路徑。然後假設須要更新應該會給一個補丁下載的地址 把補丁保存到你設置好的這個路徑裏面,又一次啟動程序就能夠把補丁載入進去了,每次載入補丁後 把補丁進行刪除。防止每次進程序都會去載入補丁。

然後重點是補丁的生成,,在這一步有點有失誤。。希望可以給你們一點提醒

生成path文件的工具就是apkpatch-1.0.3 詳細的下載地址 https://github.com/alibaba/AndFix 能夠在這裏面找到

技術分享

技術分享

然後就是生成補丁文件

先進入到apkpatch所在的目錄 假設輸入apkpatch顯演示樣例如以下 是對的

技術分享

技術分享

然後能夠把你要合並的兩個apk 和你的密鑰庫文件都放到裏面來 方便 命令的輸入

命令 : apkpatch.bat-fnew.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的password

-a<alias>: keystore 用戶別名

-e<alias password>: keystore 用戶別名密碼


然後要註意的是 andfix 不能改動成員變量 也不能改動布局文件

在你打包的時候 兩個apk包必須都是用相同的打包方式 debug 或者release 假設兩個不相同的話,會報錯 。由於兩次生成的buildConfig是不同的,改動了成員變量然後就會報錯,產生不了補丁文件。

技術分享

如圖所看到的 是成功的。

技術分享


技術分享

當你補丁文件產生成功後。把他移至你在程序入口定義的addPath()路徑裏面,第二次啟動程序後就會改動你改動過的內容。



記得混淆代碼:

-optimizationpasses 5                                                           # 指定代碼的壓縮級別
-dontusemixedcaseclassnames                                                     # 是否使用大寫和小寫混合
-dontskipnonpubliclibraryclasses                                                # 是否混淆第三方jar
-dontpreverify                                                                  # 混淆時是否做預校驗
-verbose                                                                        # 混淆時是否記錄日誌
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*        # 混淆時所採用的算法

#重要,別忘了這些。不混淆andfix包,不混淆native方法
-dontwarn android.annotation
-dontwarn com.alipay.euler.**
-keep class com.alipay.euler.** {*;}
-keep class * extends java.lang.annotation.Annotation
-keepclasseswithmembernames class * {
    native <methods>;
}


基於AndFix的熱修復 成功後簡單的總結總結錯失