How do you enable Kotlin/JVM compilation in a Gradle Kotlin DSL build, and what does applying the Kotlin Gradle Plugin actually give you?
answer
- plugins { kotlin("jvm") } == id org.jetbrains.kotlin.jvm
- Registers compileKotlin / compileTestKotlin
- Runs kotlinc via the Kotlin daemon
- stdlib added automatically
- Exposes the kotlin { } extension
basics
~10 sAdd the Kotlin JVM plugin in the plugins block of build.gradle.kts. That tells Gradle to compile your .kt files, sets up source folders, and adds the Kotlin standard library automatically.
solid answer
~40 sIn build.gradle.kts you declare `plugins { kotlin("jvm") version "2.x" }`, which is shorthand for the plugin id `org.jetbrains.kotlin.jvm`. Applying the Kotlin Gradle Plugin (KGP) does several things: it registers `compileKotlin`/`compileTestKotlin` tasks that invoke the Kotlin compiler (kotlinc) via the Kotlin daemon, wires Kotlin source sets (`src/main/kotlin`, `src/test/kotlin`) into the Java/Gradle source set model, makes `compileKotlin` produce classes consumed by the `jar`/`classes` tasks, and—because the JVM plugin also applies the Java plugin behavior—integrates with the standard `build`/`test` lifecycle. In current KGP the stdlib (`kotlin-stdlib`) is added automatically to the `implementation`/`api` configurations, so you don't declare it yourself. You still typically pair it with a toolchain (`kotlin { jvmToolchain(21) }`) to fix the JDK.
code
kotlin · 14 linesplugins {
kotlin("jvm") version "2.1.0"
}
kotlin {
jvmToolchain(21)
}
repositories { mavenCentral() }
dependencies {
// kotlin-stdlib is added automatically by the plugin
testImplementation(kotlin("test"))
}go deeper
Knows to put kotlin("jvm") in the plugins block and that it compiles .kt files and brings stdlib.
Explains the plugin id mapping, the compile tasks it registers, and source-set wiring.
Discusses the Kotlin daemon, stdlib auto-dependency opt-out, and version-in-one-place resolution.
Frames KGP as the integration layer between Gradle's task/lifecycle model and kotlinc, and reasons about applying vs. resolving plugins across a multi-module build.
## What the Kotlin Gradle Plugin (KGP) is The **Kotlin Gradle Plugin** is the Gradle plugin that teaches Gradle how to compile Kotlin. Without it, Gradle has no idea what to do with `.kt` files. For a plain JVM project the plugin id is **`org.jetbrains.kotlin.jvm`**. ## Applying it in the Kotlin DSL In `build.gradle.kts`: ```kotlin plugins { kotlin("jvm") version "2.1.0" } ``` - `kotlin("jvm")` is a **helper function** the Gradle Kotlin DSL provides; it expands to `id("org.jetbrains.kotlin.jvm")`. - The `version` is only declared where the plugin is *resolved* (usually the root/settings via a version catalog or here). In submodules you apply it without a version: `kotlin("jvm")`. ## What applying it actually does - **Registers compile tasks**: `compileKotlin` and `compileTestKotlin` (typed `KotlinCompile`). These run **kotlinc** through the **Kotlin daemon** (a long-lived JVM process that keeps the compiler warm for speed). - **Wires source sets**: adds `src/main/kotlin` and `src/test/kotlin` as Kotlin source roots, and also lets Kotlin compile code under `src/main/java` so Kotlin and Java can be mixed. - **Hooks the lifecycle**: `compileKotlin` output feeds the `classes`, `jar`, `test`, and `build` tasks, so the normal Gradle lifecycle just works. - **Adds the standard library**: modern KGP automatically adds **`kotlin-stdlib`** as a dependency, so you do not write it by hand (and can opt out via `kotlin.stdlib.default.dependency=false`). - **Exposes the `kotlin { }` extension**: where you configure the **toolchain** (`jvmToolchain(21)`) and **`compilerOptions { }`**. ## Minimal complete example ```kotlin plugins { kotlin("jvm") version "2.1.0" } kotlin { jvmToolchain(21) } repositories { mavenCentral() } ``` That is enough to compile and run a Kotlin/JVM project: the plugin drives kotlinc, the toolchain pins the JDK, and stdlib comes in automatically.
- Do you still need to declare kotlin-stdlib as a dependency?No. Modern KGP adds it automatically. You'd only add it manually if you set kotlin.stdlib.default.dependency=false to control the version yourself.
- Why apply the version only in one place?A plugin is resolved once for the build; submodules just apply it. Declaring the version repeatedly risks conflicting versions, so you pin it centrally (settings/version catalog/root).
The KGP is the adapter that lets the Gradle 'engine' understand the Kotlin 'fuel' — without it, .kt files are just unread text.
saying these in an interview costs you the question
- Thinking you must manually add kotlin-stdlib to every module
- Confusing the plugin id with a library coordinate (it is a plugin, not a dependency)
- Claiming Kotlin only compiles src/main/kotlin and never src/main/java
- Saying the plugin 'is' the compiler — it drives kotlinc, it isn't kotlinc