Kotlin Native replace JVM

Kotlin Native is another step toward making Kotlin usable throughout a modern application. Eventually, it will be possible to use Kotlin to write every component, from the server back-end to the web or mobile clients. Sharing the skill set is one big motivation for this scenario.

How Kotlin Native replace JVM?

Kotlin is all about knowing that Kotlin is compatible with Java because it is built for use on the JVM. Kotlin is often Android or backend. More than that (or whatever ran on our beloved JVM).

More on Kotlin : See other interesting tutorial and news about Kotlin

What if one day we do not want to use the JVM?

In some cases we may have to say goodbye to the JVM for some reason.

  • Want to use existing native library library?
  • Run native app on various platforms.
  • I want to use the machine resources to it.
  • Boring JVM

For these reasons, we have to find a way to bring our beloved Kotlin to native applications.

[tmh_article_ads]

Recommended articleTop 30 Android Tools Every Developer should Know

What is Native?

Speaking of native, come up with a few, and then summarize what it is. What’s the difference with the JVM?

Native translator is native If in a computer system is to run our command set directly on the platform or machine code, each system will have a different format. Even though it is the same work.

The JVM gets a set of bytes of code to figure out how to execute that statement.

If compared Running a direct command set is better than passing another system. Because we have control over the machine.

For Kotlin geekersBye bye Gradle and Maven !! Say hello Kotlin Kobalt

And What is Kotlin Native?

Kotlin Native is a compiler that allows us to write a native app with Kotlin. It uses the LLVM compiler to create machine code based on the platform we want.

This can now be done on the

  • Mac OS X
  • Linux
  • Apple iOS
  • Raspberry Pi
  • Windows
  • Web Assembly (in the future)

Let’s start building a better Kotlin Native app.

From the ordinary app, I will let the app leave Hello Native messages. The project will only use Gradle without any plugin, because Kotlin Native does not have a plugin to play. (Currently at 0.3)New project with IntelliJ IDEA

New project with IntelliJ IDEA

Note that we did not even select the Kotlin (Java) because the JVM’s stdlib could not be shared with the Native, resulting in an error during the compile time.

When you have a project, we set up Kotlin Native before in the build.gradle by this.

group 'com.babedev'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies"
        }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.3"
    }
}

apply plugin: 'konan'

repositories {
    mavenCentral()
}

konanArtifacts {
    HelloNative { }
}

From the settings above. There are two main parts. The part is in buildscript to install kotlin-native-gradle-plugin and konanArtifacts section. Used to configure Kotlin Native, for example.

  • Target tells the platform we want
  • UseInterop defines the definition of the native library we will use, such as header
  • LinkerOpts determines where to use the native library

Do you know : Disadvantage of Kotlin : You must know before use

Press the sync gradle around the finish and then create a file Kotlin.

Fun  main ( args :  Array < String >) {
    Print ( " Hello Native " )
}

The code is standard, then build with the command.

$ ./gradlew build

If the build succeeds, we will get an executable app in this build folder.

Executable app path
Executable app path

Then try to run it.

Just as we have a native app written with Kotlin and quite chic.

For smart developer : Best Android Studio Live Templates for Writing Less Code

Conclusion

Although Kotlin can now be used outside of the JVM, it is still under development. Still, far from production, the Kotlin Native Stdlib is not yet fully released.

[tmh_article_ads]

As for the performance from the benchmark, it is shocking that the Kotlin Native is still slower than the Kotlin JVM, so it will have to wait and see how the future will work.

For this article just ended. Have fun with Kotlin.

Download project here

 

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...