android 版的 assets 图集资源,react-native 热更新(android)

本文详细介绍了如何在Android版的react-native应用中实现热更新,包括修改`getJSBundleFile()`方法加载本地更新的index.android.bundle,下载并解压更新包,以及实现增量更新的方法,如使用google-diff-match-patch生成和应用补丁文件,以减少更新体积和时间。
摘要由CSDN通过智能技术生成

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{

return super.getJSBundleFile();

}

}

// ... 其它代码

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值