
The different kotlin-stdlibs explained
(Not sure you should use kotlin-stdlib-jdk8…)
Edit 2022–06–30: reworked the conclusion to highlight that the Kotlin Gradle plugin now adds the stdlib automatically
Edit 2023–01–16: Kotlin 1.8.0 dropped support for Java 1.6 and 1.7 and therefore all artifacts are now merged
I love Kotlin as it’s a concise yet powerful language with very sensible defaults and design decisions. In that regard, it adopts the Zen of Python:
There should be one-- and preferably only one --obvious way to do it.
One area where this failed though is the configuration of the stdlib. This is kind of ironic as it’s also one of the first thing you configure as a Kotlin newcomer.
What’s the best between the below? And what should you choose on an Android device with partial Java 8 support ?
// so many stdlib to choose from!
implementation("org.jetbrains.kotlin:kotlin-stdlib-jre8")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jre7")
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7")kotlin-stdlib-jre7 and kotlin-stdlib-jre8
These are the easy ones. They have been deprecated in favour of their -jdk7 and -jdk8 counterparts 2 years ago so you can just forget about them. This was made to accomodate the java9 module system and more specifically to avoid split packages. You can read the details from the github commit and release notes.
You might see them occasionally. Don’t use them.
kotlin-stdlib, -jdk7 and -jdk8
According to the Kotlin doc:
The Kotlin standard library kotlin-stdlib targets Java 6 and above. There are extended versions of the standard library that add support for some of the features of JDK 7 and JDK 8. To use these versions, add one of the following dependencies instead of kotlin-stdlib.Indeed, if you look at the kotlin-stdlib-jdk7 pom file, you can see that it transitively depends on kotlin-stdlib:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.3.50</version>
<scope>compile</scope>
</dependency>If you include kotlin-stdlib-jdk7, it will pull kotlin-stdlib.
If you include kotlin-stdlib-jdk8, it will pull kotlin-stdlib-jdk7 and kotlin-stdlib. You can also check that by running ./gradlew :dependencies:
compileClasspath - Compile classpath for compilation 'main' (target (jvm)).
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.50
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50
\--- org.jetbrains.kotlin:kotlin-stdlib:1.3.50 (*)We can also check the sizes on mavenCentral (https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/):
martin@bowser-castle$ ls -al kotlin-stdlib-*.jar1326269 Sep 14 16:58 kotlin-stdlib-1.3.50.jar
3129 Sep 14 16:58 kotlin-stdlib-jdk7-1.3.50.jar
15476 Sep 14 16:58 kotlin-stdlib-jdk8-1.3.50.jarkotlin-stdlib is 1MB+ while the other two are a few kB at most. That confirms that most of the functionnality is in kotlin-stdlib with additions in -jdk7 and -jdk8.
So let’s pull kotlin-stdlib-jdk8 and we’ll have everything, right ? Well… not so sure, let’s look at what’s inside these artifacts.
kotlin-stdlib
Contains:
- most of the functionality: Collections, Ranges, Math, Regex, File extensions, Locks, etc... Most of what you use daily is in kotlin-stdlib.
kotlin-stdlib-jdk7
Contains:
- Reflection-free suppressed exceptions
Suppressed exception were added in Java 7 at the same time as try-with-resources. It gives more information when an exception is thrown while releasing a resource:




