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

Flutter项目构建APK时Gradle依赖下载失败求助

问题

基于Flutter 3、Gradle 7.3.0构建APK时遭遇Gradle依赖下载异常,项目依赖了shared_preferences_android的easy_localization插件。已执行flutter clean,flutter doctor检测正常,但安装其他插件时也会出现相同错误。手动访问错误日志里的.jar文件URL可正常打开,但Gradle始终无法找到这些文件。

错误日志片段

FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':shared_preferences_android'.
> Could not resolve all files for configuration ':shared_preferences_android:classpath'.
   > Could not find gradle-7.2.2.jar (com.android.tools.build:gradle:7.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.2.2/gradle-7.2.2.jar
   > Could not find builder-7.2.2.jar (com.android.tools.build:builder:7.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/build/builder/7.2.2/builder-7.2.2.jar
   > Could not find sdk-common-30.2.2.jar (com.android.tools:sdk-common:30.2.2).
     Searched in the following locations:
https://dl.google.com/dl/android/maven2/com/android/tools/sdk-common/30.2.2/sdk-common-30.2.2.jar
   > Could not find repository-30.2.2.jar (com.android.tools:repository:30.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/repository/30.2.2/repository-30.2.2.jar
   > Could not find gradle-api-7.2.2.jar (com.android.tools.build:gradle-api:7.2.2).
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle-api/7.2.2/gradle-api-7.2.2.jar
   > Could not find builder-test-api-7.2.2.jar (com.android.tools.build:builder-test-api:7.2.2).
...

项目根目录build.gradle内容

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
解决方案
  • 强制统一子项目的Gradle版本
    子项目(如shared_preferences_android)可能使用与根项目不一致的Gradle版本,导致依赖拉取冲突。在根目录build.gradle的subprojects块中添加配置,强制所有子项目使用根项目指定的Gradle插件版本:

    subprojects {
        project.plugins.whenPluginAdded { plugin ->
            if (plugin instanceof com.android.build.gradle.api.AndroidBasePlugin) {
                project.buildscript.dependencies {
                    classpath 'com.android.tools.build:gradle:7.3.1' // 与根项目版本保持一致
                }
            }
        }
    }
    
  • 清理Gradle代理配置
    即使手动能访问URL,Gradle可能因代理配置异常无法下载。检查以下文件:

    • 项目根目录的gradle.properties,删除未正确配置的http.proxyHost、http.proxyPort等代理字段(无需代理时)。
    • 系统级Gradle配置文件:Windows路径为C:\Users\<用户名>\.gradle\gradle.properties,Mac/Linux路径为~/.gradle/gradle.properties,清理错误的代理设置。
  • 手动下载依赖到本地仓库
    下载错误日志中缺失的.jar文件,放入本地Gradle仓库的对应路径。例如gradle-7.2.2.jar需放入~/.gradle/caches/modules-2/files-2.1/com.android.tools.build/gradle/7.2.2/[哈希值]/gradle-7.2.2.jar,哈希值可参考Gradle生成的路径或依赖解析日志。

  • 更新Flutter插件版本
    检查pubspec.yaml中easy_localization和shared_preferences的版本,更新到最新稳定版,新版本可能已修复Gradle版本兼容问题。

  • 彻底清理Gradle缓存
    执行以下命令清理缓存后重新构建:

    # 清理项目缓存
    flutter clean
    # 清理本地Gradle缓存(Mac/Linux)
    rm -rf ~/.gradle/caches/
    # Windows:删除C:\Users\<用户名>\.gradle\caches\文件夹
    

    之后重新执行flutter build apk。


内容的提问来源于stack exchange,提问作者Juan Pablo Palmero Valdés

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:06:58