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

Flutter添加shared_preferences包时出现lintClassPath配置解析失败错误

解决方案

问题根因

该报错是低版本项目配置和新版shared_preferences依赖的构建要求不匹配,加上JCenter镜像停服导致部分依赖包无法拉取导致。

操作步骤

  • 第一步:修改android/build.gradle配置
    升级Kotlin版本、Gradle插件版本,替换JCenter为MavenCentral,修改后内容如下:
    buildscript {
        ext.kotlin_version = '1.6.21'
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:7.0.4'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
    rootProject.buildDir = '../build'
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
        project.evaluationDependsOn(':app')
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
  • 第二步:修改android/gradle/wrapper/gradle-wrapper.properties配置
    将distributionUrl改为和Gradle插件匹配的版本:
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
    
  • 第三步:指定shared_preferences兼容版本
    由于你使用的Flutter 2.5.1版本较旧,不要使用最新版的shared_preferences,在pubspec.yaml中指定适配版本:
    dependencies:
      shared_preferences: 2.0.15
    
  • 第四步:重新构建项目
    依次执行以下命令:
    flutter clean
    flutter pub get
    
    之后再正常启动项目即可。

内容的提问来源于stack exchange,提问作者Muhammad Kadi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 12:18:03