为了账号安全,请及时绑定邮箱和手机立即绑定

android:一步步实现插件化与热更新(二)

标签:
Android

接着android:一步步实现插件化与热更新(一)的内容继续下面的操作

六、插件化更新

  1. 修改代码

    我修改的是app.chat中FromHomeActivity

    在参数传递过来的时候,弹个吐司:
    
    UIUtils.showToast("name=" + name + ",age=" + age);
  2. 记得修改版本号:

     versionCode 2   1——>2
      versionName "1.1"  1.0->1.1
    
    注意你修改的哪个插件修改哪个插件的版本即可,不是宿主app模块,我这里修改的是app.chat
  3. 两步:buildLib->buildBundle
  4. 查找so,在你的宿主模块中
    图片描述

  5. 部署服务器,我直接在我的电脑上部署一个tomcat
    图片描述

  6. 服务器的bundle.json

    {
      "manifest": {
        "bundles": [
          {
            "pkg": "tsou.cn.lib.data",
            "uri": "lib.data"
          },
          {
            "pkg": "tsou.cn.lib.utils",
            "uri": "lib.utils"
          },
          {
            "pkg": "tsou.cn.lib.style",
            "uri": "lib.style"
          },
          {
            "pkg": "tsou.cn.lib.layout",
            "uri": "lib.layout"
          },
          {
            "pkg": "tsou.cn.lib.icon",
            "uri": "lib.icon"
          },
          {
            "pkg": "tsou.cn.app.home",
            "uri": "home"
          },
          {
            "pkg": "tsou.cn.app.chat",
            "rules": {
              "FromHomeActivity": "tsou.cn.app.chat.activity.FromHomeActivity"
            },
            "uri": "chat"
          },
          {
            "pkg": "tsou.cn.app.recom",
            "uri": "recom"
          },
          {
            "pkg": "tsou.cn.app.me",
            "uri": "me"
          }
        ],
        "version": "1.0.0"
      },
      "updates": [
        {
          "pkg": "tsou.cn.app.chat",
          "url": "http://192.168.19.125:8080/json/libtsou_cn_app_chat.so"
        }
      ]
    }
  7. 实现插件化更新

    /**
     * 插件化更新
     */
    private void checkUpgrade() {
        new UpgradeManager(getContext()).checkUpgrade();
    }

    private static class UpgradeManager {

        private static class UpdateInfo {
            public String packageName;
            public String downloadUrl;
        }

        private static class UpgradeInfo {
            public JSONObject manifest;
            public List<UpdateInfo> updates;
        }

        private interface OnResponseListener {
            void onResponse(UpgradeInfo info);
        }

        private interface OnUpgradeListener {
            void onUpgrade(boolean succeed);
        }

        private static class ResponseHandler extends Handler {
            private OnResponseListener mListener;

            public ResponseHandler(OnResponseListener listener) {
                mListener = listener;
            }

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        mListener.onResponse((UpgradeInfo) msg.obj);
                        break;
                }
            }
        }

        private ResponseHandler mResponseHandler;

        private Context mContext;
        private ProgressDialog mProgressDlg;

        public UpgradeManager(Context context) {
            mContext = context;
        }

        public void checkUpgrade() {
            mProgressDlg = ProgressDialog.show(mContext, "Small", "检查更新...", false, true);
            requestUpgradeInfo(Small.getBundleVersions(), new OnResponseListener() {
                @Override
                public void onResponse(UpgradeInfo info) {
                    mProgressDlg.setMessage("升级中...");
                    upgradeBundles(info,
                            new OnUpgradeListener() {
                                @Override
                                public void onUpgrade(boolean succeed) {
                                    mProgressDlg.dismiss();
                                    mProgressDlg = null;
                                    String text = succeed ?
                                            "升级成功!切换到后台并返回到前台来查看更改"
                                            : "升级失败!";
                                    UIUtils.showToast(text);
                                }
                            });
                }
            });
        }

        /**
         * @param versions
         * @param listener
         */
        private void requestUpgradeInfo(Map versions, OnResponseListener listener) {
            System.out.println(versions); // this should be passed as HTTP parameters
            mResponseHandler = new ResponseHandler(listener);
            ThreadUtils.runOnLongBackThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // Example HTTP request to get the upgrade bundles information.
                        // Json format see http://wequick.github.io/small/upgrade/bundles.json
                        URL url = new URL("http://192.168.19.125:8080/json/bundle.json");
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        StringBuilder sb = new StringBuilder();
                        InputStream is = conn.getInputStream();
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = is.read(buffer)) != -1) {
                            sb.append(new String(buffer, 0, length));
                        }

                        // Parse json
                        JSONObject jo = new JSONObject(sb.toString());
                        JSONObject mf = jo.has("manifest") ? jo.getJSONObject("manifest") : null;
                        JSONArray updates = jo.getJSONArray("updates");
                        int N = updates.length();
                        List<UpdateInfo> infos = new ArrayList<UpdateInfo>(N);
                        for (int i = 0; i < N; i++) {
                            JSONObject o = updates.getJSONObject(i);
                            UpdateInfo info = new UpdateInfo();
                            info.packageName = o.getString("pkg");
                            info.downloadUrl = o.getString("url");
                            infos.add(info);
                        }

                        // Post message
                        UpgradeInfo ui = new UpgradeInfo();
                        ui.manifest = mf;
                        ui.updates = infos;
                        Message.obtain(mResponseHandler, 1, ui).sendToTarget();
                    } catch (Exception e) {
                        e.printStackTrace();
                        ThreadUtils.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProgressDlg.dismiss();
                                mProgressDlg = null;
                                UIUtils.showToast("更新失败");
                            }
                        });
                    }
                }
            });
        }

        private static class DownloadHandler extends Handler {
            private OnUpgradeListener mListener;

            public DownloadHandler(OnUpgradeListener listener) {
                mListener = listener;
            }

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        mListener.onUpgrade((Boolean) msg.obj);
                        break;
                }
            }
        }

        private DownloadHandler mHandler;

        private void upgradeBundles(final UpgradeInfo info,
                                    final OnUpgradeListener listener) {
            // Just for example, you can do this by OkHttp or something.
            mHandler = new DownloadHandler(listener);
            ThreadUtils.runOnLongBackThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // Update manifest
                        if (info.manifest != null) {
                            if (!Small.updateManifest(info.manifest, false)) {

                                Message.obtain(mHandler, 1, false).sendToTarget();
                                return;
                            }
                        }
                        // Download bundles
                        List<UpdateInfo> updates = info.updates;
                        for (UpdateInfo u : updates) {
                            // Get the patch file for downloading
                            net.wequick.small.Bundle bundle = Small.getBundle(u.packageName);
                            File file = bundle.getPatchFile();

                            // Download
                            URL url = new URL(u.downloadUrl);
                            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
                            InputStream is = urlConn.getInputStream();
                            OutputStream os = new FileOutputStream(file);
                            byte[] buffer = new byte[1024];
                            int length;
                            while ((length = is.read(buffer)) != -1) {
                                os.write(buffer, 0, length);
                            }
                            os.flush();
                            os.close();
                            is.close();

                            // Upgrade
                            bundle.upgrade();
                        }
                        Message.obtain(mHandler, 1, true).sendToTarget();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Message.obtain(mHandler, 1, false).sendToTarget();
                        ThreadUtils.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProgressDlg.dismiss();
                                mProgressDlg = null;
                                UIUtils.showToast("更新失败");
                            }
                        });
                    }
                }
            });
        }
    }

到此你就可以使用small进行插件化开发了........

当你没有使用插件化框架时该如何热更新:android:一步步实现插件化与热更新(三)

点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
50
获赞与收藏
188

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消