阿里热修复AndFix的使用和注意事项

阿里热修复AndFix的使用和注意事项

前言
这段时间项目的问题比较多,公司就有了热修复的想法,于是就测试了阿里和微信的,发现阿里的集成过程很便捷。于是就决定就用它啦。

1. 准备工作

1.1 浏览官方文档

点击查看AndFix文档
这边简单引用一些官方内容。

  1. AndFix is a solution to fix the bugs online instead of redistributing Android App.
    译: AndFix解决在线修复bug,不用再去发布版本了。
  2. 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。
  3. The implementation principle of AndFix is method body’s replacing.
    译: AndFix的实现原理是方法的替换
  4. 总结
    看到这里大家有清楚了修复的概念,方法的替换,前提是方法,也就是布局文件,实体类暂定,(估计不行)
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 password

    5.测试

    正常的项目使用,当然会用到推送,或者登录实时请求网络等方式,强制打补丁,或者提示等方式啦。在这里为了验证,当然这个例子都是官方提供的,感觉也没什么问题。大概就是自己在Sd卡里建一个文件夹,把修改了名称的补丁,放在里面,然后打开旧版的Apk,点击补丁按钮,再点击Toast,发现内容变化了。但是TextView默认没有变化。在代码中写textView.setText(“变化”),这种方式也就能改变了。
    这里写图片描述

    6.测试结果

    这里写图片描述

6.注意事项

凡事都要谨慎,打完补丁,也是需要先测试的,所以你会发现一些问题,为什么没效果,或者直接报错了。一般有几种可能,一个是上面操作中哪一步做错了,另一个就是不支持这么操作咯,注意热修复只修复方法,像数据库加个字段,就别想了,直接发布版本吧。修改界面,如果能代码实现,那也是没问题的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
  1. 首先添加依赖     compile 'com.alipay.euler:andfix:0.3.1@aar'   然后在Application.onCreate()中添加以下代码:     patchManager = new PatchManager(context); //初始化补丁包管理器     patchManager.init(appversion); //版本 String appversion= getPackageManager().getPackageInfo(getPackageName(), 0).versionName;     patchManager.loadPatch(); //加载所有补丁   注意每次appversion变更都会导致所有补丁被删除,如果appversion没有改变,则会加载已经保存的所有补丁。   然后在需要的地方调用PatchManager.addPatch()方法加载新补丁,比如可以在下载补丁文件之后调用。   如果你使用混淆,你要保存mapping.txt,这样的话你在新版本的构建是就可以借助 "-apply mapping” 来使用它了。   混淆需要加入:     -keep class * extends java.lang.annotation.Annotation     -keepclasseswithmembernames class * {       native <methods>;     }     -keep class com.alipay.euler.andfix.** { *; }   2. 打补丁包,首先生成一个apk文件,然后更改代码,在修复bug后生成另一个apk。     通过AndFix官方提供的补丁包创建工具apkpatch生成一个.apatch格式的补丁文件,需要提供原apk,修复后的apk,以及一个签名文件。     可以直接使用命令apkpatch查看具体的使用方法。     使用示例:apkpatch -o D:/Patch/ -k debug.keystore -p android -a androiddebugkey -e android f bug-fix.apk t release.apk     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.   有时候你的团队成员可能会fix各自的bug,这个时候就会有不止一个补丁文件.apatch。这种情况下,可以用这个工具merge这些.apatch文件:     usage: apkpatch -m <apatch_path...> -o <output> -k <keystore> -p <***> -a <alias> -e <***>     -a,--alias <alias> keystore entry alias.     -e,--epassword <***> keystore entry password.     -k,--keystore <loc> keystore path.     -m,--merge <loc...> path of .apatch files.     -n,--name <name> patch name.     -o,--out <dir> output dir.     -p,--kpassword <***> keystore password.   3. 通过网络传输或者adb push的方式将apatch文件传到手机上;   4. 然后运行到patchManager.addPatch(path)的时候就会加载补丁。//path:补丁文件下载到

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值