美文网首页
[CodePush] Unable to get the has

[CodePush] Unable to get the has

作者: 莫帆海氵 | 来源:发表于2020-11-05 14:47 被阅读0次

    问题日志如下

    10-29 23:15:13.910 24916 31490 I ReactNativeJS: [CodePush] Downloading package.
    10-29 23:15:14.570 24916 31489 D ReactNative: [CodePush] Applying full update.
    10-29 23:15:14.577 24916 31490 I ReactNativeJS: [CodePush] Installing update.
    10-29 23:15:14.612 24916 31491 D ReactNative: [CodePush] Loading JS bundle from "assets://index.android.bundle"
    10-29 23:15:14.612 24916 24916 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
    10-29 23:15:14.614 24916 31490 I ReactNativeJS: [CodePush] Restarting app
    10-29 23:15:14.615 24916 24916 D ReactNative: ReactInstanceManager.onJSBundleLoadedFromServer()
    10-29 23:15:14.616 24916 24916 D ReactNative: ReactInstanceManager.recreateReactContextInBackground()
    10-29 23:15:14.616 24916 24916 D ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
    10-29 23:15:14.616 24916 24916 D ReactNative: ReactInstanceManager.tearDownReactContext()
    10-29 23:15:14.626 24916 24916 D ReactNative: CatalystInstanceImpl.destroy() start
    10-29 23:15:14.626 24916 24916 W unknown:ReactNative: Packager connection already open, nooping.
    10-29 23:15:14.627 24916 31539 D ReactNative: ReactInstanceManager.createReactContext()
    10-29 23:15:14.632 24916 31539 D ReactNative: [CodePush] Unable to get the hash of the binary's bundled resources - "codepush.gradle" may have not been added to the build definition.
    10-29 23:15:14.632 24916 31539 D ReactNative: Initializing React Xplat Bridge.
    10-29 23:15:14.635 24916 31539 D ReactNative: Initializing React Xplat Bridge before initializeBridge
    10-29 23:15:14.638 24916 31539 D ReactNative: Initializing React Xplat Bridge after initializeBridge
    10-29 23:15:14.638 24916 31539 D ReactNative: CatalystInstanceImpl.runJSBundle()
    10-29 23:15:14.639 24916 31543 D ReactNative: ReactInstanceManager.setupReactContext()
    10-29 23:15:14.639 24916 31543 D ReactNative: CatalystInstanceImpl.initialize()
    10-29 23:15:14.639 24916 31543 D ReactNative: ReactInstanceManager.attachRootViewToInstance()
    10-29 23:15:14.642 24916 24916 W unknown:ReactNative: Packager connection already open, nooping.
    

    环境

    • native 和 RN 混合开发
    • react-native 0.59.9
    • react-native-code-push 5.6.0

    问题描述

    1. 从 native 中打开 RN 页面
    2. RN 页面中通过 codepush 检测到有更新包,弹框提示更新
    3. 点击下载后,更新完成后自动刷新页面
    4. 再次进入 RN 页面依然显示旧的版本

    解决步骤

    1. 首先根据提示找到错误信息是 codepush 生成的文件不存在,然后经调试发现每次下载资源包和安装都是成功的,文件也生成,只是在刷新后重启会自动清除这个目录

    native 中 codepush 安装资源包的目录如下

    /data/user/0/com.xxx.mall/files/CodePush/c402ff3de01546e2d364f692aa06a38342a53d63a3bb0be8a60479539ef8fd55/CodePush/index.android.bundle
    
    1. 然后就定位哪里有清空目录的操作,进一步查找原因是 CodePush getJSBundleFileInternal 获取当前 bundle 文件的时候,每次比较版本号不匹配就清空目录

    codepush meta data 对应 app.json 的内容

    {
        "deploymentKey": "****",
        "label": "v13",
        "packageSize": 474757,
        "isPending": false,
        "description": "test_2100",
        "appVersion": "1.9.0",
        "isMandatory": false,
        "packageHash": "c402ff3de01546e2d364f692aa06a38342a53d63a3bb0be8a60479539ef8fd55",
        "downloadUrl": "https:\/\/file.xxx.com\/mt_code_push\/Fn1KPV7TWLWkjoyM6c_YqfW08Axk",
        "failedInstall": false,
        "binaryModifiedTime": "1604052522083",
        "bundlePath": "\/CodePush\/index.android.bundle"
    }
    

    比如下面的版本信息

    native version = 1.9.0.dev
    codepush appVersion = 1.9.0
    
    1. 基于上面的版本信息,codepush 通过 hasBinaryVersionChanged 完全匹配这两个版本信息,不匹配就清除目录
    // The binary version is newer.
    this.mDidUpdate = false;
    if (!this.mIsDebugMode || hasBinaryVersionChanged(packageMetadata)) {
        this.clearUpdates();
    }
    
    private boolean hasBinaryVersionChanged(JSONObject packageMetadata) {
        String packageAppVersion = packageMetadata.optString("appVersion", null);
        return !sAppVersion.equals(packageAppVersion);
     }
    

    问题原因

    出现这个问题是因为 code push server 不支持三位以上的版本号
    我们的 app 版本号可能有 1.9.0.dev 或 1.9.0.1 或 1.9.0.1.dev
    这种版本号 code push server 计算更新哪个资源包的时候会识别不匹配,获取不到更新的资源包
    所以我们调整 code push server 处理版本号的逻辑,最多只截取前三位
    这样造成接口响应中的 appVersion 和 当前 app 版本号匹配不上 hasBinaryVersionChanged=false 直接舍弃了当前的更新资源包

    • 修改 code push server updateCheck 接口响应中的 appVersion,保持原样返回
    • 或者确保版本号都符合 1.9.0 这种规则也可以使用

    相关文章

      网友评论

          本文标题:[CodePush] Unable to get the has

          本文链接:https://www.haomeiwen.com/subject/rnaovktx.html