Android AndFix热更新

AndFix,。是阿里开源的一个Android热补丁框架,允许APP在不重新发布版本的情况下修复线上的bug

 1、导入依赖包

  compile 'com.alipay.euler:andfix:0.5.0@aar'

2、Application 的OnCreate中

private PatchManager mPatchManager;
private String PATH = "andfix.apatch";  //热更新文件

@Override
public void onCreate() {
    super.onCreate();
    mPatchManager = new PatchManager(this);
    mPatchManager.init(Utils.getVersionName(this));  //APP版本号(版本号不同将会移除更Apatch文件)
    mPatchManager.loadPatch();

    String apatchPath = Utils.getApplicationCachePath(this) + File.separatorChar + PATH;
    try {   //加载热更新文件
        mPatchManager.addPatch(apatchPath);
        mPatchManager.removeAllPatch();//移除更新
    } catch (Exception e) {

    }
    getApath(apatchPath);  //网络下载,此处在Application中开启网络请求

}


/**
 * 下载apatch文件
 *通过Retrofit进行下载
 * @param apathPath
 */
public void getApath(final String apathPath) {
    ApiService apiService = ApiRetrofit.getInstance().getApiService();
    Call<ResponseBody> call = apiService.getApath("url");  //服务器上apatch文件地址
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            boolean isSucess = Utils.writeToApplicationCache(response.body(), apathPath);//获取到文件之后保存到本地SD卡
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
 
/**
 * 保存热更新文件到SD卡根目录下
 * @param body
 * @param filePath
 * @return
 */
public static boolean writeToApplicationCache(ResponseBody body,String filePath) {
    try {
        // todo change the file location/name according to your needs
        File apatchFile = new File(filePath);

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            byte[] fileReader = new byte[4096];
            inputStream = body.byteStream();
            outputStream = new FileOutputStream(apatchFile);

            while (true) {
                int read = inputStream.read(fileReader);
                if (read == -1) {
                    break;
                }
                outputStream.write(fileReader, 0, read);
            }

            outputStream.flush();

            return true;
        } catch (IOException e) {
            return false;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }
        }
    } catch (IOException e) {
        return false;
    }
}
});}
 
3、生成apatch文件
 1)下载生成工具  https://raw.githubusercontent.com/alibaba/AndFix/master/tools/apkpatch-1.0.3.zip
   2)两个APK文件 一个线上带有BUG的,一个修复后的

  3)生成补丁

下载完生成工具后解压,打开cmd命令行,cd到apkpatch.bat所在的目录: 
具体命令可以参考github上,这里我引用下来: 
apkpatch -f ‘新apk路径’ -t‘ 旧apk的路径’ -o ‘生成的补丁包的输出目录’ -k‘ 打包签名的keystore文件路径’ -p ‘签名文件密码’ -a‘ keystore别名’ -e ’keyStore entry的密码‘ 

 如:apkpatch -o E:\ -k E:\liujks -p liu -a liu -e liu -f app-release.apk -t app-debug.apk

回车就会生成对应的 apatch文件,将其放到对应的网络或者手机打开APP即可替换

 4)如果本地有多个apatch文件则根据生成时间来加载






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值