Kotlin/Native
- Kotlin course
- Expert
- Lesson 19/47
- 5 min
Learn native compilation with Kotlin/Native.
What is Kotlin/Native?
Kotlin/Native compiles Kotlin code into a native binary without the JVM. It is LLVM-based and supports iOS, macOS, Linux and Windows targets.
A Kotlin/Native project starts with the kotlin("multiplatform") plugin in Gradle; targets are added with blocks like iosArm64() and linuxX64(). The output is either an executable or a library (framework, .so, .dylib). There is no startup time; small binaries are produced. In this lesson we model the concept with a simple platformInfo function.
Target platforms
Kotlin/Native supports many targets: iOS (arm64), macOS, Linux (x64, arm64), Windows (x64, mingw) and embedded systems.
Each target is added with a separate Gradle block; e.g. iosArm64() for iPhone, linuxX64() for Linux desktop. The output varies: a framework on iOS, an ELF binary on Linux, a PE binary on Windows. A project can host several targets at once; this is the basis of Kotlin Multiplatform. Target dependencies are managed via sourceSets.
Practice Write platformInfo
Define fun platformInfo(): String. It should return "Kotlin/Native". In main, write println(platformInfo()).
Starter code
// add the platformInfo function
fun main() {
// print the platform info
}Sample solution
fun platformInfo(): String = "Kotlin/Native"
fun main() {
println(platformInfo())
}Expected output
Kotlin/Native
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which backend does Kotlin/Native use?
- LLVM Correct answer
- CLR
- JVM
- V8
Explanation: Kotlin/Native is built on LLVM; it produces a native binary.
2 Complete the platformInfo method
fun ____(): String = "Kotlin/Native"
fun platformInfo(): String = "Kotlin/Native"
Explanation: platformInfo() declares the native target.
3 Match native parts
| Term | Meaning |
|---|---|
kotlin("multiplatform") |
Gradle plugin |
iosArm64() |
iPhone target |
linuxX64() |
Linux target |
LLVM |
Compile backend |