上游 · Compiler options in the Kotlin Gradle plugin
[]
apiVersion
Restrict the use of declarations to those from the specified version of bundled libraries
"2.0", "2.1", "2.2", "2.3", "2.4", "2.5" (EXPERIMENTAL)
全部配置 / APP-APIVERSION
apiVersion · APP-APIVERSION · Gradle DSL · 同源入口 api-version
默认值:视情况。未设则跟工具链 API 默认走。使用场合:场景可配。场景可配:有明确目标(性能、包体、安全、兼容)时再改。
用途。限制只能使用指定版本及更早的 Kotlin 标准库 API。用于兼容性约束;常见与 languageVersion 配合。
影响。锁定语言/API 能力边界;设错会导致源码无法编译或误用新 API。
适用场景。编译器/后端排查或信息查询;定位完成后关闭冗余输出。
可写。<属性值> (Gradle Property:org.gradle.api.provider.Property<org.jetbrains.kotlin.gradle.dsl.KotlinVersion>)
定义依据:dsl/KotlinCommonCompilerOptions.kt 在仓库中打开
// 文件:build.gradle.kts(应用的 Kotlin 模块,例如 composeApp)
kotlin {
compilerOptions {
apiVersion.set(/* value */)
}
}
应用工程只按上方「怎么用」里的 Gradle 示例配置。下面列出编译器还认识的旧名字,避免对着旧文档混用。
优先级:Gradle DSL 与 CLI 配置的是同一 API 版本旋钮;工程里优先用 compilerOptions。
compilerOptions.apiVersion(键 apiVersion) — Gradle DSL(compilerOptions)。-api-version <version>(键 api-version) — 旧编译器旗标;应用工程用本页推荐的 compilerOptions,不要再写一条 freeCompilerArgs。不写这个配置时,编译器会怎么处理?
此选项的默认值视情况而定(如目标平台或构建类型)。详见下方默认值说明。
未设则跟工具链 API 默认走。
以下摘抄原文中与本配置最相关的段落,便于速读;完整上下文请点原文。
[]
apiVersion
Restrict the use of declarations to those from the specified version of bundled libraries
"2.0", "2.1", "2.2", "2.3", "2.4", "2.5" (EXPERIMENTAL)