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

Gradle构建错误:Settings与Project仓库配置冲突求助

解决Gradle仓库配置冲突问题

问题根源

你的settings.gradle.kts中设置了repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS),这个配置的作用是严格禁止所有项目(包括根项目)的构建文件中添加任何仓库声明,所有依赖仓库必须统一在settings.gradle.kts的dependencyResolutionManagement块中配置。

你提到移除了MavenRepo但问题依旧,是因为根目录的build.gradle.kts中仍然存在repositories { mavenCentral() }这个仓库声明,同时根项目还配置了不必要的依赖和插件,这些都是导致冲突的核心原因。

解决方案

1. 修正根目录build.gradle.kts

将根项目的构建文件简化为仅声明需要的插件(且apply false,让子模块按需应用),移除所有多余的仓库、依赖和无用代码:

plugins {
    id("com.android.application") version "8.2.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
  • 移除buildscript块:里面的变量未被引用,新版本Gradle无需通过buildscript管理插件版本,plugins块已足够。
  • 移除kotlin("jvm")插件:根项目是聚合模块,不是JVM应用/库,无需此插件。
  • 移除dependencies块:根项目不需要直接声明依赖,子模块的依赖由自身配置。
  • 移除repositories块:所有仓库已在settings.gradle.kts中统一配置,此处禁止添加。

2. 确认settings.gradle.kts配置正确性

你的settings.gradle.kts配置符合集中式仓库管理要求,保持现有内容即可:

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "Course"
include(":app")

3. 清理并重新构建

  • 在Android Studio中执行:File > Invalidate Caches...,勾选Invalidate and Restart,重启IDE。
  • 或者在项目根目录的终端执行命令:
    # Windows
    gradlew clean build
    
    # macOS/Linux
    ./gradlew clean build
    

补充说明

RepositoriesMode.FAIL_ON_PROJECT_REPOS是Gradle推荐的集中式仓库管理模式,能避免多模块仓库配置不一致的问题,所有模块都会自动使用settings.gradle.kts中定义的仓库,无需在子模块或根项目中重复声明。

内容的提问来源于stack exchange,提问作者Malek.A Zhr

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 20:36:12