AndFix热修复

来源:互联网 发布:高校教学软件行业 编辑:程序博客网 时间:2024/05/21 17:34

阿里官方开源项目及文档:AndFix

集成流程

1.添加依赖

       dependencies {
         compile 'com.alipay.euler:andfix:0.5.0@aar'
      }

2.初始化

     // 初始化patch管理类
        mPatchManager = new PatchManager(context);
        // 初始化patch版本
        mPatchManager.init("1.0");
        // 加载已经添加到PatchManager中的patch
        mPatchManager.loadPatch();


3.添加补丁


public static void hotFix(String path){
        //添加patch,只需指定patch的路径即可,补丁会立即生效
        try {
            mPatchManager.addPatch(path);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("hotFix", "hotFix: "+e.getMessage() );
        }
    }

4.清除补丁

public static void clear(){
        //删除所有已加载的patch文件
        mPatchManager.removeAllPatch();
    }

5.功能测试

public class Utils {
    public  void test(Context context){
        Toast.makeText(context, "有BUG", Toast.LENGTH_SHORT).show();
//        Toast.makeText(context, "已修复", Toast.LENGTH_SHORT).show();
    }
}


switch (view.getId()){
            case R.id.test:
                new Utils().test(this);
                break;
            case R.id.hotfix:
                File file = new File(Environment.
                        getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/new.apatch");
                if(file.exists()){
                    MainApplication.hotFix(file.getAbsolutePath());
                    KLog.e(file.getAbsolutePath());
                }
                else
                    Toast.makeText(this, "无补丁文件", Toast.LENGTH_SHORT).show();
                break;

        }

权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

工具下载:apkpatch
差分包的生成
命令:apkpatch.bat -f new.apk -t old.apk -o output -k keystone_2015.jks -p 密码 -a 用户别名 -e 用户密码
生成的.apatch文件就是差分包

所遇的坑
(1).初始化之前需要先获取外存读写权限
(2).不支持7.1系统,补丁不生效
(3).AndroidStudio2.3签名问题,需要选择V1和V2 详情:签名问题




原创粉丝点击