Chapters

Hide chapters

Kotlin Apprentice

Second Edition · Android 10 · Kotlin 1.3 · IDEA

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section III: Building Your Own Types

Section 3: 8 chapters
Show chapters Hide chapters

Section IV: Intermediate Topics

Section 4: 9 chapters
Show chapters Hide chapters

25. Kotlin/Native
Written by Joe Howard

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

While Kotlin began its existence focused on the JVM, it has since expanded in scope to include native and web development. In fact, as you’ll see in the next chapter “Kotlin Multiplatform”, alongside its popularity in Android development, you can even use Kotlin to assist in building iOS apps.

The technology used to bring Kotlin beyond the JVM is called Kotlin/Native. Kotlin/Native allows you to compile Kotlin code outside of virtual machines, resulting in self-contained binaries that are native to the environment in which they’re run. The Kotlin/Native compiler was announced in 2017, and in late 2018 reached version 1.0.

In previous chapters, you’ve used IntelliJ IDEA to create and run Kotlin code. IntelliJ utilized the version of the Kotlin compiler for the JVM to build and run Java bytecode. In this chapter, you’ll see how to install and use the Kotlin/Native compiler outside of IntelliJ in order to create native binaries that can be run outside the JVM.

Konan and LLVM

This diagram below shows the process through which Kotlin/Native takes Kotlin code and turns it into native code.

The Kotlin/Native compiler itself is named Konan. Konan handles the front-end of the process, compiling the Kotlin source code into an Intermediate Representation (IR) that is the input to the back-end of the process. Kotlin/Native leverages the LLVM compiler for the back-end.

If you’re not familiar with LLVM, at one point it stood for “Low-Level Virtual Machine”, but it actually has nothing to do with virtual machines as the term is used today. The name is now just a stand-alone acronym.

LLVM was created initially by a team led by Chris Lattner, who also led the team that created the Swift language at Apple. LLVM is a set of components that optimize IR code and compile it to machine-dependent binaries.

By combining Konan with LLVM, Kotlin/Native lets you produce native executables from your Kotlin code. In theory, Kotlin/Native can be used on any platform supported by LLVM.

Installation

To install the stand-alone Kotlin/Native compiler on macOS, a good place to start is the Kotlin/Native page on the Kotlin language web site: https://kotlinlang.org/docs/reference/native-overview.html.

tar -xzvf kotlin-native-macos-1.3.50.tar.gz

diff kotlinc konanc
export PATH="$PATH:~/bin/kotlin-native-macos-1.3.50/bin"
source ~/.bashrc

Hello, Kotlin/Native

With a Kotlin/Native compiler in place, you can now proceed to write your first native program in Kotlin.

fun main() {
  
}
println("Hello, Kotlin/Native!")
konanc -o hello hello.kt

Kotlin Standard Library

One thing you may notice is that, when compiled with v1.3.50 of Kotlin/Native, the executable file hello.kexe is about 650 KB on macOS, which is about 100 times larger than the executable for an equivalent program written in C would be. That large size is due to the fact that the Kotlin standard library is statically linked in to the executable file.

val numbers = listOf(1, 2, 3)
println(numbers.size)
konanc -o hello hello.kt

Challenges

val numbers = (1..100).toList()

Key points

  • Kotlin/Native is used to compile Kotlin code to native binaries that can be run without virtual machines and Kotlin runtimes.
  • The name of the Kotlin/Native compiler is Konan. The command to run Konan is konanc.
  • Kotlin/Native leverages the LLVM compiler to produce native binaries. Konan acts as a front-end for LLVM, producing an Intermediate Representation for LLVM.
  • The Kotlin/Native compiler can be installed from it’s GitHub page at https://github.com/kotlin-native.
  • When installing the Kotlin/Native compiler, be sure to distinguish it from the kotlin-jvm compiler used to create Java bytecode.
  • Kotlin/Native code starts with a main function, similar to other C-like languages.
  • The Kotlin standard library is statically linked to Kotlin/Native executables.

Where to go from here?

Now that you’ve got a handle on the use of Kotlin/Native and what it’s for, the next chapter will utilize Kotlin/Native in the context of a Kotlin Multiplatform project. Kotlin/Native will be used by your IDE to compile shared Kotlin code for use on iOS.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now