iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Gradle

Gradle is the build tool for Kotlin/Android. Kotlin DSL (build.gradle.kts) gives you typed config, IDE completion, and refactoring across files. Pair with a version catalog (libs.versions.toml) for centralised dependency versions and convention plugins for shared build logic.

Kotlin DSL, version catalog, convention plugin

EXAMPLE
// settings.gradle.kts
pluginManagement {
    repositories { gradlePluginPortal(); google(); mavenCentral() }
}
dependencyResolutionManagement {
    repositories { google(); mavenCentral() }
}
rootProject.name = "shop"
include(":app", ":core", ":data")

// gradle/libs.versions.toml — single source of truth for versions
[versions]
kotlin = "2.0.0"
agp = "8.4.0"
coroutines = "1.8.0"
compose = "2024.06.00"
serialization = "1.6.3"

[libraries]
kotlin-stdlib            = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
coroutines-core          = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core",     version.ref = "coroutines" }
coroutines-android       = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android",  version.ref = "coroutines" }
serialization-json       = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" }
compose-bom              = { module = "androidx.compose:compose-bom", version.ref = "compose" }
compose-material3        = { module = "androidx.compose.material3:material3" }
junit                    = { module = "junit:junit", version = "4.13.2" }

[plugins]
kotlin-jvm     = { id = "org.jetbrains.kotlin.jvm",          version.ref = "kotlin" }
kotlin-android = { id = "org.jetbrains.kotlin.android",      version.ref = "kotlin" }
android-app    = { id = "com.android.application",            version.ref = "agp" }
serialization  = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

// app/build.gradle.kts
plugins {
    alias(libs.plugins.android.app)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.serialization)
}

android {
    namespace = "au.com.example.shop"
    compileSdk = 34

    defaultConfig {
        applicationId = "au.com.example.shop"
        minSdk = 24
        targetSdk = 34
        versionCode = 142
        versionName = "1.4.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions { jvmTarget = "17" }

    buildFeatures { compose = true }
}

dependencies {
    implementation(libs.kotlin.stdlib)
    implementation(libs.coroutines.android)
    implementation(libs.serialization.json)

    implementation(platform(libs.compose.bom))
    implementation(libs.compose.material3)

    testImplementation(libs.junit)
}

// gradle.properties — common knobs
// org.gradle.jvmargs=-Xmx4096m -XX:+UseG1GC
// org.gradle.parallel=true
// org.gradle.caching=true
// android.useAndroidX=true
// kotlin.code.style=official

// ===== Convention plugin =====
// Stop duplicating android {} / kotlinOptions across modules.
// build-logic/src/main/kotlin/shop.android.library.gradle.kts
//
// plugins {
//   id("com.android.library")
//   id("org.jetbrains.kotlin.android")
// }
// android {
//   compileSdk = 34
//   defaultConfig { minSdk = 24 }
//   compileOptions { sourceCompatibility = JavaVersion.VERSION_17; targetCompatibility = JavaVersion.VERSION_17 }
//   kotlinOptions { jvmTarget = "17" }
// }
//
// core/build.gradle.kts
// plugins { id("shop.android.library") }
// dependencies { implementation(libs.kotlin.stdlib) }

// ===== Useful tasks =====
// ./gradlew tasks
// ./gradlew :app:assembleDebug
// ./gradlew :app:bundleRelease       # signed AAB for Play
// ./gradlew :app:test :app:lint
// ./gradlew dependencies --configuration releaseRuntimeClasspath

// ===== Performance =====
// - Enable build cache and parallel (gradle.properties above)
// - Use the configuration cache: 'org.gradle.configuration-cache=true'
// - Run './gradlew --profile assembleDebug' to find slow tasks
// - Limit dependency upgrades; the version catalog is the right place

Why it matters

A version catalog (libs.versions.toml) + Kotlin DSL build files + convention plugins is the trio that stops a multi-module project from drifting. Versions live in one file, build logic lives in one plugin, and adding a new module is "apply the convention plugin" instead of copy-pasting 80 lines of android {} config.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# build.gradle.kts
plugins { kotlin("jvm") version "1.9.22" }
dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}
# CLI
./gradlew build
./gradlew run
Try it Yourself »

Discussion

Loading…