How To Work With Kotlin Native?

Last updated on Nov 27,2019 2.8K Views

How To Work With Kotlin Native?

edureka.co

Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. This is something fascinating for someone who is new to Kotlin. So, in this article, I’ll focus more on Kotlin Native platform. 

I’ll be discussing the topics in this order:

Let’s begin!

What is Kotlin Native?

Kotlin Native is an astonishingly new product from JetBrains that allows the developers to code native applications for Linux, macOS, Windows, and other platforms. This means that we are allowed to compile Kotlin for platforms where Virtual machines are not desirable or possible, like embedded devices or iOS. 

It comprises of an LLVM (Low-Level Virtual Machine)-based backend for the Kotlin compiler and native implementation of the Kotlin run-time library.

Now you might ask, what are the different platforms that it supports. The answer to this question is:

Now that you have understood this, let’s move ahead and understand how exactly you can obtain this compiler.

How to configure the environment for Kotlin Native?

If you are starting with Kotlin, you will find it very easy in the beginning and then when you upgrade to Kotlin Native, you will not find it very easy because there are not many dedicated IDEs out there that help in the development process.

The only IDE in the JetBrains family that 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. This is a major reason why I will not be using CLion for the implementation.

You can use another product of JetBrains, IntelliJ platform.

Let’s see how to select the option Kotlin Native.

Select automatically import options.

Then provide a project name and click on Finish.

Hurray! You’ve clear with how you can select Kotlin Gradle.

Now let’s move ahead and understand how to write a simple program in Kotlin Native.

Let’s see a simple Hello World program.

We can open up our favorite IDE or editor and write the following code in a file named hello.kt file. 

fun main() {
println("Hello Kotlin/Native!")
}

Now, there is a little change in the compilation process. To manually compile the application call the downloaded compiler and generate a hello. kexe (Linux and macOS) or hello.exe (Windows) binary file:

kotlinc-native hello.kt -o hello

While compilation from the console seems to be easy and clear, you should notice, that it does not scale well for bigger projects with hundreds of files and libraries. In addition to this, the command line approach does not explain to the IDE that how it can open such a project, where the sources are located, what dependencies are used, or how the dependencies are downloaded and so on.

Kotlin Native Gradle

The New Project wizard in IntelliJ IDEA can be used to start a new Kotlin/Native project with just a single click. Just select the Native | Gradle option to generate the project.

I’ll first create a project folder. All the paths will be relative to this folder. Sometimes the missing directories will have to be created before new files are added.

Now talking about the language support for Gradle, Gradle supports Groovy and Kotlin in order to build the scripts.

Groovy is the oldest supported scripting language for Gradle. It leverages the power of dynamic typing and runtime features. Sometimes it can be harder to maintain Groovy build scripts.

Now in order to run the scripts and compile the basic HelloWorld application, you need to do two things:

From the root directory, where build. gradle file is located, you can now run the following commands:

Now, let’s move ahead to the final topic of this article.

Advantages

This brings us to the end of this article on Kotlin Native. Hope you are clear with all that has been shared with you in this article.

Now that you have gone through our Kotlin Native blog, you can check out Edureka’s Android App Development Certification Training Got a question for us? Please mention it in the comments of “Kotlin Native” blog section and we will get back to you.

BROWSE COURSES