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

Flutter项目Android Studio中Gradle仓库配置错误求助

解决Flutter项目在Android Studio中的Gradle仓库冲突错误

问题原因

这个错误源于Gradle 7.x及以上版本默认启用了优先使用settings级仓库配置的规则,但你的项目同时在settings.gradle和项目级build.gradle中重复配置了仓库,且配置方式不符合Gradle新规范。VS Code能正常运行是因为它的Gradle执行逻辑未严格触发该校验,而Android Studio对Gradle规范的校验更严格。

解决方案

1. 修改项目级build.gradle(Project:android)

移除allprojects块中的仓库配置,Gradle 7+推荐在settings.gradle中统一管理所有依赖仓库:

// buildscript {
//     ext.kotlin_version = '1.8.0'
//     repositories {
//         google()
//         jcenter()
//                 mavenCentral() // Add this repository

//     }

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

//         // Add the google services classpath
//         classpath 'com.google.gms:google-services:4.3.10'

// //
// //        // Add the Flutter plugin classpath
// //        classpath 'dev.flutter:flutter-gradle-plugin:1.0.0' // Add this line
//     }
// }

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

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

2. 更新settings.gradle

添加dependencyResolutionManagement块分离插件仓库与依赖仓库的配置,符合Gradle规范:

pluginManagement {
def flutterSdkPath = {
    def properties = new Properties()
    file("local.properties").withInputStream { properties.load(it) }
    def flutterSdkPath = properties.getProperty("flutter.sdk")
    assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
    return flutterSdkPath
}()

includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

// 仅用于加载Gradle插件的仓库
repositories {
    google()
    mavenCentral()
    gradlePluginPortal()
}
}

// 管理项目所有依赖的仓库
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // 注意:jcenter已停止维护,建议后续替换为mavenCentral
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.2.0" apply false
    id "org.jetbrains.kotlin.android" version "1.7.20" apply false
}

include ":app"

3. 同步验证

在Android Studio中点击File > Sync Project with Gradle Files,等待同步完成后重新运行项目,错误即可消除。

额外优化建议

  • 移除jcenter()仓库:jcenter已停止维护,建议检查并迁移所有依赖至mavenCentral()或其他活跃仓库
  • 统一版本兼容性:确保com.android.application插件版本(7.2.0)与本地Gradle版本匹配(可在gradle/wrapper/gradle-wrapper.properties查看,7.2.0插件对应Gradle 7.3+版本)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 14:22:03