zoukankan      html  css  js  c++  java
  • Android Hotpatch系列之-项目介绍

    给现实Android apk打补丁,不用强迫客户升级客户端,悄悄的就把bug修复了,程序猿再也不用被老大骂娘了。 客户端例子实现:https://github.com/fengcunhan/Hotpatch-Sample 服务器简易实现:https://github.com/fengcunhan/Hotpatch-SimpleServer

        在手淘工作期间,内部有一套解决方案,给线上apk打补丁,直接修复bug,不用客户端升级,有严重线上bug,找到问题,写个patch,推上线就把问题解决了,用过的都说好。前段时间,他们终于把这套东西的Android端开源了,Dexposed,具体可以看详细的项目介绍。其功能就是可以加载类,替换原先apk里面某个类的方法,或者在方法之前,方法之后做些什么,比如在方法执行之前,修改传入方法的参数了,方法执行以后,修改方法的返回值,或者就是完全替换掉原先的方法。

        项目里面有一个sample,介绍了如何加载一个外部apk包,然后替换宿主apk里面类的方法。其实这就已经完成了80%的工作了,我做的工作只是将Patch获取从本地加载改成了去服务器获取,以及添加了Patch apk的校验(md5和签名信息)。

        Hotpatch的工作流程是:

             1.实现一个从服务器端获取Patch信息的接口(上传本地客户端版本等信息,服务器好根据这些信息来判断是否有对应的Patch包),并将这个信息注入给HotpatchManager。

             2.检测客户端是否可以支持。(Android L和Android M)目前都不支持,最低支持2.3

             3.如果客户端支持,那就调用第一步注入的Patch包获取类,获取到对应的Patch信息。下载Patch apk文件。

             4.校验。获取下载好的Patch apk md5签名信息和服务器给的Patch信息的md5值是否一样,然后还要校验Patch apk和宿主apk签名是否一致。加校验是为了防止Patch apk被篡改,如果load进去了一个第三方的Patch,那就是引狼入室了。一定不能去掉校验,不能去掉,不能去掉,重要的事情说三遍!!!

             5.校验成功,就load Patch apk。

       下面是贴代码时间:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    public class HotpatchManager {
        private static HotpatchManager INSTANCE=new HotpatchManager();
        private HotpatchManager() {
     
        }
     
        public static HotpatchManager getInstance(IPatchInfoRequest request){
            RequestManager.getInstance().setIPatchInfoRequest(request);
            return INSTANCE;
        }
     
        public boolean init(final Context ctx) {
            boolean isSupport = DexposedBridge.canDexposed(ctx);
            if (isSupport) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        check(ctx);
                    }
                }).start();
            }
            return isSupport;
        }
     
        private  void check(final Context ctx) {
            RequestManager manager = RequestManager.getInstance();
            manager.setIPatchInfoRequest(RequestManager.getInstance().getIPatchInfoRequest());
            manager.reqeust(new RequestManager.OnRequestCallBackListener() {
                @Override
                public void onRequest(final PatchInfo info) {
                    if (null != info) {
                        String apkPath = Utils.getCacheApkFilePath(ctx, info.apkFileUrl);
                        File file = new File(apkPath);
                        if (file.exists()) {
                            loadPath(info, ctx, apkPath);
                        else {
                            DownLoadManager.getInstance().downloadFile(ctx, info.apkFileUrl, new DownLoadManager.OnFileDownload() {
                                @Override
                                public void fileDownload(String apkFilePath) {
                                    loadPath(info, ctx, apkFilePath);
                                }
                            });
                        }
                    }
                }
            });
        }
     
        private  void loadPath(PatchInfo info, Context ctx, String apkFilePath) {
            if (Utils.isSignEqual(ctx, apkFilePath) && TextUtils.equals(info.apkMd5, Utils.getMd5ByFile(new File(apkFilePath)))) {
                PatchMain.load(ctx, apkFilePath, null);
            }
        }
     
    }

      

        已经提供了实现好的客户端和服务器例子。网址分别是:

        客户端例子实现:https://github.com/fengcunhan/Hotpatch-Sample

       服务器简易实现:https://github.com/fengcunhan/Hotpatch-SimpleServer

        运行例子步骤:

        1.修改 DefaultPatch类中的info.patchApkUrl  的url中的IP替换成你的服务器的IP,将代码部署到tomcat中。

        2.导入Hotpatch-Sample中的工程到Android Studio中。

        3.打开网址 http://服务器IP:服务器端口/PathServer,将patchsample下build/outputs/apk/app-debug.apk上传到服务器上面,一定不要改名字。

        4.修改DefaultPatchInfoRequest 中的hostUrl的IP和端口为你的服务器端IP和端口。  

        5.运行dexposedexamples项目,点击 Check Hotpatch,如果出现 “The dialog is shown from patch apk”,恭喜你,成功运行了Demo。

         Demo中的Patch只是很简单的一个例子,后续会碰到混淆的类怎么怎么写Patch之类的问题,这些都将在以后一一叙述。请大家关注这个Hotpatch系列。     

         Demo中服务器实现很渣渣。请服务器端大神不要喷,可以fork,不要fuck。

  • 相关阅读:
    bzoj1589[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果*
    bzoj1672[Usaco2005 Dec]Cleaning Shifts 清理牛棚*
    bzoj1691[Usaco2007 Dec]挑剔的美食家*
    bzoj1637[Usaco2007 Mar]Balanced Lineup*
    LinkedList源码
    链表
    反向打印链表
    空格替换
    二维数组查找
    待编辑
  • 原文地址:https://www.cnblogs.com/clarence/p/5066590.html
Copyright © 2011-2022 走看看