What's the difference between Kotlin JVM and Kotlin Native?
Asked Answered
S

1

22

So I know Kotlin Native is obviously Native and Kotlin JVM isn't but is the code between Kotlin JVM and Kotlin Native: 1. Different Compiler and Different Code 2. Different Compiler and Similar Code 3. Different Compiler and Same Code 4. None of the above (please explain)

Sarina answered 28/3, 2019 at 21:58 Comment(0)
B
30

The Kotlin/JVM and Kotlin/Native compilers share the front-end (the part that performs code parsing, name resolution, type inference etc.), but the compiler back-ends that translate the internal program representation to the target code (the JVM bytecode and LLVM bitcode, respectively) are different.

The Kotlin language accepted by the two compilers is the same, but some of the features and checks are platform-specific. Also, the standard libraries for Kotlin/JVM and Kotlin/Native are sufficiently different, see the APIs available on each platform here: Kotlin Standard Library.

Another big difference is the memory model: Kotlin/JVM uses the Java memory model, while Kotlin/Native offers its own concurrency and memory model.

Also, the dependencies that one can use in Kotlin/JVM and Kotlin/Native projects are different. In addition to the projects built using the same Kotlin target:

  • Kotlin/JVM can also use any libraries built for the JVM (written in Java, Scala etc.)

  • Kotlin/Native can also interoperate with native libraries written in C (or at least having C headers) using the C interop tools.

  • Both Kotlin/JVM and Kotlin/Native can use Kotlin Multiplatform libraries. Given that a dependency is a multiplatform library, one can entirely reuse the code working with it between Kotlin/JVM and Kotlin/Native.

Bellybutton answered 28/3, 2019 at 22:40 Comment(3)
Is there any grammatical difference? Which one is available to write Java code in Kotlin project? And how can i know that my project is Kotlin/JVM or Kotlin/Native?Madiemadigan
is Android studio default projects a kotlin/jvm? Can it be kotlin/native?Eustis
Yes, Android projects using Kotlin are normally Kotlin/JVM. The Kotlin code is first compiled to JVM bytecode and then either distributed as JVM class files as a part of AAR (Android library archive). I don't think there's a project template for Kotlin/Native targeting Android, but you can setup it manually with Kotlin Multiplatform. The right targets for NDK would be androidNativeArm64, androidNativeX64, androidNativeArm32, and androidNativeX86. Note that Kotlin/Native Android code can't use the normal Android SDK and is actually built with Android NDK.Bellybutton

© 2022 - 2024 — McMap. All rights reserved.