Kotlin Academy · Lesson

Kotlin/Native Overview: Targets, Toolchain & Compilation

Understand available Kotlin/Native targets and compile a simple CLI binary.

Lesson 1 of 413 steps

Kotlin/Native Overview: Targets, Toolchain & Compilation is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

What Is Kotlin/Native?

Kotlin/Native compiles Kotlin source code directly to native machine code via LLVM, without a JVM or Dalvik runtime. The result is a self-contained binary — no JVM installation required on the target machine.

Supported Targets

Kotlin/Native supports a wide range of targets:

  • macOS / iOS: macosX64, macosArm64, iosX64, iosArm64, iosSimulatorArm64
  • Linux: linuxX64, linuxArm64
  • Windows: mingwX64
  • Embedded / watchOS / tvOS: various targets

The Compilation Pipeline

Kotlin/Native's compilation pipeline: Kotlin source → Kotlin IR (intermediate representation) → LLVM IR → native binary. The konanc compiler drives this process, but in practice you always use the Kotlin Multiplatform Gradle plugin.

Setting Up a Native Target in Gradle

In a Kotlin Multiplatform project, declare native targets in build.gradle.kts:

kotlin {
    macosArm64("macos") {
        binaries {
            executable { entryPoint = "main" }
        }
    }
    linuxX64("linux") {
        binaries { executable { entryPoint = "main" } }
    }
}

Source Sets for Native Targets

Native targets share commonMain/commonTest with other platforms. Target-specific code lives in source sets like macosMain or linuxX64Main. Use expect/actual for platform-specific APIs.

Output Binary Types

Kotlin/Native can produce several binary types:

  • executable — standalone command-line program
  • staticLib — static library for linking into C/C++ projects
  • sharedLib — dynamic (.dylib / .so / .dll) library
  • framework — Apple framework for iOS/macOS integration

Running Native Tests

Run native tests with ./gradlew macosArm64Test (or the relevant target task). Tests compile to a native binary and execute on the host machine — no JVM is involved.

Cross-Compilation Limitations

Kotlin/Native cross-compilation is limited: you can compile for Apple targets only on macOS (Xcode required). Linux and Windows targets can be compiled on any supported host. CI pipelines typically use macOS agents for Apple targets.

The klib Format

Kotlin/Native libraries are distributed as .klib files — Kotlin's platform-independent library format. Dependencies are resolved as .klibs and linked into the final binary at compilation time.

Kotlin/Native in a KMP iOS App

In a standard Kotlin Multiplatform Mobile (KMM) project, shared code compiles to an Apple framework that Xcode links into the iOS app. Swift/Objective-C code calls Kotlin APIs through generated headers.

Performance Characteristics

Native binaries start instantly (no JVM startup), have a smaller memory footprint, and are suitable for command-line tools, server-side Linux applications, and iOS/macOS shared logic. Long-running JVM applications may outperform Native due to JIT optimizations, but startup and memory advantages favour Native for many use cases.

Quick Check

Which Gradle binary type produces an Apple framework from Kotlin/Native for use in an iOS Xcode project?

Recap: Kotlin/Native Overview

Key takeaways:

  • Kotlin/Native compiles to native machine code via LLVM — no JVM required
  • Supports macOS, iOS, Linux, Windows, watchOS, tvOS targets
  • Configure targets and binary types in the KMP Gradle plugin
  • Apple targets require macOS + Xcode; cross-compilation has platform restrictions
  • Libraries distributed as .klib; iOS integration via framework binary
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Kotlin/Native Overview: Targets, Toolchain & Compilation” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Kotlin/Native Overview: Targets, Toolchain & Compilation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “Kotlin/Native Overview: Targets, Toolchain & Compilation”?

Understand available Kotlin/Native targets and compile a simple CLI binary. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Kotlin/Native Overview: Targets, Toolchain & Compilation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Kotlin/Native Overview: Targets, Toolchain & Compilation
  2. Memory Management in Kotlin/Native: The New MM
  3. C Interop with cinterop and .def Files
  4. Kotlin/WASM: Compiling to WebAssembly for the Browser
← Back to Kotlin Academy