react-native 热更新(android)

react-native 热更新(android)

react-native代码分为android部分,js部分,图片资源文件
其中android部分代码是不支持热更新的,支持更新的只有js代码和图片资源文件
react项目生成app时,所有js文件都被压缩到index.android.bundle文件中,该文件和图片资源都位于assets目录下,app启动时,MainActivity首先加载index.android.bundle,转换为对应的原生试图显示到rootView
所以,react热更新就是在app启动时从服务器下载新的index.android.bundle和图片资源,然后加载新的index.android.bundle文件

更新index.android.bundle

react-native提供了getJSBundleFile()来修改index.android.bundle文件的加载路径,复写该方法,返回本地更新后的index.android.bundle文件路径

0.29及以后版本:在你的MainApplication中增加如下代码:

    import cn.reactnative.modules.update.UpdateContext;
    public class MainApplication extends Application implements ReactApplication {
        public static final String JS_BUNDLE_LOCAL_PATH = Environment.getExternalStorageDirectory().toString() + File.separator + "patches/index.android.bundle";
        private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
            @Override
            protected String getJSBundleFile() {
              File file = new File(JS_BUNDLE_LOCAL_PATH);
              if(file != null && file.exists()){
                return JS_BUNDLE_LOCAL_PATH;
              }else{
                return super.getJSBundleFile();
              }
        }
        // ... 其它代码 
        }
    }

0.28及以前版本:在你的MainActivity中增加如下代码:

    // ... 其它代码
    import cn.reactnative.modules.update.UpdateContext;
    public class MainActivity extends ReactActivity {
        public static final String JS_BUNDLE_LOCAL_PATH = Environment.getExternalStorageDirectory().toString() + File.separator + "patches/index.android.bundle";
        @Override
        protected String getJSBundleFile() {
            File file = new File(JS_BUNDLE_LOCAL_PATH);
              if(file != null && file.exists()){
                return JS_BUNDLE_LOCAL_PATH;
              }else{
              
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值