Which standard library to use in Kotlin

前端 未结 3 1354
刺人心
刺人心 2021-02-13 05:41

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

相关标签:
3条回答
  • 2021-02-13 06:19

    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
    
    ...
    
    0 讨论(0)
  • 2021-02-13 06:25

    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.

    0 讨论(0)
  • 2021-02-13 06:45

    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.

    0 讨论(0)
提交回复
热议问题