React-Native:undefined 不是一个对象(评估 'Sn[e]')

React-Native: undefined is not an object (evaluating 'Sn[e]')

原生社区!

我正在尝试 运行 一个应用程序,但我一直收到这个神秘错误:

com.facebook.react.common.JavascriptException: undefined is not an object (evaluating 'Sn[e]'),

当我使用 class 运行 按钮尝试在我的 phone 上构建应用程序时,我在 Android Studio 中遇到此错误(是的,它已连接并已识别),当我使用终端 运行 react-native run-android.

时,我也会收到此错误

当我 运行 / 构建应用程序时,它会在我的 phone 上安装 apk,但每次启动时都会立即崩溃。

在终端中使用 adb logcat *:E 时,我发现此错误至少出现了 6 次:

我在 Android Studio 中单击 "Run" 时多次发现此错误。

我之前只见过一两个类似的问题,例如 here on SO,或者

我也尝试了 this SO question 中的 react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/,但它对我也不起作用。

这是我的主要 gradle 文件:

android/build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

android/app/build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile

def useIntlJsc = false

project.ext.react = [ entryFile: 'index.android.js', enableHermes: false ]

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

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        applicationId "com.my.app.id"
        minSdkVersion 16
        targetSdkVersion 26
        multiDexEnabled true
        versionCode 9
        versionName "2.0.6"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation project(':react-native-linear-gradient')
    implementation project(':react-native-google-analytics-bridge')
    implementation project(':react-native-mapbox-gl') {
        implementation ('com.squareup.okhttp3:okhttp:3.6.0') {
            force = true
        }
    }
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation "androidx.appcompat:appcompat:1.0.2"
    implementation "com.google.android.material:material:1.0.0"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation   "com.facebook.react:react-native:+"  // From node_modules
    implementation(project(':react-native-geolocation-service')) {
        exclude group: 'com.google.android.gms', module: 'play-services-location'
    }
    implementation 'com.google.android.gms:play-services-location:17.0.0'

    if (useIntlJsc) {
        implementation 'org.webkit:android-jsc-intl:+'
    } else {
        implementation 'org.webkit:android-jsc:+'
    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

和我的 package.json,以防有帮助:

{
    "name": "pkg",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "run:android": "./gradlew installDebug && npm run start:android",
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "test": "jest"
    },
    "dependencies": {
        "@turf/buffer": "^5.1.5",
        "@turf/collect": "^5.1.5",
        "@turf/turf": "^5.1.6",
        "react": "^16.9.0",
        "react-native": "^0.60.5",
        "react-native-geolocation-service": "^3.0.0",
        "react-native-google-analytics-bridge": "^5.3.3",
        "react-native-indicators": "^0.13.0",
        "react-native-lightbox": "^0.7.0",
        "react-native-linear-gradient": "^2.5.6",
        "react-native-permissions": "^1.1.1",
        "react-native-phone-call": "^1.0.4",
        "react-native-progress": "^3.4.0",
        "react-native-search-bar": "^3.0.0",
        "react-navigation": "^3.11.0",
        "react-redux": "^7.1.0",
        "redux": "^4.0.4",
        "redux-actions": "^2.0.3",
        "redux-logger": "^3.0.6",
        "redux-persist": "^5.10.0",
        "redux-thunk": "^2.3.0"
    },
    "devDependencies": {
        "@babel/core": "^7.5.5",
        "@babel/preset-flow": "^7.0.0",
        "babel-eslint": "^10.0.2",
        "babel-jest": "^24.1.0",
        "babel-plugin-module-resolver": "^3.2.0",
        "eslint": "^3.19.0",
        "eslint-config-airbnb": "^15.0.1",
        "eslint-config-import": "^0.13.0",
        "eslint-plugin-import": "^2.3.0",
        "eslint-plugin-jsx-a11y": "^5.0.3",
        "eslint-plugin-react": "^7.0.1",
        "eslint-plugin-react-native": "^2.3.2",
        "jest": "24.1.0",
        "metro-react-native-babel-preset": "^0.56.0",
        "react-native-htmlview": "^0.14.0",
        "react-test-renderer": "16.6.3"
    },
    "jest": {
        "preset": "react-native"
    }
}

事实证明,这是我的 index.android.bundle 文件中的一个错误,我花了一段时间才弄清楚我可以通过查看 [=] 周围的捆绑代码来追踪错误来自哪个包11=]。事实证明,这个错误来自 react-navigation,我必须完成以下步骤:

  1. 关注完整react-navigation install guide
    • 安装 react-native-gesture-handlerreact-native-reanimated(网站上有针对您的特定 react-native 版本以及您是否使用 expo 的说明)
    • 向我的 MainActivity.java 文件添加一些导入和新功能
  2. 安装 fbjs 包以解决一些缺少的依赖项 (see this SO answer)
  3. 重新捆绑我的代码:
    • react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/