One of the reasons why Kotlin community is growing is that (starting from version 1.2) includes support for multiplatform projects. A Kotlin multiplatform project allows to compile the same code to multiple target platforms:
It can be compiled to JVM bytecode
Compiling to Native will be added soon
What’s that? Kotlin/Native is a technology for compiling Kotlin to native binaries that run without any Virtual Machine. It comprises a LLVM-based backend for the Kotlin compiler and a native implementation of the Kotlin runtime library. What does it mean to us? We are allowed to compile Kotlin for platforms where VM are not desirable or possible (for example iOS!). Kotlin/Native currently supports the following platforms:
Windows (x86_64 only at the moment)
Linux (x86_64, arm32, MIPS, MIPS little endian)
iOS (arm64 only)
Android (arm32 and arm64)
WebAssembly (wasm32 only)
We can target 3 OS for Desktop, the 2 most popular OS for mobile, and Raspberry Pi!
Example of application written in Kotlin/Native. The screenshots come from iOS and Android KotlinConf App created by JetBrains.
That’s the theory. Let’s check how it looks in practice.
Configuring environment for Kotlin/Native development
Starting with Kotlin/Native is not easy. Unfortunately there is no dedicated IDE for Kotlin/Native development. The only one in JetBrains family which supports it at this time is CLion, which is problematic for projects looking to be multiplatform with JVM, JS or iOS. And the biggest problem, in my opinion, is that CLion doesn’t support Gradle...
Let’s start our adventure with installing CLion. In order to use Kotlin in our projects, we have to install a couple of plugins, namely the Kotlin plugin which provides core support for the language, and the Kotlin/Native plugin which adds functionality for native support. Moreover, the second plugin give us a couple of existing projects templates. I would recommend you to use the CLion Early Access Program builds, because it contains support for most recent Kotlin/Native plugins.
Kotlin/Native gradle plugin
Now we can start with our first application written in Native! Click on the New Project wizard and choose the HelloWorldtemplate. After creation, our project should like the one presented below: Now we can build our project, run and :ta-da: - Hello, Native World! appears on the console. It looks like everything is working properly, but where’s my Gradle? Unfortunately, CLion doesn’t support Gradle, so we need to write the script manually. Kotlin/Native has a plugin for Gradle which allows us to compile, build libraries, reference libraries, and in general perform all the actions that we would need to build our application. In order to compile our HelloWorld application we need to do two things:
First of all we need to create a Gradle script which will compile our application. Below you can find the simplest one needed to compile our app:
Secondly - we have to move our hello.kt file to src/main/kotlin package, which is where our source files are located by default, or we need to add the proper srcFiles
From the root directory, where our build.gradle file is located, we can now run the following commands:
gradle build - which will build the application
gradle run - which will execute our application
One more important thing - In order to be able to use CLion, we need to have our project defined using CMake. The Gradle plugins provides us a convenient way to do this using the command: generateCMake. The result is a new file named CMakeLists.txt which allows us to open the project in CLion.
To sum up, we gained some basic knowledge about Kotlin/Native and sample project setup. As you could read there are some pros of using it in our applications, but there are many cons that makes Kotlin/Native really hard to use:
CLion is the only IDE which supports Kotlin/Native, which is problematic for multiplatform projects
CLion doesn’t support Gradle
Lack of API documentation
Lack of support for Kotlin/Native in multiplatform project
Still no stable version
Kotlin Native is still under development, but in my opinion it looks like it can be a great tool in the nearest future.
Despite all the cons I’ve mentioned above I would like to encourage you to give Kotlin/Native a try!