skip to content

How do you apply the Kotlin Gradle Plugin for a JVM project, and what does applying org.jetbrains.kotlin.jvm actually add to your build?

level: juniorimportance: must knowfreq 70%

answer

  1. plugins {} block, id org.jetbrains.kotlin.jvm
  2. applies the Java/JVM plugin under it
  3. adds kotlin {} extension
  4. compileKotlin / compileTestKotlin tasks
  5. kotlin-stdlib added by default

basics

~10 s

Add id("org.jetbrains.kotlin.jvm") version "…" in the plugins {} block. It registers Kotlin source sets, a kotlin {} extension, and compileKotlin tasks that compile .kt files into JVM bytecode.

solid answer

~30 s

You apply it in the `plugins {}` block: `id("org.jetbrains.kotlin.jvm") version "2.0.0"`. KGP is layered on top of the Java/JVM plugin, which it applies for you, so you get the standard `main`/`test` source sets plus Kotlin variants of them. Concretely it adds: a `kotlin {}` project extension for configuring the toolchain and compiler, `compileKotlin`/`compileTestKotlin` tasks (instances of `KotlinCompile`) wired into the `classes`/`testClasses` lifecycle, and the `kotlin-stdlib` dependency by default. It also lets `.kt` and `.java` sources live together and compile in the right order. In version-catalog setups you reference it as `alias(libs.plugins.kotlin.jvm)` and pin the version centrally.

code

kotlin · 10 lines
kotlin
plugins {
    id("org.jetbrains.kotlin.jvm") version "2.0.0"
}

repositories { mavenCentral() }

dependencies {
    // kotlin-stdlib is added automatically by KGP
    testImplementation(kotlin("test"))
}

go deeper

for a junior

Know the plugin id and that you apply it in plugins {}, and that it lets you compile Kotlin.

for a middle

Explain that it layers on the JVM plugin, adds compileKotlin tasks, the kotlin {} extension, and stdlib.

for a senior

Discuss plugins {} vs apply(), version-catalog aliasing, and how Kotlin output integrates into existing source sets/lifecycle.

for a principal

Frame plugin-version governance across many modules (catalog, convention plugins) and the stdlib-default toggle as policy.

## What the Kotlin Gradle Plugin (KGP) is KGP is the Gradle plugin published by JetBrains that teaches Gradle how to compile Kotlin. For server/library work the relevant plugin id is `org.jetbrains.kotlin.jvm` (the legacy id `kotlin` and `kotlin("jvm")` in the Kotlin DSL are the same thing). There are sibling plugins (`multiplatform`, `android`, `js`) but the JVM one is the everyday case. ## How you apply it The modern, recommended way is the `plugins {}` block, because it resolves the plugin through the Gradle Plugin Portal and lets Gradle reason about it statically: ```kotlin plugins { id("org.jetbrains.kotlin.jvm") version "2.0.0" } ``` With a version catalog you instead pin the version in `gradle/libs.versions.toml` and write `alias(libs.plugins.kotlin.jvm)`, keeping the version in one place across modules. ## What applying it adds Applying KGP causes several things to happen: - It **applies the JVM base** (the Java plugin), so you inherit the `main` and `test` source sets, the `compileJava` tasks, the `jar` task, and the configurations like `implementation`/`api`/`runtimeOnly`. - It adds a **Kotlin source directory** to each source set — `src/main/kotlin` and `src/test/kotlin` — and lets `.kt` and `.java` coexist. - It registers **compilation tasks**: `compileKotlin` and `compileTestKotlin`, which are `org.jetbrains.kotlin.gradle.tasks.KotlinCompile` tasks. These are wired so that `classes` depends on `compileKotlin`, and Kotlin output ends up on the same classpath as Java output. - It exposes the **`kotlin {}` extension** on the project, the single entry point for configuring the JVM toolchain and the compiler. - It adds **`kotlin-stdlib`** to your dependencies automatically (controllable via `kotlin.stdlib.default.dependency`). ## Why the layering matters Because KGP sits on top of the JVM plugin rather than replacing it, everything you know about Java source sets, configurations, and the build lifecycle still applies — KGP just inserts Kotlin compilation into that existing machinery. That is why a mixed Java+Kotlin module "just works": both compilers feed the same source set outputs.

  • Why prefer the plugins {} block over the older apply(plugin = "…") form?
    The plugins {} block resolves and pins plugin versions through the plugin-management system, gives Gradle static knowledge of the plugin (better up-front validation and editor support), and avoids buildscript classpath leakage between projects.
  • Where does the kotlin-stdlib dependency come from if you never declared it?
    KGP adds it automatically. You can disable that with the Gradle property `kotlin.stdlib.default.dependency=false` if you want to manage the stdlib version yourself.

saying these in an interview costs you the question

  • Saying KGP replaces the Java plugin — it applies and builds on top of it.
  • Claiming you must manually add kotlin-stdlib — KGP adds it by default.

context