编译android时react-native-camera错误-找不到符号类NonNull

时间:2019-06-23 15:59:25

标签: react-native react-native-camera non-nullable

package.js

...
"react": "16.8.3",
"react-native": "0.59.5",
"react-native-camera": "git+https://git@github.com/react-native-community/react-native-camera.git"
...

settings.gradle

include ':app'

include ':react-native-camera'
project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android')

settings.gradle

...
android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "xxxxxx"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    ...
}

dependencies {
    implementation project(':react-native-camera')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.annotation:annotation:1.0.2'
    implementation "com.facebook.react:react-native:+"  // From node_modules
}
...

gradle.properdies

android.useAndroidX=true
android.enableJetifier=true

错误

../../../../../../../../ node_modules / react-native-camera / android / src / main / java / com / google / android / cameraview /Camera2.java(3个错误)

错误:找不到符号类NonNull 错误:找不到符号类NonNull 错误:找不到符号类NonNull

1 个答案:

答案 0 :(得分:0)

此问题是由Google迁移到androidx

引起的

第一个快速解决方案是手动删除/ node-modules /

中的导入/注释

您需要:

  • 首先找到Camera2.java文件(引发错误的文件)
  • 然后找到import android.support.annotation.NonNull;行并将其删除
  • 还删除文件中正在使用的每个@NonNull注释

Mike Hardy创建了一个程序包,可让您自动执行此操作:https://github.com/mikehardy/jetifier#to-jetify--convert-node_modules-dependencies-to-androidx(它是react-native / npm友好的)

如果每个程序包都使用这些批注,则需要这样做。

还要记住,某些软件包可能正在使用位于android.support.v4的java类(例如ActivityCompat,NotificationCompat等),这些类现在位于androidx.core.app.*(androidx.core)中。 app.ActivityCompat,...)由于androidx迁移

一些参考文献:

这是旧的/新的android导入之间的csv文件映射类:

https://github.com/mikehardy/jetifier/blob/master/src/androidx-class-mapping.csv