beruic
beruic

Reputation: 5876

Which standard library to use in Kotlin

In Kotlin, when working with the JVM, it seems there is multiple choices for standard library, namely kotlin-stdlib, kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8.

I cannot, however, find anything telling me the difference between these. The only visible difference I have found is that I cannot use com.fasterxml.jackson.databind.exc.MismatchedInputException with kotlin-stdlib, but I can with kotlin-stdlib-jdk8.

Is there anywhere I can read about the advantages using one over the others, or can anyone explain this in layman terms?

Upvotes: 31

Views: 10739

Answers (4)

Dario Seidl
Dario Seidl

Reputation: 4640

As of Kotlin 1.8, the different standard libraries have been merged, and you can just use kotlin-stdlib. JVM targets 1.6 and 1.7 are not supported anymore.

Updated JVM compilation target

In Kotlin 1.8.0, the standard libraries (kotlin-stdlib, kotlin-reflect, and kotlin-script-*) are compiled with JVM target 1.8. Previously, the standard libraries were compiled with JVM target 1.6.

Kotlin 1.8.0 no longer supports JVM targets 1.6 and 1.7. As a result, you no longer need to declare kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 separately in build scripts because the contents of these artifacts have been merged into kotlin-stdlib.

note

If you have explicitly declared kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 as dependencies in your build scripts, then you should replace them with kotlin-stdlib.

Note that mixing different versions of stdlib artifacts could lead to class duplication or to missing classes. To avoid that, the Kotlin Gradle plugin can help you align stdlib versions.

What's new in Kotlin 1.8 - Updated JVM compilation target

Upvotes: 7

mbonnin
mbonnin

Reputation: 7032

Most of the stdlib is in the plain kotlin-stdlib artifact.

kotlin-stdlib-jdk7 adds suppressed exceptions and a few extension methods.

kotlin-stdlib-jdk8 adds ThreadLocalRandom as well as a few other extension methods and retrieving groups by name in Regexes.

The code is there: https://github.com/JetBrains/kotlin/blob/55c8b35eee2ee8a93ccaffeaac6f9c3e4fd4ff18/libraries/stdlib/jvm/src/kotlin/internal/PlatformImplementations.kt#L27

EDIT: I got curious so wrote an article about this: https://medium.com/@mbonnin/the-different-kotlin-stdlibs-explained-83d7c6bf293. Bottom line: Android declares a weird JVM version so almost nothing from -jdk7 and -jdk8 is used.

Upvotes: 32

Mir-Ismaili
Mir-Ismaili

Reputation: 17168

By running Gradle dependencies task in your Kotlin project, you can find some useful information. This is a part of output in a sample project:

$ ./gradlew dependencies

...

kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.20
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.20
     |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.20
     |    \--- org.jetbrains:annotations:13.0
     +--- org.jetbrains.kotlin:kotlin-script-runtime:1.3.20
     +--- org.jetbrains.kotlin:kotlin-reflect:1.3.20
     |    \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.20 (*)
     \--- org.jetbrains.intellij.deps:trove4j:1.0.20181211

...

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692023

As the name indicates, -jdk8 is supposed to be used when using the JDK8. It contains code used to integrate the changes made in the JDK 8 into the Kotlin standard lib.

As its pom indicates, it depends on -jdk7, which contains the code needed to integrate the changes made in the JDK 7 into the Kotlin standard lib.

And as the pom of -jdk7 indicates, it depends on the stdlib.

So, in short, use the one matching your JDK. Adding it to the dependencies will also, transitively, add all the ones for the previous versions of the JDK.

Upvotes: 19

Related Questions