20

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)

SmushyTaco
  • 1,421
  • 2
  • 16
  • 32

1 Answers1

28

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.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 2
    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? – shn Feb 27 '21 at 09:19
  • is Android studio default projects a kotlin/jvm? Can it be kotlin/native? – Hamid Z Jun 22 '22 at 09:46
  • 1
    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. – hotkey Jun 22 '22 at 14:36