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

Flutter简易天气项目geolocator依赖解析错误求助

解决方案

1. 对齐Kotlin、AGP与Gradle版本兼容性

核心问题是Android Gradle Plugin(AGP)版本过旧,无法兼容依赖要求的Kotlin 1.7.x版本:当前AGP 4.1.0仅支持Kotlin 1.5.x及以下版本,但geolocator_android等依赖可能要求更高的Kotlin版本,导致构建时自动尝试拉取Kotlin 1.7.1,最终因版本不兼容出现依赖解析失败。

修改android/build.gradle为以下兼容版本组合:

allprojects {
    repositories {
        google() // 必须放在mavenCentral之前
        mavenCentral()
    }
}

buildscript {
    ext.kotlin_version = '1.7.20' // 与AGP 7.0.x兼容的Kotlin版本
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4' // 升级AGP到兼容版本
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    // 强制所有子模块使用统一Kotlin版本,避免依赖冲突
    project.ext {
        kotlin_version = rootProject.ext.kotlin_version
    }
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

同时修改android/gradle/wrapper/gradle-wrapper.properties中的Gradle版本,匹配AGP 7.0.x的要求:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip

2. 彻底清理缓存并重新构建

执行以下命令完全清理旧缓存与构建产物:

# 清理Flutter项目构建产物
flutter clean
# 清理全局Gradle缓存
rm -rf ~/.gradle/caches/
# 重新下载依赖并构建
flutter pub get
flutter build apk

3. 验证依赖版本一致性

如果仍有问题,检查android/app/build.gradle中的编译版本,建议设置为33或更高以匹配依赖要求:

android {
    compileSdkVersion 33

    defaultConfig {
        targetSdkVersion 33
        // ...其他配置
    }
}

内容的提问来源于stack exchange,提问作者Prakhar Singhal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 03:19:54