Android Development (45 Blogs) Become a Certified Professional

How To Work With Kotlin Native?

Last updated on Nov 27,2019 2.7K Views

5 / 5 Blog from Kotlin

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. 

Kotlin-Native-Edureka

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:

  • Windows (x86_64 only at the moment)
  • Linux (x86_64, arm32, MIPS, MIPS little-endian)
  • macOS (x86_64)
  • iOS (arm64 only)
  • Android (arm32 and arm64)
  • WebAssembly (wasm32 only)
  • Raspberry Pi

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.

  • The Kotlin Native compiler transforms the Kotlin code into LLVM intermediate representation (IR).
  • The LLVM compilers understand IR, then create binaries for the desired platforms. 

You can use another product of JetBrains, IntelliJ platform.

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

Kotlin Native Configuration - Edureka

Select automatically import options.

Kotlin Native Configuration2 - Edureka

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:

  • First of all, you need to create a Gradle script which will compile the application. 
  • Second of all, move the program to src/main/kotlin package

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

  • gradle build – which will build the application
  • gradle run – which will execute our application

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

Advantages

  • One of the primary advantages of Kotlin/Native is the GUI, sensors, notifications and everything that is unique and specified to each device that will be developed in the native language and runtimes without restriction.
  • Barriers are reduced when it is compared to other programming languages.
  • It helps in cross-platform application development.
  • Focuses on sharing as much code required for execution when compared to other cross-platform tools.

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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Work With Kotlin Native?

edureka.co