React Native 开发 Can't find variable:Symbol 错误解决方案

hxy    2021-01-20 19:46

描述:一个RN的项目,运行没问题,升级了一些库之后再次运行出现了 can't find variable:Symbol 提示

原因:mobx的版本用的最新版本..用到了 Symbol部分es6的api特性.

解决办法:
1.  在 /package.json 文件中 dependencies 添加  "jsc-android": "236355.1.1",(新加内容在第14行)修改后的文件如下:
{
  "name": "Hanking",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "bundle-win-android": "cd android && gradlew assembleRelease",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "jsc-android": "236355.1.1",
    "@babel/plugin-proposal-decorators": "^7.8.3",
    "@react-native-community/async-storage": "^1.7.1",
    "aurora-imui-react-native": "^0.14.0",
    "axios": "^0.19.2",
    "crypto-js": "3.3.0",
    "gojs": "^2.1.33",
    "gojs-react": "^1.0.10",
    "immutable": "^4.0.0-rc.12",
    "metro-react-native-babel-preset": "^0.64.0",
    "native-echarts": "^0.5.0",
    "react": "16.6.1",
    "react-devtools": "^4.10.1",
    "react-native": "0.57.7",
    "react-native-actionsheet": "^2.4.2",
    "react-native-elements": "^1.2.7",
    "react-native-fs": "^2.16.4",
    "react-native-gesture-handler": "1.0.16",
    "react-native-image-crop-picker": "0.24.0",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-root-toast": "^3.2.0",
    "react-native-storage": "^1.0.1",
    "react-native-vector-icons": "^6.6.0",
    "react-navigation": "3.1.1",
    "react-redux": "5.0.6",
    "redux": "^4.0.5",
    "redux-persist": "^6.0.0",
    "redux-persist-transform-immutable": "^5.0.0",
    "redux-thunk": "^2.3.0",
    "socket.io-client": "^2.3.0",
    "whatwg-fetch": "^2.0.4"
  },
  "devDependencies": {
    "babel-eslint": "^10.1.0",
    "babel-jest": "25.1.0",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.22.0",
    "jest": "25.1.0",
    "prettier-eslint": "^12.0.0",
    "react-test-renderer": "16.6.1",
    "reactotron-react-native": "^4.0.3",
    "reactotron-redux": "^3.1.3"
  },
  "jest": {
    "preset": "react-native"
  }
}
保存后,进入项目目录,执行

npm install

执行了两次才成功

2. 在/android/build.gradle文件添加以下内容:
maven {
      // Local Maven repo containing AARs with JSC library built for Android
      url "$rootDir/../node_modules/jsc-android/dist"
}
完整的 /android/build.gradle 内容如下(新加内容在33行-26行内容):
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'

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

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"
        }
        maven { url "https://jitpack.io" }
        maven {
              // Local Maven repo containing AARs with JSC library built for Android
               url "$rootDir/../node_modules/jsc-android/dist"
        }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

3. 在/android/app/build.gradle 文件下添加:
configurations.all {
    resolutionStrategy {
        force 'org.webkit:android-jsc:r236355'
    }
}
完整的build.gradle文件内容如下(修改部分见148行~152行):
apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation
 *   entryFile: "index.android.js",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

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

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 = true

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

    defaultConfig {
        applicationId "com.weixin"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    signingConfigs {
       release {
        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
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // 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
            }
        }
    }
}

configurations.all {
    resolutionStrategy {
        force 'org.webkit:android-jsc:r236355'
    }
}

dependencies {
    compile project(':aurora-imui-react-native')
    compile project(':react-native-image-crop-picker')
    compile project(':@react-native-community_async-storage')
    compile project(':react-native-linear-gradient')
    compile project(':react-native-fs')
    compile project(':react-native-vector-icons')
    compile project(':react-native-gesture-handler')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
}

// 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'
}

保存,在项目根目录下 / 运行 yarn andorid 即可。
 







 
Last Modified: 2021-03-10 10:34
Views: 864

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment