编译时出现 react-native-camera 错误 android - 找不到符号 class NonNull
react-native-camera error when compiling android - cannot find symbol class NonNull
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 个错误)
错误:找不到符号 classNonNull
错误:找不到符号 class NonNull
错误:找不到符号 class NonNull
此问题是由 google 迁移到 androidx
引起的
第一个 quick-fix 是手动删除 /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 友好)
如果每个包使用了这些注释,您将需要对它们执行此操作。
另请记住,某些软件包可能正在使用 java classes 位于:android.support.v4
(例如 ActivityCompat、NotificationCompat,...)那些 [=57=由于 androidx 迁移
,]es 现在位于此处 androidx.core.app.*
(androidx.core.app.ActivityCompat,...)
一些参考资料:
- https://github.com/itinance/react-native-fs/issues/686
- https://github.com/mikehardy/jetifier#to-jetify--convert-node_modules-dependencies-to-androidx
这是一个 class 旧/新 android 导入之间的映射 csv 文件:
https://github.com/mikehardy/jetifier/blob/master/src/androidx-class-mapping.csv
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 个错误)
错误:找不到符号 classNonNull 错误:找不到符号 class NonNull 错误:找不到符号 class NonNull
此问题是由 google 迁移到 androidx
引起的第一个 quick-fix 是手动删除 /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 友好)
如果每个包使用了这些注释,您将需要对它们执行此操作。
另请记住,某些软件包可能正在使用 java classes 位于:android.support.v4
(例如 ActivityCompat、NotificationCompat,...)那些 [=57=由于 androidx 迁移
androidx.core.app.*
(androidx.core.app.ActivityCompat,...)
一些参考资料:
- https://github.com/itinance/react-native-fs/issues/686
- https://github.com/mikehardy/jetifier#to-jetify--convert-node_modules-dependencies-to-androidx
这是一个 class 旧/新 android 导入之间的映射 csv 文件:
https://github.com/mikehardy/jetifier/blob/master/src/androidx-class-mapping.csv