React Native安卓项目构建失败:react-native-socketio编译报错求助
React Native Android构建失败:react-native-socketio要求compileSdkVersion 30+
我开发的安卓项目基于React Native,用到了socket.io、gradle和react-native-sms-x,执行npm run android时构建失败,报错信息如下:
C:\Users\user\Desktop\SmsApp> npm run android > SmsApp@0.0.1 android > react-native run-android info JS server already running. info 💡 Tip: Make sure that you have set up your development environment correctly, by running react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':tasks'. > Could not create task ':react-native-socketio:compileDebugJavaWithJavac'. > In order to compile Java 9+ source, please set compileSdkVersion to 30 or above * 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 10s error Failed to install the app.
解决方案
方法1:升级项目全局compileSdkVersion
- 打开项目根目录下的
android/build.gradle文件 - 找到
ext代码块中的compileSdkVersion,将其修改为30或更高版本(推荐使用33,适配最新安卓版本):buildscript { ext { compileSdkVersion = 33 minSdkVersion = 21 targetSdkVersion = 33 // 其他配置项... } // 其余代码... } - 保存文件后,执行
npm run android重新构建
方法2:单独指定react-native-socketio的compileSdkVersion
若不想升级项目全局版本,可单独为该依赖设置兼容版本:
- 打开
android/build.gradle,在allprojects代码块上方添加:subprojects { afterEvaluate { project -> if (project.name == 'react-native-socketio') { android { compileSdkVersion 30 targetSdkVersion 30 } } } } - 保存后重新执行构建命令
方法3:更新react-native-socketio至最新版
旧版本依赖可能存在兼容性问题,执行以下命令更新:
npm install react-native-socketio@latest --save
更新完成后再次尝试构建
内容的提问来源于stack exchange,提问作者Jarvin78
相关产品推荐
相关产品推荐

