Kotlin 的“使用 Gradle”教程中描述的这个 kotlin("...")
方法是什么?他们提到 this syntax .
plugins {
kotlin("jvm") version "1.3.71"
}
我看到它也在更多地方使用,而不仅仅是插件。例如,它可以是 used to get Kotlin dependencies .
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
我尝试用谷歌搜索这个,但我很难找到结果,因为 Gradle 也使用 Kotlin DSL,并且两者的结果混杂在一起。
运行 gradle init --dsl kotlin --type kotlin-library
输出“正常”语法(见下方的输出片段),我找不到文档在这个奇怪的 kotlin("...")
文章中,介绍了它的用途和用途。如果我像上面那样使用 kotlin("...")
方法它仍然有效,但我试图弄清楚它是什么以及它来自哪里。
plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin.
id("org.jetbrains.kotlin.jvm") version "1.3.71"
// Apply the java-library plugin for API and implementation separation.
`java-library`
}
// ...
dependencies {
// Align versions of all Kotlin components
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
最佳答案
这是Gradle提供的扩展功能。它分布在 gradle-kotlin-dsl-<version>.jar!/org/gradle/kotlin/dsl/KotlinDependencyExtensions.kt
.在 IDE 中按住 Ctrl 并单击函数名称可了解更多信息。
/**
* Builds the dependency notation for the named Kotlin [module] at the given [version].
*
* @param module simple name of the Kotlin module, for example "reflect".
* @param version optional desired version, unspecified if null.
*/
fun DependencyHandler.kotlin(module: String, version: String? = null): Any =
"org.jetbrains.kotlin:kotlin-$module${version?.let { ":$version" } ?: ""}"
关于gradle - Gradle Kotlin DSL 中的 kotlin ("...") 方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836231/