React-Native 报错集合及解决方案

1.The SDK directory ‘C:\Users\plankton\AppData\Local\Android\android-sdk’ does not exist.

解决:因为我配置ANDROID_HOME的时候是复制的官网的地址,所以环境变量错误,找到SDK目录,然后重新设置ANDROID_HOME环境变量即可。

2.’adb’ 不是内部或外部命令,也不是可运行的程序或批处理文件

Windows简单粗暴解决方案:添加C:\Users\YOURUSERNAME\AppData\Local\Android\Sdk\platform-toolsPath环境变量,其他请参考Stack Overflow

3.Unable to resolve module AccessibilityInfo from XXX和Modul

这是RN0.56版本的bug,详见issues,解决: 退回版本到0.55yarn remove react-native and yarn add react-native@0.55

4. error: bundling failed: Error: Plugin 0 specified in “C:\Users\Administrator\ Desktop\TestProject\yellowDuck\node_modules\babel-preset-react-native\index .js” provided an invalid property of “default” (While processing preset: “C:\Us…

参考了:CSDN博客,重新安装yarn add babel-preset-react-native@2.1.0

5. Warning: isMounted(…) is deprecated in plain Javascript Classes. Instead, make sure to clean up subscriptions and pending requests in componentWillUnmount to prevent memory leaks.

貌似是个Bug,坐等修复,issue地址
但是不影响运行,也可以忽略,或者不显示警告,index.js文件中添加下面代码:

import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);
6.Package com.yellowduck signatures do not match the previously installed version; ignoring!

这个原因是因为手机未彻底删除应用程序,(我是直接拖到桌面顶部删除),而后来这个打包程序是其他平台打包的(网上说的平台,暂不知是平台还是因为换了台电脑,或者是我安装的Android SDK版本不一样造成),所以签名不一致。解决方法:确保adb devices能看到你的设备,且最好关闭Android虚拟机,使用adb命令: adb uninstall com.你的项目名字(项目名字前面加上com.)

7.React Native version mismatch.
JavaScript version: 0.51.0
Native version: 0.49.5

根据错误提示是因为项目中RN版本过低导致,解决方法:更新项目中的RN版本至最新版本。执行如下命令:
npm install –save react-native@0.51.0
版本不同,构建的项目模板也会发生变化,所以在更新了React Native版本之后,也要及时更新项目模板。
通过如下命令:
react-native upgrade
参考:简书

8.Error:Failed to resolve: android.arch.core:common:1.1.0

原因:升级了react-native版本,没有关闭上一个app的node服务器,在Android目录中的build.gradle中,找到如下代码段

allprojects {
    repositories {
        mavenLocal()
        // 加入下面这行,并且让他在jcenter函数前面运行
        maven { url "https://maven.google.com" }
        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://maven.google.com/'
            name 'Google'
        }
    }
}

参考:stackoverflow

9.Could not find support-vector-drawable.aar (com.android.support:support-vector-drawable:27.1.1).

找到Android目录下build.gradle文件,找到allprojects做如下修改:

allprojects {
    repositories {
        // 加入下面这行,并且让他在jcenter函数前面运行
        maven { url "https://maven.google.com" }
        jcenter()
    }
}
10.Execution failed for task ‘:app:processDebugResources’.

参考: stackoverflow

cd android && gradlew clean

cd .. && react-native run-android

11.Could not find method compile() for arguments [com.facebook.fresco:animated-gif:0.13.0] on object of type org. gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

应该添加在android/app/build.gradledependencies
加入compile 'com.facebook.fresco:animated-gif:1.10.0'

12.使用react-native-fs这个库的时候,报错Cannot read property 'RNFSFileTypeRegular' of undefined

解决:执行以下步骤
1.npm install react-native-fs or yarn add react-native-fs
2.react-native link react-native-fs
详见:issues

13.Flat List – ScrollToIndex should be used in conjunction with getItemLayout or onScrollToIndexFailed
14.Could not resolve all artifacts for configuration ‘:app:releaseStagingRuntimeClasspath’.Could not resolve project:react-native-splash-screen.

原因:第三方包不知道你打包的类型是release还是debug版本,打开android/app/build.gradle文件,添加如下:

    buildTypes {

        release {// 对应Production环境
            ...
            matchingFallbacks = ['release', 'debug']
        }
    }
15.Execution failed for task ‘:app:packageRelease’com.android.ide.common.signing.KeytoolException:Failed to read key meihuo from store “C:\Users\Administrator\Desktop\meihuo.jks”:Keystore was tampered with, or password was incorrect

jks签名问题,集成 bugly 时按照他们给的文档我们应该是把签名放在 build.gradle 里,但是我们直接放进去可以能会有错误,

16.Execution failed for task’:app:transformClassesWithDesugarForDebug’. >com.android.build.api.transform.TransformException: org.gradle.process.internal.ExecException: A problem occurred starting process ‘command ‘C:\Program Files\Java\jdk1.8.0_191\bin\java.exe”

项目升级了classpath 'com.android.tools.build:gradle:3.1.4'gradle版本为3.3后有修改为3.1.4,能够打包,但是打包debug的时候,和安装到虚拟机调试的时候,报这个错
解决
gradle.properties中添加android.enableD8.desugaring = true

16.隐藏开发模式所有黄色警告
import { YellowBox } from "react-native";
console.disableYellowBox = true;
console.warn('YellowBox is disabled.');
17.https://jcenter.bintray.com/ 服务器崩溃502解决办法
  • android/build.gradle文件替换jcenter()
repositories {
    google()
    maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
//    jcenter()

}
allprojects {
    repositories {
        mavenLocal()
        google()
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        // jcenter()
        // jcenter() { url 'http://jcenter.bintray.com/' }

    }
}

18,TextInput设置maxLength长度和value,输入超过限制时,闪烁

textInput组件加入value后就成为了受控组件,不设置value属性,把这个属性的值村于state中,用于做其他操作

    onChangeText = (text) => {
        this.setState({ remarkText:text })
        if (text.length >= 50) {
            Toast.show('最多输入50个字哦')
            return
        }
    }
    <TextInput
        multiline={true}
        onChangeText={this.onChangeText}
        maxLength={11}
    />
19.Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).Suggestion: add ‘tools:replace=”android:appComponentFactory”‘ to element at AndroidManifest.xml:22:5-165:19 to override.
  • 报错信息
Manifest merger failed : 
Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
    is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
    Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:22:5-165:19 to override.

原因:第三库更新了依赖,使用了android x,项目并没有迁移至android x,因为android x的语法更严格,不允许重复的属性,所以报错提示建议替换属性

解决:android/app文件夹中build.gradle添加

 project.ext {
    googlePlayServicesVersion = "15.0.0"
 }
20.Error: Activity class {com.wyapp/com.wyapp.MainActivity} does not exist.
  • 暂未解决
21.SDK location not found. Define location with sdk.dir in the local
  • 进入android项目目录,在此目录下新建local.properties文件,内容为:
    /Users/用户名/Library/Android/sdk

  • vim ~/.bash_profile

  • 加入: export ANDROID_HOME=/Users/用户名/Library/Android/sdk
  • source ~/.bash_profile
点赞