What is the kotlin("...") method in the Gradle Kotlin DSL?

Viewed 731

What is this kotlin("...") method described in Kotlin's "Using Gradle" tutorial? They mention this syntax.

plugins {
    kotlin("jvm") version "1.3.71"
}

I see it used in more places too, not just the plugin. For example, it can be used to get Kotlin dependencies.

dependencies {
    implementation(kotlin("stdlib-jdk8"))
}

I have tried googling this but I'm having trouble finding results since Gradle uses a Kotlin DSL as well and the results for the two get mixed.

Running gradle init --dsl kotlin --type kotlin-library outputs the "normal" syntax (see below for snipped output) and I can't find documentation on this strange kotlin("...") piece about what it can and can't be used for. If I use the kotlin("...") approach as above it still works but I'm trying to figure out what it is and where it comes from.

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")
}
1 Answers

It's an extension function provided by Gradle. It's distributed in gradle-kotlin-dsl-<version>.jar!/org/gradle/kotlin/dsl/KotlinDependencyExtensions.kt. Find out more by Ctrl+clicking the function name in the IDE.

/**
 * 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" } ?: ""}"
Related