ariya.io About Talks Articles

First Look: Kotlin Native

2 min read

A big surprise in the Kotlin land was the technology preview of Kotlin/Native that can compile your Kotlin program into native executable, thereby completely eliminating the need for Java Virtual Machine.

As a relatively young language, Kotlin gained some popularity in the last few years. Kotlin presents a syntax relatively familiar to Java programmers. Since it came from JetBrains (the maker of IntelliJ, WebStorm, Android Studio, etc), the tooling support is excellent. Interoperability with Java is top notch, not merely an afterthought. And of course, Kotlin is a viable alternative for developing Android applications.

Running Kotlin programs requires a virtual machine (Java or JavaScript). This is however changing. Just yesterday, JetBrains announced the availability of the technical preview of Kotlin/Native. By leveraging LLVM, Kotlin/Native compiles a Kotlin program into a self-contained executable for iOS, macOS, and Linux (Windows support will be coming later).

To try Kotlin/Native, simply download it (illustrated here for Linux):

$ curl -OL http://download.jetbrains.com/kotlin/native/kotlin-native-linux-0.1.tar.gz
$ tar xf kotlin-native-linux-0.1.tar.gz
$ cd kotlin-native-linux-0.1

Also, make sure you have Java 8 installed on the system. Often, it is as easy as installing OpenJDK 8, either via the package manager or a third-party distribution such as Zulu from Azul Systems.

Let us try it with a simple program:

$ cat << EOF > hola.kt
> fun main(args : Array<String>) {
>     println("Hello from Kotlin!")
> }
> EOF
$ bin/kotlinc hola.kt -o hola

If the Kotlin compiler, kotlinc, is invoked for the first time, it needs to grab some additional LLVM-related packages (which may take a while), giving the prompt such as:

Downloading native dependencies (LLVM, sysroot etc).
This is a one-time action performed only on the first run of the compiler.
...

Once the compilation is completed, the executable is ready:

> ./hola
Hello from Kotlin

To my excitement, the size of the executable (after running strip) is rather small, around 163 KB. This is quite good for such an initial tech preview. As expected, the resulting executable does not have a lot of dependencies:

$ ldd ./hola
        linux-vdso.so.1 (0x00007ffc419e3000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4362b4c000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f436284b000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f436262e000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4362418000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f436206d000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4362d50000)

It is also fascinating that this tech preview already has the support for interfacing with C functions (often known as FFI, foreign function interface). The Kotlin/Native git repository at github.com/JetBrains/kotlin-native contains a few sample applications which demonstrate it, including an OpenGL teapot demo (via GLUT):

OpenGL teapot

Do you think 2017 will be an exciting year for Kotlin? I believe so!

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook