React Native项目构建失败:@react-native-community_netinfo编译错误求助
解决React Native 0.59.10构建失败:@react-native-community/netinfo编译错误
问题背景
React Native项目(版本^0.59.10)此前运行正常,近期未修改代码,现在构建Android应用时失败,无法安装到模拟器,核心报错指向@react-native-community/netinfo模块的Java编译任务失败。
报错信息
Task :@react-native-community_netinfo:compileDebugJavaWithJavac FAILED FAILURE: Build failed with an exception. What went wrong: Execution failed for task ':@react-native-community_netinfo: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 3s 10 actionable tasks: 1 executed, 9 up-to-date error Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment: https://facebook.github.io/react-native/docs/getting-started.html error Command failed: ./gradlew app:installDebug. Run CLI with --verbose flag for more details. npm ERR! code ELIFECYCLE npm ERR! errno 1
项目依赖(package.json)
"dependencies": { "@babel/plugin-transform-react-constant-elements": "^7.6.3", "@react-native-community/netinfo": "^4.7.0", "@types/node": "^12.11.1", "aadhaar-validator": "^1.1.0", "babel-plugin-transform-react-pure-class-to-function": "^1.0.1", "babel-plugin-transform-react-remove-prop-types": "^0.4.24", "compare-versions": "^3.6.0", "frisbee": "^1.1.7", "libphonenumber-js": "^1.7.52", "mobx": "^4.9.4", "mobx-react": "^6.1.4", "react": "^16.8.1", "react-native": "^0.59.10", "react-native-appstore-version-checker": "^2.7.2", "react-native-calendar": "^0.13.1", "react-native-check-box": "^2.1.7", "react-native-checkbox": "^2.0.0", "react-native-collapsible": "^1.5.1", "react-native-config": "^0.11.7", "react-native-datepicker": "^1.7.2", "react-native-deprecated-custom-components": "^0.1.2", "react-native-drawer": "^2.5.1", "react-native-element": "^1.2.1", "react-native-fetch-blob": "^0.10.8", "react-native-firebase": "^5.6.0", "react-native-image-picker": "^1.1.0", "react-native-keyboard-aware-scrollview": "^2.0.0", "react-native-loading-spinner-overlay": "^1.0.1", "react-native-material-kit": "0.5.1", "react-native-phone-input": "^0.1.10", "react-native-push-notification": "^7.2.3", "react-native-side-menu": "^1.1.3", "react-native-simple-toast": "^0.1.1", "react-native-splash-screen": "^3.2.0", "react-native-style-tachyons": "^4.0.0", "react-native-swiper": "^1.5.14", "react-native-table-component": "^1.2.1", "react-native-version-number": "^0.3.6", "react-native-webview": "7.5.2", "react-test-renderer": "^16.10.2" }, "devDependencies": { "@babel/core": "^7.6.4", "@babel/plugin-transform-react-inline-elements": "^7.2.0", "@babel/preset-env": "^7.6.3", "@babel/preset-react": "^7.6.3", "@types/jest": "^24.0.18", "@types/react": "^16.9.5", "autobind-decorator": "^1.3.4", "axios": "^0.15.3", "babel-eslint": "^7.1.1", "babel-jest": "18.0.0", "babel-loader": "^7.1.5", "babel-plugin-transform-decorators-legacy": "^1.3.4", "babel-preset-react-native": "1.9.1", "babel-preset-react-native-stage-0": "^1.0.1", "bluebird": "^3.5.0", "concurrently": "^3.1.0", "eslint": "^3.14.0", "eslint-config-airbnb": "^14.0.0", "eslint-plugin-babel": "^4.0.1", "eslint-plugin-flowtype": "^2.30.0", "eslint-plugin-import": "^2.2.0", "eslint-plugin-jsx-a11y": "^3.0.2", "eslint-plugin-react": "^6.9.0", "flow-bin": "0.33.0", "jest": "18.1.0", "jetifier": "^1.6.4", "json2csv": "^3.7.3", "markdown-toc": "^1.1.0", "metro-react-native-babel-preset": "^0.56.0", "nodemon": "^1.11.0", "rimraf": "^2.5.4", "tslint": "^4.3.1", "typescript": "^3.6.4" }
解决提示
- 先获取详细编译日志:运行
./gradlew app:installDebug --stacktrace,查看具体的编译错误原因(比如语法不兼容、依赖冲突、JDK版本问题等),这是定位问题的关键。 - 锁定netinfo版本:当前依赖用了
^4.7.0,可能自动拉取了不兼容的小版本更新,将其改为固定版本"@react-native-community/netinfo": "4.7.0",然后删除node_modules、package-lock.json(或yarn.lock),重新执行npm install(或yarn install),再尝试构建。 - 检查JDK版本:React Native 0.59.x要求使用JDK 8,用
java -version确认当前版本,若为JDK 11+,切换到JDK 8再重新构建。 - 清理Gradle缓存:进入
android目录,执行./gradlew clean,回到根目录后重新运行react-native run-android。 - 重新运行Jetifier:执行
npx jetifier,确保所有依赖被正确转换适配AndroidX(React Native 0.59.x开始部分依赖需要AndroidX支持)。 - 验证Android SDK配置:确认SDK Manager中安装了Android SDK Build Tools 28.0.3,同时检查项目
android/build.gradle中对应的buildToolsVersion配置是否一致。
内容的提问来源于stack exchange,提问作者Shashank Sai
相关产品推荐
相关产品推荐

