The magic of Kotlin/Native: Part 1

If you are a member of the Android Dev community and have not been living under a rock for the past few years you must have heard of Kotlin. Its a new generation language designed & developed by folks at JetBrains. There was a growing need for an expressive language capable of seamlessly inter-operating with JAVA and JetBrains took it a step further.

Kotlin Compiler is not just capable of converting Kotlin code to Java bytecode, it has made rapid inroads into platforms well beyond the Java domain. You can now code in Kotlin for your web frameworks where the Kotlin/JS plugin converts Kotlin code into JavaScript. You can also code for your iOS applications in Kotlin. Yes you read that right. Kotlin makers have promised us an ecosystem where every part of an Application can be written in Kotlin. From Server-side development with Spring/NodeJs to Front-End development in Android/iOS/Web, it can be all done in Kotlin and with great ease.

Supporting multiple platforms for mobile devices is not a new concept. We have had multiple technologies trying to achieve this, e.g. Ionic, Xamarin, ReactJs & Flutter. Most of them follow the philosophy of code once, run anywhere. With Kotlin/MultiPlatform you follow code common and respect the platform. This will be a multi part article covering the following topics

  • Kotlin/Native and how it enables us to code for native platforms
  • Kotlin/MultiPlatform and how it enables us to share code among platforms

What is Kotlin/Native ?

Is it a new language? Is it a Kotlin framework? Is it something that will finally make aliens pay attention to us?

The short answer to all the above is No. But what it is can be derived from the following definition :-

Kotlin/Native is a technology for compiling Kotlin to native binaries that run without any VM. It comprises of a LLVM-based backend for the Kotlin compiler and a native implementation of the Kotlin runtime library.

If you are like me this must be your reaction

Lets break this definition into smaller and understandable pieces.

  • Back-end for the Kotlin compiler : 
    Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).
  • Native Implementation: 
    Broadly speaking, Native code is any code whose memory is not managed by the underlying framework but has to be managed by the programmer themselves. i.e. their is no Garbage collection.
    e.g. C++’ delete and C’s free
  • LLVM-based: LLVM is a compiler toolchain used to create compilers.
Clearly these guys have not heard of CSS/JavaScript
In short, Kotlin/Native can be used to compile Kotlin code and run it on native platforms. I could create a Hello world program, churn it through the Kotlin compiler and run it on Windows, Linux, MacOS, iOS etc. without the help of any Virtual Machine.

The Setup

Step 1: Choose the IDE
Download and install CLion from JetBrains and install the Kotlin/Native plugin.

Step 2: Create a Project
Create a new HelloWorld Kotlin/Native project. This is how your project structure will look like

New Project Snapshot

CmakeLists.txt contains the build instructions for the Kotlin/Native compiler. Its configured to read hello.kt as its source and output HelloWorld binary executable.

cmake_minimum_required(VERSION 3.8)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/KotlinCMakeModule)

project(vivek Kotlin)

konanc_executable(
NAME HelloWorld
SOURCES hello.kt
)

Step 3: Edit the hello.kt as follows

import platform.posix.getpid
fun main(args: Array<String>) {
println("Hello, The pid is ${getpid()}")
}

Here I am using the posix implementation provided by default with the Kotlin/Native compiler to print the current process pid.

Run and voila. The compiler downloads some dependencies and runs your program.

Hello, The pid is 8301

Note:
Depending upon the platform you are working on, Kotlin Compiler will generate an executable for that platform only.

That’s all for this article folks. In the next article we are going to discuss how we can interop between Kotlin and C/C++ code.

Here is the source code


Thanks for reading! Share this article if you found it useful.
Please do Clap 👏 to show some love :)

Follow Androidiots for more such amazing android related content.