What you'll learn
Real projects are more than a pile of .kt files — they need to be compiled, have their dependencies fetched, be tested, and be packaged. On the JVM that job belongs to a build tool, and for Kotlin that's almost always Gradle, driven by a build script written in Kotlin itself.
By the end you'll be able to:
- Understand what Gradle does and why projects need it
- Read a
build.gradle.ktsKotlin DSL script - Add dependencies from repositories like Maven Central
- Organise a multi-module project and know key libraries
Gradle fundamentals
Gradle automates the build: it resolves and downloads dependencies, compiles your code, runs your tests, and produces a runnable artifact (a JAR). You describe what your project is in a build script, and Gradle figures out how to build it. Every project ships a gradlew wrapper so anyone can build it with the exact right Gradle version — no manual install needed:
./gradlew build # compile, test, and assemble everything
./gradlew run # run the application
./gradlew test # run the tests only
./gradlew clean # delete build outputsNote
./gradlew on macOS/Linux, gradlew.bat on Windows) rather than a globally installed gradle. It pins the version, so builds are reproducible on every machine and in CI.The Gradle Kotlin DSL
Gradle scripts can be written in Groovy or Kotlin; for Kotlin projects prefer the Kotlin DSL (build.gradle.kts). You get the same language you already know, plus IDE autocompletion and type-checking on your build config:
plugins {
kotlin("jvm") version "2.0.0" // compile Kotlin for the JVM
application // adds the 'run' task
}
repositories {
mavenCentral() // where dependencies are fetched from
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
testImplementation(kotlin("test"))
}
application {
mainClass.set("com.example.MainKt") // entry point
}Dependencies & repositories
A dependency is an external library named by group:name:version. A repository (usually mavenCentral()) is where Gradle downloads them. Theconfiguration you declare it under controls its visibility and scope:
| Configuration | Meaning |
|---|---|
implementation | used internally; not exposed to consumers of your module |
api | used internally and re-exposed to modules that depend on you |
testImplementation | available only when compiling and running tests |
runtimeOnly | needed at runtime but not at compile time (e.g. a JDBC driver) |
Tip
implementation over api by default — it keeps your module's internals private and speeds up builds, because changing an implementation dependency doesn't force everything downstream to recompile.Multi-module projects
As an app grows, you split it into modules — say a :core library and an :appthat uses it. Modules build in parallel, enforce clean boundaries, and keep compilation fast. You declare them in settings.gradle.kts:
// settings.gradle.kts
rootProject.name = "my-app"
include(":core", ":app") // two subprojects that build togetherUseful libraries
The Kotlin ecosystem is rich. A few you'll meet again and again:
| Library | What it's for |
|---|---|
kotlinx.coroutines | coroutines, flows, and channels (Phase 6) |
kotlinx.serialization | type-safe JSON/Protobuf serialization |
Ktor | asynchronous HTTP client and server framework |
Exposed | Kotlin SQL framework / lightweight ORM |
| Retrofit / OkHttp | the standard networking stack on Android |
Key idea
Recap & quick check
Key takeaways
- Gradle automates builds: resolving dependencies, compiling, testing, and packaging.
- Use the ./gradlew wrapper for reproducible builds pinned to the right Gradle version.
- Write build scripts in the Kotlin DSL (build.gradle.kts) for autocompletion and type safety.
- Declare dependencies as group:name:version under a configuration; prefer implementation over api.
- Split large apps into modules via settings.gradle.kts, and lean on ecosystem libraries.
Quick check
1. What is Gradle's job in a Kotlin project?
2. Why use ./gradlew instead of a global gradle?
3. How is a dependency identified?
4. When should you prefer 'implementation' over 'api'?
5. Where do you declare a project's subprojects?
You can now build and manage real projects. Next comes Kotlin's biggest arena — Android development — where you'll see everything you've learned power a real app UI.