Reputation: 13597
When we create a new project in Android Studio following line is added to the app modules gradle file.
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
But what is the purpose of adding this dependency?
I thought it allows us to use Kotlin standard library, but it is not about making Kotlin standard library available to us, since we can use Kotlin standard library functions without it.
Upvotes: 9
Views: 7983
Reputation: 833
In addition to getting Kotlin's stdlib as a transitive dependency (see CommonsWare answer), if you use Kotlin plugin 1.4+ you will get it from the plugin itself.
This is what running dependencies
task with empty dependencies section shows:
releaseRuntimeClasspath - Runtime classpath of compilation 'release' (target (androidJvm)).
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0
+--- org.jetbrains.kotlin:kotlin-stdlib:1.8.0
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.8.0
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0
\--- org.jetbrains.kotlin:kotlin-stdlib:1.8.0
Moreover, starting from Kotlin 1.8 kotlin-stdlib-jdkX
are merged into kotlin-stdlib
, so if you ever need to declare dependency manually, you should use kotlin-stdlib
. (note: as seen above, 1.8 version of the plugin still adds kotlin-stdlib-jdk8
dependency. I think this is done to avoid problems with transitive dependencies)
Upvotes: 2
Reputation: 1006674
But what is the purpose of adding this dependency?
It contains the Kotlin standard library.
we can use Kotlin standard library functions without it
You are getting the Kotlin standard library through a transitive dependency.
For example, in Android Studio 3.5.3, I created a new "Empty Activity" project. I then trimmed back the dependencies
to:
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
However, in the "External Libraries" portion of the explorer tree, I could still see the Kotlin standard library. Running the dependencies
task shows that the Kotlin standard library is pulled in by core-ktx
and the kotlin-android-extensions
plugin:
releaseRuntimeClasspath - Runtime classpath of compilation 'release' (target (androidJvm)).
+--- org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.61
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.61
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.61
| \--- org.jetbrains:annotations:13.0
+--- androidx.appcompat:appcompat:1.1.0
| +--- androidx.annotation:annotation:1.1.0
| +--- androidx.core:core:1.1.0
| | +--- androidx.annotation:annotation:1.1.0
| | +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.1.0
| | | +--- androidx.lifecycle:lifecycle-common:2.1.0
| | | | \--- androidx.annotation:annotation:1.1.0
| | | +--- androidx.arch.core:core-common:2.1.0
| | | | \--- androidx.annotation:annotation:1.1.0
| | | \--- androidx.annotation:annotation:1.1.0
| | +--- androidx.versionedparcelable:versionedparcelable:1.1.0
| | | +--- androidx.annotation:annotation:1.1.0
| | | \--- androidx.collection:collection:1.0.0 -> 1.1.0
| | | \--- androidx.annotation:annotation:1.1.0
| | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| +--- androidx.cursoradapter:cursoradapter:1.0.0
| | \--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| +--- androidx.fragment:fragment:1.1.0
| | +--- androidx.annotation:annotation:1.1.0
| | +--- androidx.core:core:1.1.0 (*)
| | +--- androidx.collection:collection:1.1.0 (*)
| | +--- androidx.viewpager:viewpager:1.0.0
| | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | +--- androidx.core:core:1.0.0 -> 1.1.0 (*)
| | | \--- androidx.customview:customview:1.0.0
| | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | \--- androidx.core:core:1.0.0 -> 1.1.0 (*)
| | +--- androidx.loader:loader:1.0.0
| | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | +--- androidx.core:core:1.0.0 -> 1.1.0 (*)
| | | +--- androidx.lifecycle:lifecycle-livedata:2.0.0
| | | | +--- androidx.arch.core:core-runtime:2.0.0
| | | | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | | | \--- androidx.arch.core:core-common:2.0.0 -> 2.1.0 (*)
| | | | +--- androidx.lifecycle:lifecycle-livedata-core:2.0.0
| | | | | +--- androidx.lifecycle:lifecycle-common:2.0.0 -> 2.1.0 (*)
| | | | | +--- androidx.arch.core:core-common:2.0.0 -> 2.1.0 (*)
| | | | | \--- androidx.arch.core:core-runtime:2.0.0 (*)
| | | | \--- androidx.arch.core:core-common:2.0.0 -> 2.1.0 (*)
| | | \--- androidx.lifecycle:lifecycle-viewmodel:2.0.0 -> 2.1.0
| | | \--- androidx.annotation:annotation:1.1.0
| | +--- androidx.activity:activity:1.0.0
| | | +--- androidx.annotation:annotation:1.1.0
| | | +--- androidx.core:core:1.1.0 (*)
| | | +--- androidx.lifecycle:lifecycle-runtime:2.1.0 (*)
| | | +--- androidx.lifecycle:lifecycle-viewmodel:2.1.0 (*)
| | | \--- androidx.savedstate:savedstate:1.0.0
| | | +--- androidx.annotation:annotation:1.1.0
| | | +--- androidx.arch.core:core-common:2.0.1 -> 2.1.0 (*)
| | | \--- androidx.lifecycle:lifecycle-common:2.0.0 -> 2.1.0 (*)
| | \--- androidx.lifecycle:lifecycle-viewmodel:2.0.0 -> 2.1.0 (*)
| +--- androidx.appcompat:appcompat-resources:1.1.0
| | +--- androidx.annotation:annotation:1.1.0
| | +--- androidx.core:core:1.0.1 -> 1.1.0 (*)
| | +--- androidx.vectordrawable:vectordrawable:1.1.0
| | | +--- androidx.annotation:annotation:1.1.0
| | | +--- androidx.core:core:1.1.0 (*)
| | | \--- androidx.collection:collection:1.1.0 (*)
| | +--- androidx.vectordrawable:vectordrawable-animated:1.1.0
| | | +--- androidx.vectordrawable:vectordrawable:1.1.0 (*)
| | | +--- androidx.interpolator:interpolator:1.0.0
| | | | \--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | \--- androidx.collection:collection:1.1.0 (*)
| | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| +--- androidx.drawerlayout:drawerlayout:1.0.0
| | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | +--- androidx.core:core:1.0.0 -> 1.1.0 (*)
| | \--- androidx.customview:customview:1.0.0 (*)
| \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
+--- androidx.core:core-ktx:1.0.2
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.2.50 -> 1.3.61 (*)
| +--- androidx.annotation:annotation:1.0.2 -> 1.1.0
| \--- androidx.core:core:1.0.2 -> 1.1.0 (*)
\--- androidx.constraintlayout:constraintlayout:1.1.3
\--- androidx.constraintlayout:constraintlayout-solver:1.1.3
When I removed those two things, now the Kotlin standard library was no longer pulled in as a transitive dependency... and now I don't have access to Kotlin standard library stuff like listOf()
.
Upvotes: 11