添加material-icons-extended依赖后出现TypedResolveException错误的解决办法
添加material-icons-extended依赖后,Gradle抛出如下解析错误,移除该依赖后应用可正常运行:
Caused by: org.gradle.api.internal.artifacts.ivyservice.TypedResolveException: Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
项目使用的build.gradle.kts配置如下:
plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) } android { namespace = "com.example.peaceofmind" compileSdk = 35 defaultConfig { applicationId = "com.example.peaceofmind" minSdk = 24 targetSdk = 35 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "1.8" } buildFeatures { compose = true } composeOptions { kotlinCompilerExtensionVersion = "1.5.15" } } dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.activity.compose) implementation(platform(libs.androidx.compose.bom)) implementation(libs.androidx.ui) implementation(libs.androidx.ui.graphics) implementation(libs.androidx.ui.tooling.preview) implementation(libs.androidx.material3) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(platform(libs.androidx.compose.bom)) androidTestImplementation(libs.androidx.ui.test.junit4) debugImplementation(libs.androidx.ui.tooling) debugImplementation(libs.androidx.ui.test.manifest) implementation ("androidx.compose.material:material-icons-extended:1.8.2") }
问题根源是依赖版本不兼容:手动指定的material-icons-extended:1.8.2与项目中Compose BOM、Kotlin Compiler Extension版本不匹配。按以下步骤修复:
通过BOM统一管理图标库版本
项目已引入Compose BOM,直接用libs变量引用图标库,让BOM自动同步版本,避免冲突:// 替换原手动版本的依赖行 implementation(libs.androidx.material.icons.extended)若
libs.versions.toml中无对应条目,添加以下内容:[libraries] androidx-material-icons-extended = { group = "androidx.compose.material", name = "material-icons-extended" }无需手动指定版本,BOM会自动匹配当前Compose生态的版本。
对齐Kotlin Compiler Extension版本
当前kotlinCompilerExtensionVersion = "1.5.15"对应Compose 1.5.x系列,而material-icons-extended:1.8.2属于Compose 1.8.x,版本跨度太大导致冲突。可二选一:- 升级
kotlinCompilerExtensionVersion到与Compose 1.8.x匹配的版本(1.5.15可兼容Compose 1.8.x,若不确定,直接采用BOM对应的Compose版本); - 降低
material-icons-extended版本到与当前Compose版本一致(比如1.5.x)。
- 升级
清理Gradle缓存
缓存异常可能加剧版本冲突,执行以下命令清理并重建:./gradlew clean build --refresh-dependencies检查网络与仓库配置
若依赖下载失败也会触发该错误,确保网络可访问Maven仓库;国内环境可添加阿里云镜像到settings.gradle.kts:dependencyResolutionManagement { repositories { maven("https://maven.aliyun.com/repository/google") maven("https://maven.aliyun.com/repository/jcenter") mavenCentral() } }
内容的提问来源于stack exchange,提问作者mfusco

