React Native Android 即时热更新bundle 以及增量更新bundle~

本文仅仅提供热更新与热更新的思路
具体实现代码会提供一些核心代码

1. 加载bundle
我加载bundle的方式不是放在RaactActivity中,而是实现ReactApplication,放在mainapplication,正常Android代码也会有一个application是程序启动的一些初始化工作。代码如下:

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        protected boolean getUseDeveloperSupport() {
            return com.HeMingNetwork.ruyipin.recruiter.BuildConfig.DEBUG;
        }

        @Override

        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),);
        }

        @Nullable
        @Override
        protected String getJSBundleFile() {
            if (BuildConfig.DEBUG) {
                return super.getJSBundleFile();
            } else {
                String path = MainApplication.this.getFilesDir().getAbsolutePath() + "/index.android.bundle";
                if (!isFirstByMainActivity) {
                    return super.getJSBundleFile();
                }
                if (Utils.isFileExit(path)) {
                    return MainApplication.this.getFilesDir().getAbsolutePath() + "/index.android.bundle";
                } else {
                    return super.getJSBundleFile();
                }
            }

        }
    };

如果是debug,super.getJSBundleFile()跑的是本地磁盘上的bundle包,release模式下加载的是assets里的index.android.bundle。
属性isFirstByMainActivity判断是不是第一次使用,之后每次加载的都是下载在本地的bundle包,
2. 对比bundle
新建一个AppManager的管理bundle与apk的类,

@ReactMethod
    public void updateAppThods(ReadableMap readableMap) {
        Activity activity = getCurrentActivity();
        SharedPreferences sp = activity.getSharedPreferences("latestVersion",0);
        if (Integer.parseInt(sp.getString("bundelVersion","1")) != Integer.parseInt(readableMap.getString("bundLever"))) {
            NetHelper netHelper = new NetHelper(activity);
            netHelper.downjsBundle();
        } else {
            Log.d("readableArray","相等");
        }
    }

updateAppThods是在js中调用代码,js中有一个定时器定时读取bundle的版本接口,数据格式类似{bundLever:”x”,bundleUrl:”url”,},如果版本号不同,就去下载bundle。
3. 下载bundle

public void downjsBundle(String urlStr) {
        final String fileName = "index.android.bundle";
        final File dir = activity.getFilesDir();
        File file = new File(dir,fileName);
        Log.d(TAG,file.getAbsolutePath());
        if (file.exists()) {
            file.delete();
        }
        OkHttpClient mOkHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(urlStr).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call,IOException e) {
// Toast.makeText(activity,"bundle下载失败",Toast.LENGTH_SHORT).show();
                Log.e("downjsBundle","bundle下载失败");
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        new AlertDialog.Builder(activity).setTitle("网络错误")
                                .setMessage("请检查网络连接")
                                .setPositiveButton("确定",null).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call,Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();
                FileOutputStream fileOutputStream = null;
                try {
                    File bundle = new File(dir,fileName);
                    fileOutputStream = new FileOutputStream(bundle);
                    byte[] buffer = new byte[2048];
                    int len = 0;
                    double fileSize = 0;
                    while ((len = inputStream.read(buffer)) != -1) {
                        fileOutputStream.write(buffer,0,len);
                        fileSize += len;
                    }
                    Log.e("jsbundle",fileSize / 1024 / 1024 + "MB");
                    fileOutputStream.flush();
                    Log.e("downjsBundle","bundle下载成功");
                    Log.d("downjsBundle","bundLeversion==" + bundLeversion);
                    VersionMgr.bundelVersion = bundLeversion;

                    SharedPreferences sp = activity.getSharedPreferences("latestVersion",0);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("bundelVersion",bundLeversion);
                    edit.commit();

                    edit.putString("bundelVersion",VersionMgr.bundelVersion);
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new AlertDialog.Builder(activity).setTitle("有新版本的更新")
                                    .setMessage("需要更新嘛?")
                                    .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,int which) {
                                            activity.runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Intent intent = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName());
                                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                    activity.startActivity(intent);
                                                    activity.finish();
                                                }
                                            });

                                        }
                                    })
                                    .setNegativeButton("取消",null)
                                    .show();

                        }
                    });
                } catch (IOException e) {
                    Log.e("downjsBundle","bundle保存失败" + e.toString());
                    e.printstacktrace();
                }
// activity.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// activity.startActivity(new Intent(activity,MainActivity.class));
// activity.finish();
// }
// });
            }
        });
    }

bundle 并且把版本号存在本地。

4 增量更新 待续~~

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...