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

时间:2019-08-23 14:23:18

标签: android react-native android-gradle-plugin

本地社区!

我正在尝试运行应用程序,但我不断收到此神秘错误:

  

com.facebook.react.common.JavascriptException:未定义不是对象(正在评估“ Sn [e]”),

当我使用类“运行”按钮尝试在手机上构建应用程序时(是的,它已连接并被识别),我在Android Studio中收到此错误,并且当我使用终端机来运行react-native run-android

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

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

Full error from adb logcat *:E output

在Android Studio中单击“运行”时,我多次发现此错误。

Error in android studio

在SO上,我只见过一两个类似的问题,例如here

我还尝试了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"
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明,这是我的index.android.bundle文件中的错误,我花了一些时间才知道我可以通过查看{{1}周围的捆绑代码来跟踪错误的来源}。事实证明,此错误来自Sn[e],我必须完成以下步骤:

  1. 遵循完整的react-navigation install guide
    • 安装react-navigationreact-native-gesture-handler(网站上针对您的特定本机版本以及是否使用expo的说明)
    • 在我的react-native-reanimated文件中添加一些导入和新功能
  2. 安装MainActivity.java软件包以解决某些缺失的依赖项(see this SO answer
  3. 使用以下方法重新捆绑我的代码:
    • fbjs