You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Android构建失败:无法解析datastore-preferences:1.0.0求助

问题解答

是否与shared_preferences库有关?

无关。shared_preferences是Flutter官方基于Android SharedPreferences实现的存储插件,和Jetpack DataStore是完全独立的两个存储方案,二者不会互相干扰导致该依赖解析失败问题。

为何会出现此问题?

主要有以下几种可能:

  • 仓库配置或网络问题:项目未配置Google Maven仓库,或者网络无法访问该仓库,导致Gradle无法下载指定版本的DataStore AAR包;
  • Gradle缓存损坏:本地Gradle缓存中的损坏文件导致无法正常读取或拉取依赖;
  • 版本兼容性:DataStore 1.0.0虽为稳定版,但可能与当前Gradle版本、Android SDK版本存在兼容性冲突,间接引发依赖解析失败。

如何解决?

尝试以下步骤逐一排查:

  1. 检查Google Maven仓库配置
    确保项目根目录的build.gradle文件中已添加Google仓库:

    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
  2. 升级DataStore版本
    替换为最新稳定版DataStore依赖(当前最新稳定版为1.1.1),修改app/build.gradle中的依赖:

    dependencies {
        implementation "androidx.datastore:datastore-preferences:1.1.1"
        implementation "androidx.datastore:datastore:1.1.1"
    }
    
  3. 清理Gradle缓存并重新构建

    • 执行命令刷新依赖并清理缓存:
      # Linux/macOS
      ./gradlew clean build --refresh-dependencies
      
      # Windows
      gradlew clean build --refresh-dependencies
      
    • 若命令无效,可手动删除本地Gradle缓存目录:
      • Linux/macOS:~/.gradle/caches
      • Windows:C:\Users\<你的用户名>\.gradle\caches
  4. 适配网络环境
    若网络无法访问Google仓库,可添加国内Maven镜像(如阿里云镜像)到仓库配置:

    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        google()
        mavenCentral()
    }
    

是Maven仓库还是库版本存在问题?

两者都可能是诱因:

  • 仓库问题:若项目未配置Google仓库,或网络无法访问该仓库,必然会导致依赖找不到;
  • 版本问题:旧版本(如1.0.0)可能存在仓库索引更新不及时、与当前Gradle/SDK版本兼容性差等问题,升级到新版本通常能规避这类问题。

内容的提问来源于stack exchange,提问作者mhmd Abo hadii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 18:35:03