React Native android 常见错误和解决方法

邱秋 • 2019年04月07日 • 阅读:2235 • react react-native

Unknown argument type 'attribute' in method -[RCTAppState getCurrentAppState:error:]. Extend RCTConvert to support this type.

这个 BUGXcode.11 引起的, 可以查看这个问题的提交记录,链接为:https://github.com/facebook/react-native/issues/25138 我们只需要找到 RCTModuleMethod.mm 这个文件
修改

static BOOL RCTParseUnused(const char **input)
{
  return RCTReadString(input, "__unused") ||

         RCTReadString(input, "__attribute__((__unused__))") ||
         RCTReadString(input, "__attribute__((unused))");

}

这个函数插入 RCTReadString(input, "attribute((unused))") || 这行代码重新运行就行了。 这样项目就能启动了,而且打包的 app 也不会闪退了。

Could not determine java version from '11.0.1'.

更新 gradle 即可, 可参考 gradle 版本更新, 或者 JDK降级到 8 , 请参考Mac 安装和卸载 Java JDK

static BOOL RCTParseUnused(const char **input)
{
  return RCTReadString(input, "__unused") ||
         RCTReadString(input, "__attribute__((__unused__))") ||
         RCTReadString(input, "__attribute__((unused))");
}

这个函数插入 RCTReadString(input, "**attribute**((**unused**))") || 这行代码重新运行就行了。 这样项目就能启动了,而且打包的 app 也不会闪退了。

Cannot add task 'wrapper' as a task with that name already exists.

task wrapper(type: Wrapper) {

    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")

}
//修改为
wrapper {

    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")

}

Failed to install the following Android SDK packages as some licences have not been accepted.

cd /Users/[usre]/Library/Android/sdk
cd tools/bin
# 以上三行是进入到自己到sdk目录文件夹下

sudo ./sdkmanager --licenses #执行这句才是关键,它会提示你去允许认证

#然后命令会提醒你确定y/n(accept lincense),一路y下去便可以了。
#假如看到以下
Exception in thread "main" java.lang. NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

1. 点击 `File` > `Settings`(在 `Mac` 中,点击 `Android Studio` > `Preferences`),打开 `Preferences` 窗口。
2. 在左侧面板中,点击 `Appearance` & `Behavior` > `System Settings` > `Updates`。
3. 确保已选中 `Automatically check for updates`,然后从下拉列表中选择一个渠道(见图 1)。
4. 点击 `Apply` 或 `OK`。

大概意思就是你把`Android Studio` 更新到最新版本就行了. 点那个`check now` , 直接更新

Could not install the app on the device, read the error above for details.

  • create (or edit) ~/.bashrc
  • export ANDROID_HOME=/Users/你的用户名/Library/Android/sdk
  • export PATH=${PATH}:${ANDROID_HOME}/tools
  • chmod 755 android/gradlew

org.gradle.api.resources. ResourceException: Could not get resource

Mac 中,点击 Android Studio > Preferences ),打开 Preferences 窗口。 Build,Execution,Deployment => Gradle => Android Studio , 勾选 Enable embedded Maven repository

Could not resolve all files for configuration ':app:releaseCompileClasspath

  1. 修改 android/build.gradlegoogle()方法的位置,见下方代码注释;
allprojects {

    repositories {
        google() // new 移到这里来
        mavenLocal()
        jcenter()
        maven {
            url "https://jitpack.io"
        }
        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"
        }
        // google() // old
    }

}
  1. 如果还不行,就升级 gradle 版本到 4.10.1

Failed to capture snapshot of input files for task ':app:javaPreCompileDebug

appbuild

android {
  defaultConfig {

    //添加如下配置就OK了
    javaCompileOptions {
      annotationProcessorOptions { includeCompileClasspath = true }
    }

  }
}

Execution failed for task ':react-native-yunpeng-alipay:compileDebugJavaWithJavac'.

打开 ReactNative 根目录下的 node_modules 文件夹 找到 react-native-yunpeng-alipay 文件夹并打开 依次打开目录: android -> src -> main -> java -> com -> yunpeng -> alipay -> AlipayPackage.java

注释调这一行;

//    @Override
//    public List<Class<? extends JavaScriptModule>> createJSModules() {
//        return Collections.emptyList();
//    }

Execution failed for task 'xxx:verifyReleaseResources'

0.57 版本打包错误,SDK 版本不匹配问题

    1. 首先在 node_modules 中找到报错的包里面的 build.gradle,比如我这个就是\node_modules\react-native-version-number\android\build.gradle
    1. 修改这个build.gradle,使其与 android/build.gradle(也可能是 android/app/build.gradle)里面的 SDK 版本保持一致;
    1. build.gradle 里的 compile 改为 implementation,因为 compile 已过时。
android {

    compileSdkVersion 27 // 23 -> 27
    buildToolsVersion "27.0.3" // 23.0.1 -> 27.0.3

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 26 // 22 -> 26
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    lintOptions {
       warning 'InvalidPackage'
    }

}
dependencies {

    implementation 'com.facebook.react:react-native:+' // compile -> implementation

}

Execution failed for task ':app:processDebugManifest'

已经提示了合并错误
Manifest merger failed with multiple errors, see logs
打开 AndroidMainifest.xml => 点底部的 Merged Mainfest => 看到右边的合并错误, 点进去, 注释掉重复的就可以了 http://yifeng.studio/2017/07/09/android-manifest-merge-conflict/

Execution failed for task ':appbase:installDebugAndroidTest'.

快速定义错误, 进入终端输入
./gradlew compileDebugJavaWithJavac --stacktrace 看看哪里报错了, 修正即可

Could not resolve com.squareup.okhttp3:okhttp:3.9.1.

mavenCentral()
google()
jcenter()
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }

Minimum supported Gradle version is 4.6. Current version is 4.1.

这个问题是 GradleGradle 插件版本对应不上造成的
gradle 插件版本配置位置:project 对应的 build.gradle 文件中

buildscript {

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

}

gradle 版本配置位置:
gradle/wrapper/gradle-wrapper.properties 文件中

distributionUrl=http\://services.gradle.org/distributions/gradle-4.1-all.zip

版本对应关系如下

很明显对应不上,gradle 4.1 应该对应 3.0.0classpath 对应的 3.3.0 改为 3.0.0 即可

Failed to find Build Tools revision 26.0.2

底下报错会提示你安装构建工具, 你安装即可. 不要手贱点 update.

本文将持续更新...

我,秦始皇,打钱!