全部配置 / KN-OPT

opt

opt · KN-OPT · 编译器参数(Native)

重要:高 · 影响大 默认:视情况(按目标/构建类型) 场合:场景可配 来源:上游原样

默认值:视情况。KGP Release 常开;工程可关闭(如防链接 OOM)。使用场合:场景可配。Release。

它是做什么的

用途。启用编译期优化,通常用于发布/性能构建。原文:Enable optimizations during compilation.

影响。通常提升运行性能;可能增加编译耗时并影响调试。

适用场景。Release / 性能构建默认考虑开启;日常 Debug 通常关闭以保可调试性。

取值说明

布尔 · 写法 -opt(开启) · 默认 false(BooleanType.defaultFalse)

可写。(开关存在 / true) / false / 不传

定义依据:description/NativeCompilerArguments.kt 在仓库中打开

怎么用

  1. 配置入口:build.gradle.kts → ohosArm64.binaries.all { freeCompilerArgs += "-opt" }
  2. 示例
// 文件:build.gradle.kts(应用的 Kotlin 模块,例如 composeApp)
kotlin {
  ohosArm64 {  // 按实际 native target 调整
    binaries.all {
      freeCompilerArgs += "-opt"
    }
  }
}
  1. 生效:Sync 后执行该目标的 compile/link。
  2. 验证:日志出现 `-opt`;再按场景做一次运行/体积/可调试性冒烟。
  3. 回滚:从 binaries.all 的 freeCompilerArgs 中删除该项后 Clean 并重编。
  4. 注意:Debug误开难调试

默认行为 视情况

不写这个配置时,编译器会怎么处理?

此选项的默认值视情况而定(如目标平台或构建类型)。详见下方默认值说明。

KGP Release 常开;工程可关闭(如防链接 OOM)。

相关文档

以下摘抄原文中与本配置最相关的段落,便于速读;完整上下文请点原文。

上游 · Kotlin compiler options · Native

-nostdlib

Don't link with stdlib.

-opt

Enable compilation optimizations and produce a binary with better runtime performance. It's not recommended to combine it with the -g option, which lowers the optimization level.

-output name (-o name )

Set the name for the output file.

-produce output (-p output )

Specify output file kind:

program

static

dynamic

打开原文

溯源与对照

状态
稳定(正式选项)
版本基线
2.2.21-1.0.0
定义位置类型
编译器参数定义(arguments DSL)
源码路径
compiler/arguments/src/org/jetbrains/kotlin/arguments/description/NativeCompilerArguments.kt 在仓库中打开