Android Studio项目因Datastore依赖缺失构建失败求助
解决Android Studio无法找到datastore-preferences-android:1.1.0的构建问题
问题根源
- 依赖冗余与版本冲突:你的Gradle依赖中重复添加了
datastore-preferences-android的不同版本(1.1.0-alpha04和1.1.0),同时混合了datastore-preferences:1.0.0,导致依赖解析混乱。 - 错误的Artifact ID:
androidx.datastore:datastore-preferences-android并非官方标准依赖标识,Datastore Preferences的标准依赖是androidx.datastore:datastore-preferences,无需额外添加-android后缀。
修复步骤
1. 清理并修正依赖配置
修改app/build.gradle中的dependencies块,删除错误/冗余依赖,替换为标准的稳定版依赖:
dependencies { // 保留正确的Datastore Preferences稳定版依赖 implementation "androidx.datastore:datastore-preferences:1.1.1" // 保留其他正常依赖,统一Lifecycle版本避免冲突 implementation 'androidx.navigation:navigation-compose:2.5.3' implementation "androidx.compose.ui:ui-tooling:1.3.3" implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.3.1' implementation "androidx.compose.ui:ui:$compose_ui_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version" implementation 'androidx.compose.material:material:1.1.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4' implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1" }
2. 同步并清理构建缓存
- 点击Android Studio右上角的Sync Project with Gradle Files按钮同步依赖。
- 执行
File > Invalidate Caches... > Invalidate and Restart,清理缓存后重启IDE。 - 若仍有问题,依次执行
Build > Clean Project和Build > Rebuild Project。
额外检查项
确保项目根目录的settings.gradle中已配置必要的仓库:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
内容的提问来源于stack exchange,提问作者yves
相关产品推荐
相关产品推荐

