React Native 0.64.1构建报错:Could not resolve all files for configuration ':app:debugRuntimeClasspath'
问题背景
在React Native 0.64.1环境下,使用Android Studio Dolphin 2021.3.1构建项目时触发以下错误:
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
项目根目录build.gradle配置如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { googlePlayServicesVersion = "+" // default: "+" firebaseMessagingVersion = "21.1.0" // default: "21.1.0" buildToolsVersion = "29.0.3" minSdkVersion = 21 compileSdkVersion = 29 targetSdkVersion = 31 ndkVersion = "20.1.5948944" } repositories { google() jcenter() } dependencies { classpath('com.android.tools.build:gradle:4.1.0') classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenLocal() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url("$rootDir/../node_modules/react-native/android") } maven { // Android JSC is installed from npm url("$rootDir/../node_modules/jsc-android/dist") } google() maven { url 'https://maven.google.com' } maven { url 'https://www.jitpack.io' } mavenCentral() jcenter() } }
当前使用Android Gradle插件版本4.1.0,Gradle版本6.9-rc-1,已尝试清理项目重建、重新安装npm包、关闭离线模式、修改SDK/NDK版本,均未解决问题。
解决方案
1. 优化仓库配置
allprojects中同时存在google()和maven { url 'https://maven.google.com' },二者为同一仓库,保留google()即可。同时调整仓库顺序,优先从官方仓库拉取依赖:
allprojects { repositories { mavenLocal() google() mavenCentral() maven { url("$rootDir/../node_modules/react-native/android") } maven { url("$rootDir/../node_modules/jsc-android/dist") } maven { url 'https://www.jitpack.io' } jcenter() // jcenter已停止维护,后续建议逐步移除 } }
2. 固定Google Play Services版本
googlePlayServicesVersion = "+"会自动拉取最新版本,易引发依赖冲突,改为与firebase版本匹配的稳定版本:
buildscript { ext { googlePlayServicesVersion = "17.0.0" // 替换为适配的稳定版本 // 其他配置不变 } }
3. 升级Gradle与AGP版本
Android Studio Dolphin推荐AGP 7.2.x + Gradle 7.4+,当前版本过低存在兼容性问题:
- 修改
buildscript中的AGP版本:
classpath('com.android.tools.build:gradle:7.2.2')
- 修改
gradle/wrapper/gradle-wrapper.properties中的Gradle版本:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
4. 排查依赖冲突
执行命令查看依赖树,定位冲突模块:
./gradlew app:dependencies --configuration debugRuntimeClasspath
在app/build.gradle中排除冲突依赖,示例:
dependencies { implementation('com.google.firebase:firebase-messaging:21.1.0') { exclude group: 'com.google.android.gms', module: 'play-services-base' } }
5. 清理Gradle缓存
删除本地缓存避免损坏依赖影响构建:
rm -rf ~/.gradle/caches ./gradlew clean build
内容的提问来源于stack exchange,提问作者jc28

