JVM-Free Kotlin With Kotlin/Native

DZone 's Guide to

JVM-Free Kotlin With Kotlin/Native

JetBrains recently came out with a tech preview for Kotlin/Native. The new arrival compiles Kotlin straight to machine code, opening up new cross-platform potential.

· Java Zone ·
Free Resource

I would never call myself a real programmer, but I have a healthy obsession with studying new languages that emerge, especially those that arrive with little baggage and attempt to solve current problems and new use cases. This brought about my explorations in recent years of Swift and Kotlin, both initially aimed to fix issues with the languages traditionally used in their worlds, but rapidly became used in more widespread contexts, and generally, have enthusiastic communities.

One of the positives of Kotlin was its interoperability with the language it intended to supplement and replace, as Kotlin runs in the JVM, it was relatively easy to run it alongside your existing Java stack, replacing components of, or complete code-bases. Last week JetBrains announced a tech preview of Kotlin/Native that compiles your code straight to machine code, opening it up to a whole other world of platforms and use cases where virtual machines aren’t desirable or possible. I couldn’t resist taking a look and seeing how it worked.

To accomplish this, JetBrains are using the widespread and (fairly) standard LLVM compiler that opens up a world of common compilation targets with little effort and is a well-trodden path (also taken by Swift). Interestingly, among other potential targets, this also opens Kotlin up to iOS platforms, adding another potential cross-platform option for mobile developers to consider.

First, download a distribution for your platform of choice. Well, for now, choose from:

Let’s dive into a moderately complex example, a Tetris game. You can find the complete source code on GitHub, but you will also find a copy of the same code inside the samples folder you downloaded.

The basic compilation command takes the following form and will also download any dependencies you need:

bin/kotlinc /samples/tetris/Tetris.kt -o Tetris.kexe

But as with any code compilation command, a more realistic command typically consists of linking in external libraries, specifying targets and more, for example, the Tetris game uses the SDL library for rendering graphics. The example includes a build.sh file to give you ideas on what’s possible.

Run that with ./build.sh to create an executable, then open that executable in a terminal to run the application. There’s no native application packaging yet.

Image title

Want to compile the application for iOS instead? Then prepend the command with the correct target:

TARGET=iphone ./build.sh

But then it was unclear about how to get this to run on an iOS device of an emulator, but this is early days after all!

The release has few performance optimizations and a limited standard library, but with the compiler and tooling available on GitHub under an Apache 2 OS’ license, you are encouraged to dig in and take a look around.

cross-platform, java, jvm, kotlin, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}