react-native-image-picker编译失败:Task :react-native-image-picker:compileDebugJavaWithJavac FAILED
React Native Android编译失败:react-native-image-picker模块报错
负责React Native项目UI更新时,执行npx react-native run android命令运行应用,出现Task :react-native-image-picker:compileDebugJavaWithJavac FAILED错误,完整错误信息如下:
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings 302 actionable tasks: 2 executed, 300 up-to-date Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/03 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/03 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/03 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: unexpected element (uri:"", local:"base-extension"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level> C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:135: error: cannot find symbol if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { ^ symbol: variable TIRAMISU location: class VERSION_CODES C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:143: error: cannot find symbol libraryIntent = new Intent(MediaStore.ACTION_PICK_IMAGES); ^ symbol: variable ACTION_PICK_IMAGES location: class MediaStore C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:147: error: cannot find symbol if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { ^ symbol: variable TIRAMISU location: class VERSION_CODES C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:152: error: cannot find symbol if (selectionLimit == 0) maxNum = MediaStore.getPickImagesMaxLimit(); ^ symbol: method getPickImagesMaxLimit() location: class MediaStore C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:153: error: cannot find symbol libraryIntent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxNum); ^ symbol: variable EXTRA_PICK_IMAGES_MAX location: class MediaStore C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:162: error: cannot find symbol } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { ^ symbol: variable TIRAMISU location: class VERSION_CODES 6 errors FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native-image-picker:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 1m 58s error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/03 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/03 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/03 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: unexpected element (uri:"", local:"base-extension"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level> C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:135: error: cannot find symbol if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { ^ symbol: variable TIRAMISU location: class VERSION_CODES C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:143: error: cannot find symbol libraryIntent = new Intent(MediaStore.ACTION_PICK_IMAGES); ^ symbol: variable ACTION_PICK_IMAGES location: class MediaStore C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:147: error: cannot find symbol if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { ^ symbol: variable TIRAMISU location: class VERSION_CODES C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:152: error: cannot find symbol if (selectionLimit == 0) maxNum = MediaStore.getPickImagesMaxLimit(); ^ symbol: method getPickImagesMaxLimit() location: class MediaStore C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:153: error: cannot find symbol libraryIntent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxNum); ^ symbol: variable EXTRA_PICK_IMAGES_MAX location: class MediaStore C:\Users\CURTIS\Downloads\efik\node_modules\react-native-image-picker\android\src\main\java\com\imagepicker\ImagePickerModule.java:162: error: cannot find symbol } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { ^ symbol: variable TIRAMISU location: class VERSION_CODES 6 errors FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native-image-picker:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 1m 58s at makeError (C:\Users\CURTIS\Downloads\efik\node_modules\execa\index.js:174:9) at C:\Users\CURTIS\Downloads\efik\node_modules\execa\index.js:278:16 at processTicksAndRejections (node:internal/process/task_queues:96:5) at async runOnAllDevices (C:\Users\CURTIS\Downloads\efik\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:109:5) at async Command.handleAction (C:\Users\CURTIS\Downloads\efik\node_modules\@react-native-community\cli\build\index.js:192:9)
错误原因
TIRAMISU是Android 13(API 33)的版本代号,ACTION_PICK_IMAGES、getPickImagesMaxLimit()等API均为Android 13新增接口。当前项目的Android编译SDK版本低于33,而react-native-image-picker的新版本依赖这些API,导致编译时找不到符号。
解决方案
方案一:升级Android项目SDK版本
- 打开项目根目录下的
android/build.gradle文件,修改buildscript中的版本配置:buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 // 可根据项目需求调整,需满足依赖库最低要求 compileSdkVersion = 33 targetSdkVersion = 33 } } - 同步Gradle配置后,重新执行
npx react-native run android
方案二:降级react-native-image-picker到兼容旧SDK的版本
- 卸载当前版本,安装不依赖Android 13 API的低版本:
npm uninstall react-native-image-picker npm install react-native-image-picker@^4.8.0 - RN 0.60及以上版本自动完成链接,无需额外操作;旧版本需执行
react-native link react-native-image-picker,之后重新编译
方案三:强制指定react-native-image-picker的编译SDK版本
- 在
android/build.gradle中添加如下配置,强制该模块使用API 33编译:subprojects { afterEvaluate { project -> if (project.name == 'react-native-image-picker') { android { compileSdkVersion 33 buildToolsVersion "33.0.0" } } } } - 同步Gradle后重新运行编译命令
内容的提问来源于stack exchange,提问作者Curtis Crentsil
相关产品推荐
相关产品推荐

