阿里 Android 热补丁AndFix的使用

https://github.com/alibaba/AndFix

使用

For your gradle dependency,

build
dependencies {
    compile 'com.alipay.euler:andfix:0.4.0@aar'
}
how to use, 3 steps:
  1. Initialize PatchManager,
patchManager = new PatchManager(context);
patchManager.init(appversion);//current version
  1. Load patch,
patchManager.loadPatch();

You should load patch as early as possible, generally, in the initialization phase of your application(such as Application.onCreate()).

  1. Add patch,
patchManager.addPatch(path);//path of the patch file that was downloaded

When a new patch file has been downloaded, it will become effective immediately by addPatch.

浏览器项目中的实际使用

ChromeApplication.java
onCreate() calls initAndfixManage().

    private void initAndfixManage() {
        mPatchManager = new PatchManager(this);
        String versionName = "";
        try {
            PackageInfo pi = this.getPackageManager().getPackageInfo(
                    this.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            versionName = pi.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }

        File patchFile = null;
        File files[] = getFilesDir().listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.getName().endsWith(versionName + "_andfix.apatch")) {
                    if (f.isDirectory())
                        f.delete();
                    else {
                        patchFile = f;
                        break;
                    }
                }
            }
        }

        if (null != patchFile) {
            mPatchManager.removeAllPatch();
        }
        mPatchManager.init(versionName);
        mPatchManager.loadPatch();

        if (null != patchFile) {
            try {
                mPatchManager.addPatch(patchFile.getAbsolutePath());
                patchFile.delete();
            } catch (IOException e) {
            }
        }
    }
生成patch
./apkpatch/apkpatch.sh -f ./input/new.apk -t ./input/old.apk -o ./out/ -k ./apkpatch/browser.key -p wuxian123! -a qihoo_androidbrowser -e wuxian123!
wangxin@wangxin:~/src/src_chrome45ce_rel_v6/m_browser_chromium/360browser_build/patchtool/out$ ls
diff.dex  new-518694bb3b313faefe1fa84488fbd5f2.apatch  smali
change "new-518694bb3b313faefe1fa84488fbd5f2.apatch" to "today_7.0.0.20_andfix.apatch", then adb push it to /data/data/com.qihoo.browser/files/ dir.

改patch文件的权限, 否则Log中出现下面的IO exception, 导致无法加载成功.

I/ahking  ( 8155): ChromeApplication initAndfixManage() patchFile = today_7.0.0.20_andfix.apatch
I/ahking  ( 8155): ChromeApplication initAndfixManage() addPatch patchFile path = /data/data/com.qihoo.browser/files/today_7.0.0.20_andfix.apatch
I/ahking  ( 8155): ChromeApplication initAndfixManage() e = java.io.FileNotFoundException: /data/data/com.qihoo.browser/files/today_7.0.0.20_andfix.apatch: open failed: EACCES (Permission denied)
最终热补丁加载成功后的Log输出
I/ahking  ( 8155): ChromeTabbedActivity onResume() i = 10

I/ahking  ( 8613): ChromeApplication initAndfixManage() patchFile = today_7.0.0.20_andfix.apatch
I/ahking  ( 8613): ChromeApplication initAndfixManage() addPatch patchFile path = /data/user/0/com.qihoo.browser/files/today_7.0.0.20_andfix.apatch

----- after load the patch file -----

I/ahking  ( 8613): ChromeTabbedActivity onResume() i = 17

你可能感兴趣的:(阿里 Android 热补丁AndFix的使用)