安装react-native-maps后Gradle 8.0构建失败求助
问题描述
使用Gradle 8.0,React Native 0.70.4项目中安装react-native-maps后无法构建,已尝试清理缓存、更换设备编译等操作,仍持续报错。
报错信息
Task :react-native-maps:generateDebugRFile FAILED Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings 37 actionable tasks: 3 executed, 34 up-to-date FAILURE: Build completed with 2 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':react-native-maps:generateDebugRFile'. > Could not resolve all files for configuration ':react-native-maps:debugCompileClasspath'. > Failed to transform react-native-0.71.0-rc.0-debug.aar (com.facebook.react:react-native:0.71.0-rc.0) to match attributes {artifactType=android-symbol-with-package-name, com.android.build.api.attributes.BuildTypeAttr=debug, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-api}. > Could not find react-native-0.71.0-rc.0-debug.aar (com.facebook.react:react-native:0.71.0-rc.0). Searched in the following locations: https://repo.maven.apache.org/maven2/com/facebook/react/react-native/0.71.0-rc.0/react-native-0.71.0-rc.0-debug.aar * 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. ============================================================================== 2: Task failed with an exception. ----------- * What went wrong: java.lang.StackOverflowError (no error message) * 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 54s 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 FAILURE: Build completed with 2 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':react-native-maps:generateDebugRFile'. > Could not resolve all files for configuration ':react-native-maps:debugCompileClasspath'. > Failed to transform react-native-0.71.0-rc.0-debug.aar (com.facebook.react:react-native:0.71.0-rc.0) to match attributes {artifactType=android-symbol-with-package-name, com.android.build.api.attributes.BuildTypeAttr=debug, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-api}. > Could not find react-native-0.71.0-rc.0-debug.aar (com.facebook.react:react-native:0.71.0-rc.0). Searched in the following locations: https://repo.maven.apache.org/maven2/com/facebook/react/react-native/0.71.0-rc.0/react-native-0.71.0-rc.0-debug.aar * 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. 2: Task failed with an exception. ----------- * What went wrong: java.lang.StackOverflowError (no error message) > 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 54s
设备环境信息
info Fetching system and libraries information... System: OS: Windows 10 10.0.19044 CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz Memory: 1.05 GB / 7.89 GB Binaries: Node: 16.17.1 - C:\Program Files\nodejs\node.EXE Yarn: Not Found npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: Not Found Windows SDK: Not Found IDEs: Android Studio: Not Found Visual Studio: 17.3.32819.101 (Visual Studio Community 2022) Languages: Java: 18.0.2.1 npmPackages: @react-native-community/cli: Not Found react: 18.1.0 => 18.1.0 react-native: 0.70.4 => 0.70.4 react-native-windows: Not Found npmGlobalPackages: *react-native*: Not Found
解决方案
1. 强制react-native-maps使用当前项目的RN版本
react-native-maps默认依赖的RN版本高于项目使用的0.70.4,导致拉取不存在的0.71.0-rc.0包。在android/build.gradle中添加依赖强制策略:
allprojects { repositories { // 保留原有仓库配置 } // 新增以下配置 configurations.all { resolutionStrategy.force 'com.facebook.react:react-native:0.70.4' } }
2. 降级Gradle至兼容RN 0.70.4的版本
RN 0.70.x不兼容Gradle 8.0,需要降级:
- 修改
android/gradle/wrapper/gradle-wrapper.properties中的distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
- 修改
android/build.gradle中的Android Gradle插件版本:
buildscript { dependencies { classpath("com.android.tools.build:gradle:7.4.2") // 保留其他classpath配置 } }
3. 调整JVM内存解决StackOverflowError
在android/gradle.properties中增加JVM内存配置:
org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m
4. 清理缓存并重新构建
执行以下命令:
cd android && gradlew clean cd .. && npm install npx react-native run-android
内容的提问来源于stack exchange,提问作者StackedPotato90
相关产品推荐
相关产品推荐

