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

Gradle多模块项目:buildSrc版本无法在pluginManagement引用的解决

多模块Gradle项目版本统一管理问题

我希望统一管理多模块项目中所有插件/依赖的版本,尝试了以下方案:

  1. 在./buildSrc/main/kotlin/Versions.kt中创建包含版本常量的对象:
object Versions {
    const val myApp = "1.0.12"

    const val kotlin = "1.8.20"
    const val agp = "7.1.3"
    const val compose = "1.4.0"
    const val ktor = "2.2.4"
    const val jUnit = "5.9.0"
}
  1. 在根项目的./settings.gradle.kts中配置pluginManagement:
pluginManagement {
    repositories {
        gradlePluginPortal()
        maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
        google()
        mavenCentral()
    }

    plugins {
        kotlin("jvm").version(Versions.kotlin)
        kotlin("plugin.serialization").version(Versions.kotlin)
        kotlin("multiplatform").version(Versions.kotlin)
        kotlin("android").version(Versions.kotlin)
        id("com.android.tools.build").version(Versions.agp)
        id("com.android.base").version(Versions.agp)
        id("com.android.application").version(Versions.agp)
        id("com.android.library").version(Versions.agp)
        id("org.jetbrains.kotlin").version(Versions.kotlin)
        id("org.jetbrains.compose").version(Versions.compose)

        id("com.squareup.sqldelight").version(Versions.sqlDelight)
    }
}

执行时出现编译错误:

e: D:\src\project\settings.gradle.kts:12:31: Unresolved reference: Versions
e: D:\src\project\settings.gradle.kts:13:48: Unresolved reference: Versions
...

* What went wrong:
Script compilation errors:

  Line 12:         kotlin("jvm").version(Versions.kotlin)
                                         ^ Unresolved reference: Versions

  Line 13:         kotlin("plugin.serialization").version(Versions.kotlin)
                                                          ^ Unresolved reference: Versions

请问该如何修复此问题,或者实现多模块项目版本统一的标准方式是什么?


解决方案

问题原因

settings.gradle.kts的执行优先级高于buildSrc模块的编译,所以在pluginManagement块中无法直接引用buildSrc里定义的Versions对象。

方案一:改用gradle.properties存储版本常量

将版本信息迁移到根目录的gradle.properties文件,这样settings.gradle.kts可以直接读取:

  1. 根目录gradle.properties内容:
VERSION_MY_APP=1.0.12
VERSION_KOTLIN=1.8.20
VERSION_AGP=7.1.3
VERSION_COMPOSE=1.4.0
VERSION_KTOR=2.2.4
VERSION_JUNIT=5.9.0
VERSION_SQLDELIGHT=2.0.0 # 补充原代码中缺失的版本号
  1. 修改settings.gradle.kts:
pluginManagement {
    repositories {
        gradlePluginPortal()
        maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
        google()
        mavenCentral()
    }

    plugins {
        kotlin("jvm").version(extra["VERSION_KOTLIN"] as String)
        kotlin("plugin.serialization").version(extra["VERSION_KOTLIN"] as String)
        kotlin("multiplatform").version(extra["VERSION_KOTLIN"] as String)
        kotlin("android").version(extra["VERSION_KOTLIN"] as String)
        id("com.android.tools.build").version(extra["VERSION_AGP"] as String)
        id("com.android.base").version(extra["VERSION_AGP"] as String)
        id("com.android.application").version(extra["VERSION_AGP"] as String)
        id("com.android.library").version(extra["VERSION_AGP"] as String)
        id("org.jetbrains.kotlin").version(extra["VERSION_KOTLIN"] as String)
        id("org.jetbrains.compose").version(extra["VERSION_COMPOSE"] as String)

        id("com.squareup.sqldelight").version(extra["VERSION_SQLDELIGHT"] as String)
    }
}
  1. 若仍需buildSrc中的Versions对象,可读取系统属性保持统一:
object Versions {
    const val myApp = System.getProperty("VERSION_MY_APP")
    const val kotlin = System.getProperty("VERSION_KOTLIN")
    const val agp = System.getProperty("VERSION_AGP")
    const val compose = System.getProperty("VERSION_COMPOSE")
    const val ktor = System.getProperty("VERSION_KTOR")
    const val jUnit = System.getProperty("VERSION_JUNIT")
    const val sqlDelight = System.getProperty("VERSION_SQLDELIGHT")
}

方案二:使用Gradle版本目录(Version Catalogs)

这是Gradle 7.0及以上版本官方推荐的标准方式,比buildSrc更灵活,支持版本自动更新、跨项目共享等特性:

  1. 在根目录创建gradle/libs.versions.toml文件:
[versions]
myApp = "1.0.12"
kotlin = "1.8.20"
agp = "7.1.3"
compose = "1.4.0"
ktor = "2.2.4"
jUnit = "5.9.0"
sqlDelight = "2.0.0"

[libraries]
# 可在此定义项目依赖库,示例:
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "jUnit" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
agp = { id = "com.android.tools.build", version.ref = "agp" }
agp-base = { id = "com.android.base", version.ref = "agp" }
agp-application = { id = "com.android.application", version.ref = "agp" }
agp-library = { id = "com.android.library", version.ref = "agp" }
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
sqlDelight = { id = "com.squareup.sqldelight", version.ref = "sqlDelight" }
  1. 修改settings.gradle.kts引用版本目录:
pluginManagement {
    repositories {
        gradlePluginPortal()
        maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
        google()
        mavenCentral()
    }
    versionCatalogs {
        create("libs") {
            from(files("gradle/libs.versions.toml"))
        }
    }
}

plugins {
    alias(libs.plugins.kotlin.jvm) apply false
    alias(libs.plugins.kotlin.serialization) apply false
    alias(libs.plugins.kotlin.multiplatform) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.agp) apply false
    alias(libs.plugins.agp.base) apply false
    alias(libs.plugins.agp.application) apply false
    alias(libs.plugins.agp.library) apply false
    alias(libs.plugins.compose) apply false
    alias(libs.plugins.sqlDelight) apply false
}
  1. 在模块的build.gradle.kts中使用插件和依赖:
plugins {
    alias(libs.plugins.agp.library)
    alias(libs.plugins.kotlin.android)
}

dependencies {
    implementation(libs.ktor.client.core)
    testImplementation(libs.junit.jupiter.api)
}

方案三:提前编译buildSrc(不推荐)

如果坚持使用buildSrc,可先单独执行./gradlew buildSrc:build编译版本类,再执行主项目构建。但该方式操作繁琐,且IDE同步易出问题,不适合正式项目。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 04:53:11