Phase 7 · Professional & AppliedModule 37~34 min read

Build Tools & the Ecosystem

Build and manage projects with Gradle and the Kotlin DSL.

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.kts Kotlin 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:

terminal
./gradlew build     # compile, test, and assemble everything
./gradlew run       # run the application
./gradlew test      # run the tests only
./gradlew clean     # delete build outputs

Note

Always use the wrapper (./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:

build.gradle.kts
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:

ConfigurationMeaning
implementationused internally; not exposed to consumers of your module
apiused internally and re-exposed to modules that depend on you
testImplementationavailable only when compiling and running tests
runtimeOnlyneeded at runtime but not at compile time (e.g. a JDBC driver)

Tip

Prefer 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
// settings.gradle.kts
rootProject.name = "my-app"
include(":core", ":app")   // two subprojects that build together

Useful libraries

The Kotlin ecosystem is rich. A few you'll meet again and again:

LibraryWhat it's for
kotlinx.coroutinescoroutines, flows, and channels (Phase 6)
kotlinx.serializationtype-safe JSON/Protobuf serialization
Ktorasynchronous HTTP client and server framework
ExposedKotlin SQL framework / lightweight ORM
Retrofit / OkHttpthe standard networking stack on Android

Key idea

You don't reinvent wheels — you assemble libraries. Learning to read a library's docs, add it as a dependency, and wire it in is one of the most valuable everyday skills in professional development.

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.