skip to content

Kotlin Gradle Plugin Config

The Kotlin Gradle plugin is where you set the toolchain, compiler options, and source sets that drive kotlinc. Pinning the JDK with jvmToolchain rather than relying on the machine's default is the habit worth naming.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

How do you enable Kotlin/JVM compilation in a Gradle Kotlin DSL build, and what does applying the Kotlin Gradle Plugin actually give you?

level: juniorimportance: must knowfreq 70%

answer

  1. plugins { kotlin("jvm") } == id org.jetbrains.kotlin.jvm
  2. Registers compileKotlin / compileTestKotlin
  3. Runs kotlinc via the Kotlin daemon
  4. stdlib added automatically
  5. Exposes the kotlin { } extension

basics

~10 s

Add 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 s

In 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 lines
kotlin
plugins {
    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

for a junior

Knows to put kotlin("jvm") in the plugins block and that it compiles .kt files and brings stdlib.

for a middle

Explains the plugin id mapping, the compile tasks it registers, and source-set wiring.

for a senior

Discusses the Kotlin daemon, stdlib auto-dependency opt-out, and version-in-one-place resolution.

for a principal

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

context

open as a page

What does `kotlin { jvmToolchain(21) }` do, and how is it different from setting jvmTarget?

level: middleimportance: must knowfreq 65%

basics

~20 s

jvmToolchain(21) tells Gradle to compile and run with a JDK 21, downloading one if needed. jvmTarget only sets the bytecode version. The toolchain controls which JDK runs the build; the target controls what class-file version is produced.

open as a page

How do you configure Kotlin compiler flags in modern KGP via the `compilerOptions { }` block, and how does that differ from the old kotlinOptions DSL?

level: middleimportance: should knowfreq 50%

basics

~20 s

Use the kotlin { compilerOptions { } } block to set things like the language version, JVM target, and extra compiler args. It replaces the older kotlinOptions block and uses lazy Gradle Property values you set with .set(...).

open as a page

How does the Kotlin JVM plugin lay out source sets, and how do you add or customize Kotlin source directories?

level: seniorimportance: should knowfreq 40%

basics

~10 s

The plugin creates main and test source sets, compiling code in src/main/kotlin and src/test/kotlin (plus the matching java folders). You can add or change directories through the sourceSets configuration in the build script.

open as a page

How does KGP wire in the Kotlin standard library, and when and how would you override that behavior?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

The Kotlin plugin adds the standard library for you automatically, matching the plugin's version, so you don't declare it. You can turn that off with a Gradle property if you need to control the version yourself.

open as a page