Convention plugins and version catalogs

Learn Gradle convention plugins in an included build-logic build and the libs.versions.toml version catalog to keep Android multi-module builds consistent. About 25 minutes.

Why it matters

  • In a 40-module Android app, repeating compileSdk, minSdk, the JVM target and dependency versions in every build.gradle.kts guarantees drift. Convention plugins put that setup in one place.
  • Gradle's own best-practices guide recommends putting build logic in an included build (usually build-logic) rather than buildSrc, and Google's Now in Android sample does exactly that.
  • The libs.versions.toml version catalog gives one source of truth for versions, libraries, bundles and plugin IDs. Interviewers ask how you stop one module using one version of androidx.core:core-ktx while another uses a different one.
  • Knowing how convention plugins read the catalog, when to use a BOM, and how this ties into api vs implementation and explicit API mode connects to the API-design and build-performance lessons in this module.

The simple explanation

  • A convention plugin is a pre-filled form: every new module gets the same fields (SDK levels, JVM target, lint rules) without re-typing them.
  • The version catalog is the office supply list: one sheet names the exact Kotlin, AGP and library versions, and every team orders from it.
  • A BOM is a matched set of screws: you pick the set, not individual screws, so nothing mismatches.

What is a Gradle convention plugin? It is your own Gradle plugin that applies other plugins and shared configuration, so a module's build file shrinks to a line like alias(libs.plugins.myapp.android.library). You can write it as a precompiled script plugin (a .gradle.kts file whose name becomes the plugin ID) or as a binary plugin (a Kotlin class implementing Plugin<Project>, registered with gradlePlugin { }). Now in Android uses binary plugins.

Where it lives matters. buildSrc is built automatically, but Gradle's docs warn that any change in buildSrc makes the entire build out of date. An included build declared with includeBuild("build-logic") inside pluginManagement { } in settings.gradle.kts is a normal Gradle build: you can split it into subprojects and test it, and a change in one of its subprojects only makes the projects that use that subproject out of date. Gradle recommends it for convention plugins.

The version catalog (gradle/libs.versions.toml) has four sections:

SectionPurpose
[versions]Named version strings, referenced by version.ref
[libraries]Alias to group:name coordinates plus a version or version.ref
[bundles]A named list of library aliases added together
[plugins]Plugin ID plus version, used with alias(...) in plugins { }

Gradle generates type-safe accessors such as libs.androidx.core.ktx (dashes become dots) for build scripts in the main build. Inside a convention plugin's code those accessors are not available, so you read the catalog through VersionCatalogsExtension: extensions.getByType<VersionCatalogsExtension>().named("libs"), then findLibrary, findBundle, findVersion or findPlugin.

A BOM (bill of materials) is a platform dependency that supplies versions for a group of artifacts. add("implementation", platform(libs.findLibrary("androidx-compose-bom").get())) aligns every Compose artifact to the BOM's versions, so individual Compose libraries can be declared without versions.

Notes

  • Put convention plugins in an included build (build-logic) and add includeBuild("build-logic") inside pluginManagement { } in settings.gradle.kts.
  • Gradle's docs: any change in buildSrc makes the whole build out of date. With an included build, only projects that use the changed build-logic subproject are affected.
  • A precompiled script plugin needs the kotlin-dsl plugin in its build; a binary plugin is registered with gradlePlugin { plugins { register(...) } }.
  • The version catalog lives at gradle/libs.versions.toml and has [versions], [libraries], [bundles] and [plugins] sections.
  • The included build does not see the main build's catalog automatically. Now in Android adds versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } } to build-logic/settings.gradle.kts.
  • Inside plugin code, read the catalog with extensions.getByType<VersionCatalogsExtension>().named("libs") and findLibrary("alias"), which returns an Optional.
  • AGP and the Kotlin Gradle plugin go in build-logic as compileOnly dependencies, so the plugin compiles against them without fixing their versions for the main build.
  • A BOM is added with platform(...). It aligns the versions of a group of artifacts, such as Compose with androidx.compose:compose-bom.
  • A [plugins] entry with no version (myapp-android-library = { id = "myapp.android.library" }) is a common way to apply your own convention plugins with alias(...).
  • Keep convention plugins small and composable (library, application, Compose, Hilt, feature) instead of one plugin that configures everything.

Code example

The version catalog, the included build's setup, one binary convention plugin, and a module that uses it.

# gradle/libs.versions.toml
[versions]
androidGradlePlugin = "8.7.3"
kotlin = "2.1.0"
coreKtx = "1.15.0"
 
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
 
[plugins]
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
myapp-android-library = { id = "myapp.android.library" }
// build-logic/settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
    versionCatalogs {
        create("libs") { from(files("../gradle/libs.versions.toml")) }
    }
}
rootProject.name = "build-logic"
include(":convention")
 
// build-logic/convention/build.gradle.kts
plugins {
    `kotlin-dsl`
}
 
dependencies {
    compileOnly(libs.android.gradlePlugin)
    compileOnly(libs.kotlin.gradlePlugin)
}
 
gradlePlugin {
    plugins {
        register("androidLibrary") {
            id = "myapp.android.library"
            implementationClass = "AndroidLibraryConventionPlugin"
        }
    }
}
// build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
 
class AndroidLibraryConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            pluginManager.apply("com.android.library")
            pluginManager.apply("org.jetbrains.kotlin.android")
 
            val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
 
            extensions.configure<LibraryExtension> {
                compileSdk = 35
                defaultConfig {
                    minSdk = 24
                }
                compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_17
                    targetCompatibility = JavaVersion.VERSION_17
                }
            }
 
            extensions.configure<KotlinAndroidProjectExtension> {
                compilerOptions {
                    jvmTarget.set(JvmTarget.JVM_17)
                }
            }
 
            dependencies {
                add("implementation", libs.findLibrary("androidx-core-ktx").get())
            }
        }
    }
}
// settings.gradle.kts (root)
pluginManagement {
    includeBuild("build-logic")
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
 
// core/data/build.gradle.kts
plugins {
    alias(libs.plugins.myapp.android.library)
}
 
android {
    namespace = "com.example.core.data"
}
  • The included build gets its own libs catalog from the same TOML file, so build-logic/convention/build.gradle.kts can use type-safe accessors like libs.android.gradlePlugin.
  • Inside the plugin class there are no generated accessors, so it reads the catalog through VersionCatalogsExtension and findLibrary.
  • AGP and the Kotlin Gradle plugin are compileOnly: the root build still decides their versions. The root build.gradle.kts usually declares them with alias(libs.plugins.android.library) apply false and alias(libs.plugins.kotlin.android) apply false.
  • The module's build file only keeps what is unique to it, here the namespace.
  • The version numbers are examples; keep AGP, Kotlin and Gradle on combinations listed as compatible in their release notes. With AGP 9's built-in Kotlin support you no longer apply org.jetbrains.kotlin.android yourself; check the AGP release notes when upgrading.

In production

Every buildSrc edit rebuilt the whole project
  • A large app kept its shared build logic in buildSrc. Each small change to a helper, even one used by a single module, made the whole build out of date.
  • Developers editing build logic paid a full rebuild every time, and CI caches were invalidated for unrelated modules.
The fix
  • Moved the logic into an included build-logic build with small binary convention plugins (library, application, Compose, Hilt), splitting rarely used logic into its own build-logic subproject so editing it does not touch every module.
  • Applied them with alias(libs.plugins...) entries in the version catalog and deleted buildSrc.
  • Compared Build Scans before and after a build-logic change to confirm that only the affected modules re-ran tasks.
Unresolved R references after enabling non-transitive R classes
  • After turning on non-transitive R classes (the AGP 8 default), many modules failed to compile with Unresolved reference errors on R.string and R.drawable values.
  • Those modules had been using resources defined in other modules through their own R class, which only worked because R classes used to include every transitive resource.
The fix
  • Ran Android Studio's Refactor > Migrate to Non-Transitive R Classes, which rewrites references to use the R class of the module that owns each resource.
  • Added an explicit dependency on the owning module (for example :core:ui) where a module had only been reaching the resources transitively.
  • Moved shared strings and icons into the design-system module so their owner was obvious.

Common mistakes

  • Keeping convention plugins in buildSrc in a large build. Any buildSrc change makes the whole build out of date. Use an included build-logic build, as Gradle recommends.
  • One giant convention plugin. A plugin that applies Compose, Hilt and Room to every module adds work to modules that need none of it. Write small plugins and compose them.
  • Hardcoding versions in module build files. Each hardcoded version is a drift point. Reference the catalog so one edit in libs.versions.toml updates every module.
  • Expecting libs. accessors inside plugin code. They are generated for build scripts, not for classes in build-logic. Use VersionCatalogsExtension and findLibrary.
  • Adding Compose artifacts without the BOM. Without platform(compose-bom), each Compose artifact needs its own version and they can drift apart. Add the BOM and declare the artifacts without versions.
  • Adding AGP as implementation in build-logic. Prefer compileOnly, so the root build's plugin versions decide which AGP actually runs.

Interview questions

Answer out loud first. Then reveal the answer and compare it at each depth.

“Why should a large Android project use build-logic instead of buildSrc for convention plugins?”

“How does the version catalog work, and what are its four sections?”

“What goes into a convention plugin, and what stays in the module's build file?”

“How do you keep transitive dependency versions aligned across 50 modules?”

Quiz

Where does Gradle recommend putting convention plugins in a large build?

How does code inside a binary convention plugin read the version catalog?

What does adding a BOM with platform() do?

What is the effect of non-transitive R classes, the default since AGP 8.0?

0 of 4 answered

Mini task

15-minute exercise: add a convention plugin and version catalog

  1. Create the included build. Create build-logic/settings.gradle.kts (with the versionCatalogs block from this lesson and include(":convention")) and build-logic/convention/build.gradle.kts (with kotlin-dsl, compileOnly AGP and Kotlin Gradle plugin, and a gradlePlugin registration).
  2. Include it. In the root settings.gradle.kts, add includeBuild("build-logic") inside pluginManagement { }.
  3. Write the plugin. Add AndroidLibraryConventionPlugin.kt that applies the Android library plugin, sets compileSdk, minSdk and the Java/Kotlin targets, and adds one library from the catalog with findLibrary.
  4. Update the catalog. In gradle/libs.versions.toml, add [libraries] entries for the Android and Kotlin Gradle plugins and a [plugins] entry for your convention plugin ID.
  5. Apply it. In one library module, replace the plugins { } block with alias(libs.plugins.myapp.android.library) and delete the settings the plugin now provides.
  6. Verify. Run ./gradlew :your-module:assembleDebug, then ./gradlew :your-module:dependencies --configuration debugCompileClasspath to confirm the catalog library is on the classpath.

Done condition: the module builds with the convention plugin, its build.gradle.kts has no SDK levels or version strings, and the plugin reads the catalog through VersionCatalogsExtension.

Next: API design between modules