KMP 版本目录的集中管理方案?

解读

在国内 Kotlin Multiplatform(KMP)落地的面试场景中,面试官真正想考察的是“跨平台工程化”能力:

  1. 能否把 Android、iOS、桌面、后端等模块的依赖版本收敛到一处,避免“各写各的”导致线上事故;
  2. 能否让 CI/CD、发版脚本、自动化测试都能无侵入地读取同一份版本号;
  3. 能否在 Gradle 8、Android Gradle Plugin 8、Xcode 15、CocoaPods 1.14 等国内主流工具链下稳定工作;
  4. 能否让不熟悉 Gradle 的 iOS 同事也能一眼看懂版本变更。
    因此,回答必须给出“一套能直接搬进公司代码仓库”的完整方案,而不是只讲 Gradle 语法。

知识点

  1. Gradle Version Catalog(TOML)原理与多项目 includeBuild 复用方式;
  2. KMP 特有的 iosArm64/iosX64/androidNative 等 target 别名与 library 坐标;
  3. CocoaPods 的 Podspec 模板注入,以及如何用 Gradle Task 把版本号同步到 iosApp/Podfile.lock;
  4. 国内镜像加速(阿里云、清华、华为)在 settings.gradle.kts 中的写法;
  5. gradle.properties 与 local.properties 的优先级,以及如何在 AGP 8 的 namespace 约束下保持版本统一;
  6. CI 场景下通过环境变量覆盖版本号,实现“一键出包”hotfix;
  7. 回滚策略:版本目录与 Git Tag 对齐,回滚时只需 revert TOML 文件即可。

答案

  1. 工程结构
root
├── gradle
│   └── libs.versions.toml   // 唯一真理源
├── build-conventions
│   ├── settings.gradle.kts
│   └── src/main/kotlin
│       ├── kmp-library.gradle.kts
│       └── publish.gradle.kts
├── shared                   // KMP 模块
├── androidApp
└── iosApp                   // Xcode 工程
  1. gradle/libs.versions.toml(核心)
[versions]
# 语言与插件
kotlin = "1.9.23"
agp = "8.2.2"
compose = "1.6.2"
coroutines = "1.8.0"
ktor = "2.3.9"
moko = "0.23.0"

# 国内镜像占位符,CI 可覆盖
repoMaven = "https://repo.huaweicloud.com/repository/maven/"

[libraries]
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
android-library = { id = "com.android.library", version.ref = "agp" }
  1. build-conventions 复用
    settings.gradle.kts
dependencyResolutionManagement {
    versionCatalogs {
        create("libs") { from(files("../gradle/libs.versions.toml")) }
    }
}

把 build-conventions 作为 included build,所有业务模块通过 plugins { id("kmp-library") } 即可隐式引用同一套版本。

  1. KMP 模块 build.gradle.kts
plugins {
    alias(libs.plugins.kotlin.multiplatform)
    alias(libs.plugins.android.library)
    id("publish") // 自定义发布脚本
}

kotlin {
    jvm()
    iosArm64()
    iosX64()
    iosSimulatorArm64()

    sourceSets {
        commonMain.dependencies {
            implementation(libs.kotlinx.coroutines.core)
            implementation(libs.ktor.client.core)
        }
    }
}
  1. 同步到 iOS
    在 shared 目录新增 syncPodspec.gradle.kts
tasks.register("generatePodspec") {
    val libVersion = libs.versions.kotlin.get()
    doLast {
        file("shared.podspec").writeText("""
            Pod::Spec.new do |s|
              s.name         = 'shared'
              s.version      = '$libVersion'
              s.source       = { :path => '.' }
              s.ios.deployment_target = '15.0'
            end
        """.trimIndent())
    }
}

./gradlew generatePodspec 后,iOS 同事执行 pod install 即可保证 Xcode 侧与 Gradle 侧版本一致。

  1. CI 覆盖
    GitHub Actions / GitLab CI 中
- name: Hotfix version
  run: echo "repoMaven=https://oss.sonatype.org/content/repositories/snapshots/" >> local.properties

无需改 TOML,也能出临时包。

  1. 回滚
    只要 revert gradle/libs.versions.toml 一条 commit,所有模块(含 iOS)下次构建自动回到旧版本,无需手动改多处。

拓展思考

  1. 如果公司同时存在 Maven 发布与 CocoaPods 发布,可把 TOML 再拆成 maven.versions.toml 与 pods.versions.toml,通过 includeBuild 聚合,实现“双轨制”发版;
  2. 当 KMP 模块数 >50 时,可把版本目录发布到私有 Maven 仓库,其他仓库通过 versionCatalogs { create("libs") { from("com.xxx:catalog:1.0.0") } } 远程依赖,实现“版本即制品”;
  3. 未来 Kotlin 2.0 将提供 kotlin-project-structure 插件,可提前在 build-conventions 里做适配层,等官方稳定后一键切换;
  4. 若需要合规审计,可在 generatePodspec Task 里追加 s.license = 'Apache-2.0' 并读取 TOML 中的 license 字段,保证双端合规信息同源。