ReactNative javascript 在开发环境中使用错误版本捆绑构建

ReactNative javascript bundle build with wrong version in dev environment

我有一个使用 RN 0.49.5 开发的 React Native 应用程序。我不得不将它升级到最新的 react-native 版本(现在是 0.59.5)。

尽管 运行 在开发模式下我遇到了问题。有趣的是,如果我为发布 (cd android && ./gradlew assembleRelease) 构建它,它工作正常。应用程序一启动就崩溃,logcat 显示以下错误:

2019-05-16 14:36:56.504 10313-10425/com.devorg.app E/AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
    Process: com.devorg.app, PID: 10313
    com.facebook.react.common.JavascriptException: React Native version mismatch.

    JavaScript version: 0.49.5
    Native version: 0.59.5

    Make sure that you have rebuilt the native code. If the problem persists try clearing the watchman and packager caches with `watchman watch-del-all && react-native start --reset-cache`., stack:
    <unknown>@62:1035
    i@2:553
    n@2:266
    global code@316:9

        at com.facebook.react.modules.core.ExceptionsManagerModule.showOrThrowError(ExceptionsManagerModule.java:54)
        at com.facebook.react.modules.core.ExceptionsManagerModule.reportFatalException(ExceptionsManagerModule.java:38)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:158)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
        at android.os.Looper.loop(Looper.java:201)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl.run(MessageQueueThreadImpl.java:232)
        at java.lang.Thread.run(Thread.java:764)
2019-05-16 14:36:56.521 10313-10424/com.devorg.app E/ReactNativeJS: Requiring module "59", which threw an exception: Error: React Native version mismatch.

    JavaScript version: 0.49.5
    Native version: 0.59.5

    Make sure that you have rebuilt the native code. If the problem persists try clearing the watchman and packager caches with `watchman watch-del-all && react-native start --reset-cache`.

relevant files are package.json:

{
  "name": "DevOrgApp",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "@babel/core": "^7.4.4",
    "@babel/runtime": "^7.4.4",
    "babel-jest": "^24.7.1",
    "jest": "^24.7.1",
    "metro-react-native-babel-preset": "^0.53.1",
    "react-test-renderer": "16.8.3"
  },
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "test": "node node_modules/jest/bin/jest.js --watch"
  },
  "jest": {
    "preset": "react-native"
  },
  "dependencies": {
    "hoist-non-react-statics": "^2.5.0",
    "react": "16.8.3",
    "react-native": "0.59.5",
    "react-native-button": "^2.1.0",
    "react-native-i18n": "^2.0.10"
  }
}

android/app/build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        release {
            ...
        }
    }

    defaultConfig {
        applicationId "com.devorg.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 14
        versionName "0.94"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }

    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"

            signingConfig signingConfigs.release
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])

    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules

    // required
    compile 'io.branch.sdk.android:library:2.+'

    // optional
    compile 'com.android.support:customtabs:26.+' // Chrome Tab matching

    compile project(':react-native-i18n')
    compile project(':mstblelib')    
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

android/build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 19
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
    }
}

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }

        flatDir {
            dirs "$rootDir/libs"
        }
    }
}

关闭所有 node terminals。这通常意味着您有一些终端 运行 react-native。我会推荐 restart 电脑,最简单。

如果这不起作用,请尝试 delete 您的 /node_modules 文件夹并在终端中执行:

 1. npm cache verify
 2. npm install

然后使用您的 react-native run-android 命令。