下载依赖 我的依赖是 下载完后 如果有ios的需求话需要对ios进行 cd ios && pod install

"react-native-update": "^8.1.0"

在android代码里的android/app/src/main/java/MainApplication.kt

import cn.reactnative.modules.update.UpdateContext


class MainApplication : Application(), ReactApplication {

  override val reactNativeHost: ReactNativeHost =
      object : DefaultReactNativeHost(this) {
        override fun getJSBundleFile(): String? {
          return UpdateContext.getBundleUrl(this@MainApplication)
        }
        override fun getPackages(): List<ReactPackage> =
            PackageList(this).packages.apply {
              // Packages that cannot be autolinked yet can be added manually here, for example:
              // add(MyReactNativePackage())
            }

        override fun getJSMainModuleName(): String = "index"

        override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG

        override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
        override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
      }

  override val reactHost: ReactHost
    get() = getDefaultReactHost(this.applicationContext, reactNativeHost)

  override fun onCreate() {
    super.onCreate()
    SoLoader.init(this, false)
    if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
      // If you opted-in for the New Architecture, we load the native entry point for this app.
      load()
    }
    ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager)
  }
}

在ios下的AppDelegate.mm

#import "RCTPushy.h"
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
  // return [self getBundleURL];
  #if DEBUG
  // 原先DEBUG这里的写法不作修改(所以DEBUG模式下不可热更新)
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
  return [RCTPushy bundleURL];  // <--  把这里非DEBUG的情况替换为热更新bundle
#endif
}

在reactNative中调用代码

import {
  checkUpdate,
  downloadUpdate,
  switchVersion,
  isFirstTime,
  isRolledBack,
  markSuccess,
  switchVersionLater
} from 'react-native-update';
import _updateConfig from '../../../update.json';
const { appKey } = (_updateConfig as any)[Platform.OS];

useEffect(() => { 
    checkPatch();

    if (isFirstTime) {
      markSuccess();
      // 补丁成功,上报服务器信息
      // 补丁安装成功率:99.5% ~ 99.7%
    } else if (isRolledBack) {
      // 补丁回滚,上报服务器信息
    }
  }, []);

// {"forceUpdate":true}
  const checkPatch = async () => {
    const info: any = await checkUpdate(appKey);
    const { update, name, description, metaInfo } = info;
    const metaJson = JSON.parse(metaInfo);
    save('patchVersion', name);
    const { forceUpdate } = metaJson;
    if (forceUpdate) {
      // 弹窗提示用户
    } else {
      // 不弹窗默默操作
    }
    if (update) {
      const hash = await downloadUpdate(
        info,
        {
          onDownloadProgress: ({ received, total }) => { },
        },
      );
      if (hash) {
        if (forceUpdate) {
          switchVersion(hash);
        } else {
          switchVersionLater(hash);
        }
      }
    }
  }

流程

  1. 第一步 打包android yarn android-build
  2. 第二步 给模拟器安装 adb install ./android/app/build/outputs/apk/release/app-release.apk
  3. 第三步 发布到热更新后台 pushy uploadApk ./android/app/build/outputs/apk/release/app-release.apk
  4. 第四部 上传修改后的apk pushy bundle --platform android  
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐