Gradle配置报错:无法解析:app:debugCompileClasspath的所有文件
我已多次修改Gradle文件,但至今仍未解决问题。点击在模拟器上运行应用时,出现如下错误:
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:debugCompileClasspath'.
相关Gradle文件内容
build.gradle(:app)
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-android-extensions' id 'kotlin-kapt' } android { namespace 'com.example.mechanicsapp' compileSdk 33 buildToolsVersion "30.0.3" buildFeatures { dataBinding true viewBinding true compose true } defaultConfig { applicationId "com.example.mechanicsapp" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.0' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3' implementation 'androidx.navigation:navigation-ui-ktx:2.5.3' implementation "androidx.compose.ui:ui:1.3.3" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.activity:activity-compose:1.6.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation "androidx.core:core-ktx:1.9.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21" //dependencies //material design implementation 'com.google.android.material:material:1.7.0' //circle image view implementation 'de.hdodenhof:circleimageview:3.1.0' //scalable unit text size implementation 'com.intuit.ssp:ssp-android:1.0.6' //scalable unit size implementation 'com.intuit.sdp:sdp-android:1.0.6' //room database implementation 'androidx.room:room-runtime:2.5.0' kapt 'androidx.room:room-compiler:2.5.0' implementation 'androidx.room:room-ktx:2.5.0' implementation 'com.makeramen:roundedimageview:2.3.0' //crop image library implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' //easy permission implementation 'pub.devrel:easypermissions:3.0.0' //coroutines core implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' }
build.gradle(Project: Mechanics App)
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.0' apply false id 'com.android.library' version '7.4.0' apply false id 'org.jetbrains.kotlin.android' version '1.7.21' apply false } repositories { google() mavenCentral() maven { url 'https://maven.google.com' } }
settings.gradle(Project: MechanicsApp)
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } rootProject.name = "Mechanics App" include ':app'
这个错误一般是依赖解析失败或配置冲突导致的,按以下步骤排查修复:
删除重复依赖
你的build.gradle(:app)里存在重复依赖:androidx.core:core-ktx:1.9.0出现两次com.google.android.material:material:1.7.0出现两次
删掉重复项,避免依赖版本冲突。
匹配Build Tools与Compile SDK版本
当前buildToolsVersion "30.0.3"和compileSdk 33版本不匹配,建议直接移除buildToolsVersion配置——Android Gradle Plugin 7.0+会自动匹配对应版本;如果要手动指定,可改为33.0.2(对应compileSdk 33的稳定版本)。简化仓库配置
项目根目录build.gradle里的maven { url 'https://maven.google.com' }是冗余的,google()仓库已经包含这个地址,删掉它减少仓库解析负担。执行Gradle清理与同步
- 点击Android Studio右上角的
Sync Project with Gradle Files按钮同步项目 - 执行
File > Invalidate Caches... > Invalidate and Restart清理缓存并重启IDE - 运行终端命令
./gradlew clean build清理构建产物并重新构建
- 点击Android Studio右上角的
检查网络连接
依赖下载失败也可能是网络问题,确保能正常访问Google和Maven Central仓库,必要时可配置国内镜像仓库加速下载。
修改后的关键文件示例
调整后的build.gradle(:app)(依赖部分)
dependencies { implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.0' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3' implementation 'androidx.navigation:navigation-ui-ktx:2.5.3' implementation "androidx.compose.ui:ui:1.3.3" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.activity:activity-compose:1.6.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21" //circle image view implementation 'de.hdodenhof:circleimageview:3.1.0' //scalable unit text size implementation 'com.intuit.ssp:ssp-android:1.0.6' //scalable unit size implementation 'com.intuit.sdp:sdp-android:1.0.6' //room database implementation 'androidx.room:room-runtime:2.5.0' kapt 'androidx.room:room-compiler:2.5.0' implementation 'androidx.room:room-ktx:2.5.0' implementation 'com.makeramen:roundedimageview:2.3.0' //crop image library implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' //easy permission implementation 'pub.devrel:easypermissions:3.0.0' //coroutines core implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' }
调整后的build.gradle(Project: Mechanics App)
plugins { id 'com.android.application' version '7.4.0' apply false id 'com.android.library' version '7.4.0' apply false id 'org.jetbrains.kotlin.android' version '1.7.21' apply false } repositories { google() mavenCentral() }
内容的提问来源于stack exchange,提问作者DCam

