当我们在 Android Studio 中创建新项目时,以下行将添加到应用程序模块 gradle 文件中。
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
但是添加这个依赖的目的是什么呢?
我认为它允许我们使用 Kotlin 标准库,但这并不是让 Kotlin 标准库可供我们使用,因为我们可以在没有它的情况下使用 Kotlin 标准库函数。
最佳答案
But what is the purpose of adding this dependency?
它包含 Kotlin 标准库。
we can use Kotlin standard library functions without it
您将通过传递依赖关系获取 Kotlin 标准库。
例如,在Android Studio 3.5.3中,我创建了一个新的“空 Activity ”项目。然后我将依赖项
修剪为:
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
但是,在资源管理器树的“外部库”部分,我仍然可以看到 Kotlin 标准库。运行 dependencies
任务显示 Kotlin 标准库已被 core-ktx
和 kotlin-android-extensions
插件引入:
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
当我删除这两件事后,现在 Kotlin 标准库不再作为传递依赖项被引入......现在我无法访问 Kotlin 标准库的内容,例如 listOf()
.
关于android - kotlin-stdlib-jdk 依赖的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59591618/