Sanniv Khare 6 min read

Gradle version catalogs: porting an Android app, checked

Gradle version catalogs: porting an Android app, checked

Moving an Android module from hard-coded version strings to gradle/libs.versions.toml should change nothing that ships, and you can prove it: dump releaseRuntimeClasspath before and after and diff the two. We ported a small Compose app on AGP 9.4.0, Kotlin 2.3.21 and Gradle 9.6.0 (JDK 17) on our M1 lab Mac. Both versions built, the 548-line runtime dependency tree and the plugin classpath came out identical, and both debug APKs were 8,533,010 bytes. The whole port is one new TOML file and a search-and-replace in each build.gradle.kts.

Why we did this: Peepal and Shopkeeper started life with versions typed into each module’s build file, and client React Native apps often still do. Every upgrade post we write (AGP, Kotlin, Compose) is easier to follow when the versions live in one file, so this is the first step we take on an older Android project.

app/build.gradle.kts before and after the move to a version catalog

The app module’s build file before and after, shortened to fit. The full files are below.

What a version catalog is, in one paragraph

Gradle imports a catalog automatically when it finds libs.versions.toml in the root project’s gradle folder, and exposes it to every build script as libs. The file has four sections: [versions], [libraries], [bundles] and [plugins]. A catalog only names coordinates; it does not change how dependencies resolve. Gradle’s platforms page makes the same distinction: platforms (BOMs) influence resolution, catalogs give type-safe accessors. That is why a correct port leaves the resolved classpath untouched.

The starting point

The root build.gradle.kts declared the plugins with versions:

plugins {
    id("com.android.application") version "9.4.0" apply false
    id("org.jetbrains.kotlin.plugin.compose") version "2.3.21" apply false
}

And app/build.gradle.kts applied them and listed two libraries:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.plugin.compose")
}

dependencies {
    implementation("androidx.compose.foundation:foundation:1.10.0")
    implementation("androidx.activity:activity-compose:1.10.1")
}

There is no org.jetbrains.kotlin.android plugin: on AGP 9 the project uses the built-in Kotlin support. When we first ran this sample with the old android.builtInKotlin=false and android.newDsl=false flags still in gradle.properties, AGP 9.4.0 warned that both settings are deprecated and will be removed in AGP 10, and the Kotlin plugin warned that org.jetbrains.kotlin.android is no longer required since AGP 9.0. We removed both flags and the plugin before porting. Our AGP 8 to 9.5 guide covers that step.

Step 1: write the catalog

Create gradle/libs.versions.toml next to the wrapper folder:

[versions]
agp = "9.4.0"
kotlin = "2.3.21"
compose-foundation = "1.10.0"
activity-compose = "1.10.1"

[libraries]
androidx-compose-foundation = { group = "androidx.compose.foundation", name = "foundation", version.ref = "compose-foundation" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity-compose" }

[bundles]
compose-ui = ["androidx-compose-foundation", "androidx-activity-compose"]

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

Notes from the docs that save time:

  • Android’s migration guide uses the group and name form shown here. Gradle also accepts module = "group:name" or a single "group:name:version" string.
  • Use dashes in alias names. Gradle turns - and _ into dots, so androidx-activity-compose becomes libs.androidx.activity.compose.
  • Some names are reserved: extensions, class and convention cannot be aliases, and bundles, versions and plugins cannot be the first part of a library alias. versions-dependency is rejected; dependency-versions is fine.
  • Keep the Kotlin compiler plugins on the same kotlin version ref, so they move together on the next upgrade.

Step 2: switch the plugins

Root build.gradle.kts:

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.compose) apply false
}

app/build.gradle.kts:

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.compose)
}

Two limits from Gradle’s page: a plugin’s version in the catalog is used as a required version, and catalog plugins cannot be used in settings.gradle.kts. If you are still on a Gradle older than 8.1, Android’s guide says the plugins {} block needs @Suppress("DSL_SCOPE_VIOLATION") before alias works.

Step 3: switch the libraries

dependencies {
    implementation(libs.bundles.compose.ui)
}

A bundle is optional. If you prefer one line per library, implementation(libs.androidx.compose.foundation) and implementation(libs.androidx.activity.compose) do the same. We use bundles only for groups that always travel together; a bundle that half the modules need ends up pulling libraries into modules that do not use them.

Android’s guide recommends doing this in a fixed order per dependency: add the entry to the catalog, sync, then replace the string with the accessor. On a large module that keeps each change small enough to review.

Step 4: prove nothing changed

This is the part most migration notes skip. Run the same two reports before and after, from the project root:

./gradlew -q :app:dependencies --configuration releaseRuntimeClasspath > before.txt
./gradlew -q buildEnvironment > before-plugins.txt
# ...port...
./gradlew -q :app:dependencies --configuration releaseRuntimeClasspath > after.txt
./gradlew -q buildEnvironment > after-plugins.txt
diff before.txt after.txt && diff before-plugins.txt after-plugins.txt
Terminal output: identical runtime and plugin classpaths before and after, same APK size, and the error for a misspelt accessor

Real output from our M1 lab Mac. Both diffs were empty and assembleDebug succeeded in both projects.

If the diff is not empty, a version in the TOML does not match the string it replaced, or a library was dropped from a bundle. Fix the catalog, not the diff.

What a typo looks like

We misspelt one accessor on purpose (libs.androidx.activity.compse). The build failed in 3 seconds, during script compilation, with:

e: app/build.gradle.kts:25:43: Unresolved reference 'compse'.

That is the main win over strings. A mistyped coordinate string is only caught when Gradle tries to resolve it. A mistyped accessor fails at once, with a line and column. The message does not suggest the right name, so look at the TOML; Android Studio’s code completion on libs. lists the valid ones.

Gotchas on real projects

  • buildSrc and convention plugins. Gradle’s docs say buildSrc does not inherit the catalog automatically. Import it in buildSrc/settings.gradle.kts with from(files("../gradle/libs.versions.toml")) inside a versionCatalogs { create("libs") { ... } } block.
  • BOMs. Declare the BOM as a normal library with a version, then the libraries it governs without a version, and use implementation(platform(libs.your.bom)). The catalog only names them; the BOM still does the resolving.
  • React Native apps. Libraries under node_modules keep their own Gradle files and versions. A catalog in android/gradle/ only covers your app module and any modules you own.
  • Multiple modules. Every module in the build sees the same libs, so port module by module and run the classpath diff for each releaseRuntimeClasspath you ship.

What to check on your own app

  1. Save releaseRuntimeClasspath and buildEnvironment output before you start.
  2. Port plugins first, then libraries, one module at a time.
  3. Diff both reports after each module. Empty diff, then commit.
  4. Search the build files for any : inside a string in dependencies {}: that is a version you missed.

Made by the team behind this post

Apps from our studio

  • Goes with this post

    BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get
  • Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.