美文网首页
Andfix热更新框架(使用篇)

Andfix热更新框架(使用篇)

作者: 懒癌患者2018 | 来源:发表于2017-06-11 10:35 被阅读0次

    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能实现热更新的原理是方法体的替换,如图:

    Paste_Image.png

    简单实现原理就是这样,在源码篇详细介绍(如果我能看懂的话...)

    3.Andfix业务场景与流程#

    Paste_Image.png

    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]

    相关文章

      网友评论

          本文标题:Andfix热更新框架(使用篇)

          本文链接:https://www.haomeiwen.com/subject/hechqxtx.html