kcire_eae
kcire_eae

Reputation: 349

Is the JDK required for Kotlin?

I have not used Android for a long time but now I find a new term, Kotlin, so my doubt is, JDK is required for Kotlin?

If Kotlin will replace to Java, so the JDK it is not necessary because according to me JDK is for develop on Java, is correct?

If it is correct then I have other doubt (this already it's old) why JDK is used for Android if this already include an Android SDK?

P. D. I am going crazy, I need a single explanation.

Upvotes: 14

Views: 25917

Answers (5)

CLOVIS
CLOVIS

Reputation: 998

The Kotlin compiler itself (kotlinc) does not require a JDK, even when targetting the JVM. It does, however, require a JRE to run itself and to run any created JVM apps.

Other programs in your compilation toolchain may still require a JDK (e.g. Gradle) and you will need a JDK if you plan to compile Java code as part of your Kotlin project.

If you want to convince yourself of that fact, run this Dockerfile:

FROM archlinux:latest
RUN pacman -Syuu --noconfirm jre-openjdk-headless kotlin
ENTRYPOINT [ "/usr/bin/kotlinc-jvm" ]

It will run the Kotlin/JVM REPL with only a headless JRE. You can refer to Java classes, like java.util.ArrayList.

Upvotes: 6

Taslim Oseni
Taslim Oseni

Reputation: 6263

Yes! Kotlin 1.1.2 (and above) require JDK.

Also, very importantly, Kotlin wasn't supposed so as to "replace" Java. This is another very common erroneous notion. Kotlin was merely added to the list of languages supported for writing Android apps.

You can check out the link below to get a feel of the language's history: https://en.wikipedia.org/wiki/Kotlin_(programming_language)

Upvotes: 1

zsmb13
zsmb13

Reputation: 89548

Yes, the JDK is required for Kotlin development on the JVM.

Historically, Kotlin worked with JDK 1.6+ targets. Starting with Kotlin 1.5, Kotlin needs a JDK with version 1.8 (i.e. Java 8) or higher.

Upvotes: 14

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28228

As already mentioned, Kotlin JVM requires the JVM to work. Kotlin compiles to JVM bytecode, which means it has the same requirements as Java (runtime and development kit). This is also why Kotlin has Java interop. Additionally, this is the one you're most likely to find yourself using with Android.

However, there is Kotlin Native. The SDK itself is slightly different from Kotlin JVM, and it's still a WIP, but it compiles without the need for JVM. Specifically, it compiles to a native target using LLVM. This version of Kotlin supports C interop.

Kotlin Native also opens the door for Kotlin Multiplatform, which compiles to whatever you want (mobile targets, desktop targets, and it packs it all into one, and includes interop with the native programming language). NOTE: I'm not sure what Kotlin Native with Multiplatform compiles to. Some demo projects use Kotlin JVM in the Android module, which suggests it doesn't use NDK-style native code. Multiplatforms, like Native, is experimental. I also haven't touched Multiplatform much, but it appears to be using a combination of declared functions and platform-declared functions. I'm not sure how interop works here (in terms of languages - not module interop).

Multiplatform doesn't require Native though, but it enables significantly more platforms. If you're using a cross-platform Java library (for an instance LibGDX), or otherwise build on modules with a core and platforms, you can use Multiplatform here too. However, note that this likely requires the JDK, although it does depend on your project.

In addition to these, there's also Kotlin.JS, which as the name suggests, compiles to JavaScript. Unlike Kotlin JVM, it naturally doesn't require the JVM. However, it instead requires the JavaScript-related APIs. And as you'd expect, Kotlin.JS supports JavaScript interop.

TL;DR:

The JDK isn't always required. Kotlin Native and Kotlin JS do not require the JDK, because the targets they compile to isn't on the JVM.

Upvotes: 7

bichito
bichito

Reputation: 1426

Kotlin compiles to jvm bytecode. It NEEDS a jvm. It can be used in place of Java the language

Upvotes: -3

Related Questions