Kotlin Tooling Native Build Performance

kotlin/kotlin-agent-skills/skills/kotlin-tooling-native-build-performance

作者 kotlinc2f90697bf71Apache-2.01K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

Diagnoses and fixes slow Kotlin/Native compilation and linking in Kotlin Multiplatform projects that target iOS. Use when the user reports slow iOS or shared-framework builds, long linkDebug*/linkRelease* or XCFramework tasks, cold CI builds that re-download the Kotlin/Native toolchain, KSP or other generated code on the native path, transitiveExport usage, or asks for a local-development versus CI build performance plan.

AI 產生的概覽

診斷並修復 Kotlin Multiplatform iOS 專案中緩慢的 Kotlin/Native 編譯與連結。

功能
引導代理對緩慢的建置情境進行分類,執行唯讀稽核指令碼,並在修改前後量測建置。接著依安全順序套用修正:還原 Gradle 與 Kotlin/Native 快取、縮小建置任務與目標範圍、降低匯出與產生程式碼的成本,最後才是實驗性開關。最終產出一份簡短效能說明,包含量測證據、安全變更以及前後對照指令。
適用情境
當使用者回報 iOS 或共享框架建置緩慢、linkDebug 或 linkRelease 或 XCFramework 任務耗時過長、CI 冷建置反覆下載 Kotlin/Native 工具鏈,或原生路徑上存在 KSP 與產生程式碼時使用。也適用於規劃本機開發與 CI 建置效能的場景。
執行需求
需要搭配 Gradle 的 Kotlin Multiplatform 專案;最好有 macOS 主機與 Xcode 以實際執行建置。此技能附帶可執行稽核指令碼(scripts/audit-native-build.sh),並引用官方 Kotlin 文件連結。

Kotlin/Native Build Performance

Turn "the iOS build is slow" into a measured diagnosis and a small set of safe fixes. Two rules apply throughout:

  1. Never trade away required release behavior. A faster local loop must not change what CI publishes.
  2. Measure before and after with the same command and the same build state. An unmeasured fix is a guess.

Step 0: Classify the Slow Scenario

Establish four facts before editing anything: where (local or CI), what (debug feedback loop or release/distribution artifact), state (first build, clean, warm, or no-op), and phase (which tasks dominate the log). Then match the dominant symptom:

Symptom in the build logLikely causeRead
linkRelease* or *ReleaseXCFramework tasks in a local development loopBuilding distribution artifacts for developmentartifacts-and-targets [blocked]
Kotlin/Native compiler distribution downloaded on every CI run~/.konan not preserved between runscaching-and-gradle [blocked]
Long pause before the first task startsConfiguration phase, no configuration cachecaching-and-gradle [blocked]
All iOS targets build when only one simulator is neededBroad task (build, assemble, assemble*XCFramework) or unused targetsartifacts-and-targets [blocked]
ksp* tasks ahead of compileKotlinIos*Generated-code work on the native pathexports-and-generated-code [blocked]
Small source edit recompiles and relinks everythingCompiler caches disabled, or missing incrementalitycaching-and-gradle [blocked], experimental [blocked]
Machine overloaded while several link* tasks run at onceParallel native linkingcaching-and-gradle [blocked], worker-limit caveat

Step 1: Audit and Measure

  1. Run the static audit from the project root:

    bash
    scripts/audit-native-build.sh /path/to/project

    It is read-only and prints file:line findings (disabled caches, broad local tasks, transitiveExport, broad KSP configuration, missing CI .konan cache), each pointing at the reference file with the fix. Findings are leads, not verdicts — confirm each against project policy.

  2. Find the command the user actually waits for: a script, a CI step, or the Gradle invocation inside an Xcode build phase. Optimize that command, not a task you picked yourself.

  3. Run it twice when practical. The first build downloads Kotlin/Native components and fills caches; only the second and later runs are representative. Attribute time per task before blaming the compiler:

    properties
    kotlin.build.report.output=file   # writes build/reports/kotlin-build/

    Gradle's --scan or --profile work too.

  4. If you cannot run the build (no macOS host, no Xcode), analyze logs, build scans, or checked-in metrics instead — and state explicitly that the conclusion is static.

Step 2: Fix in Safe Order

Apply fixes one at a time, re-measuring as you go:

  1. Restore healthy defaults — remove cache/daemon workarounds, enable Gradle build and configuration caches, keep ~/.konan warm in CI, update Kotlin: references/caching-and-gradle.md [blocked]
  2. Build only what the feedback loop needs — one specific task per loop, correct integration method, justified target matrix: references/artifacts-and-targets.md [blocked]
  3. Cut export and generated-code cost — drop transitiveExport, narrow export(...), scope KSP work to the native compilations that need it: references/exports-and-generated-code.md [blocked]
  4. Experimental switches last, with the user's agreement: references/experimental.md [blocked]

Worked Example

A developer on an Apple Silicon Mac complains that "every shared-module change costs 12 minutes". Their loop runs ./gradlew :shared:assembleXCFramework. A build scan of the second (warm) run shows:

:shared:linkReleaseFrameworkIosArm64             348s:shared:linkReleaseFrameworkIosX64               341s:shared:compileKotlinIosX64                       96s:shared:linkDebugFrameworkIosSimulatorArm64       41s:shared:compileKotlinIosSimulatorArm64            38sconfiguration phase                               64s

Reasoning chain:

  • The loop is local + debug + warm, but ~690s goes to linkRelease* — release linking is an order of magnitude slower than debug and only CI needs it. Replace the local command with :shared:linkDebugFrameworkIosSimulatorArm64 (or the Xcode embed task if Xcode drives the build). (artifacts-and-targets)
  • All iosX64 work serves Intel simulators; ask whether the team still supports them before removing the target. (artifacts-and-targets)
  • 64s of configuration on every run disappears behind org.gradle.configuration-cache=true once trialed. (caching-and-gradle)
  • Expected loop after the change: ~40s compile + ~40s link on warm builds — confirm by re-running the new command twice and comparing.
  • CI keeps assembleXCFramework untouched; note that explicitly in the report.

Verify

  • Re-run the exact baseline command; compare warm build against warm build, not warm against cold.
  • Second run with the configuration cache reports it is being reused.
  • The local development log no longer contains linkRelease*, *ReleaseXCFramework, or removed generator tasks.
  • CI still produces every required release artifact, unchanged.
  • Tests pass and the app still runs from Xcode.
  • scripts/audit-native-build.sh reports no findings you have not consciously accepted and documented.

Report Your Changes

Close with a short performance note:

  • The slow scenario (local/CI, debug/release, cold/warm) and the measured evidence — or a statement that the analysis was static.
  • Each change, and why it is safe for release behavior.
  • The before/after commands the user can run to confirm the win.
  • Remaining tradeoffs: experimental flags enabled, targets removed under a policy assumption, worker limits, or generated-code work deferred.
  • Links to the relevant official documentation below.

Official Documentation

來源與署名

來源:kotlin/kotlin-agent-skills位於skills/kotlin-tooling-native-build-performance提交c2f9069

授權條款: Apache-2.0

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架