Keeping track of dependencies and their versions across a project can quickly become a nightmare, especially as your project grows. Manually updating versions in every module is tedious and error-prone. Enter Gradle Version Catalogs, your new best friend for managing dependencies!

Version catalogs provide a centralized, type-safe way to define and access dependency versions and libraries in your Gradle projects. This means less code duplication, improved maintainability, and fewer headaches.

Here’s how to use version catalogs with Groovy Gradle and Kotlin Gradle:

1. Define Your libs.versions.toml

Create a file named libs.versions.toml in the gradle directory of your project. This is where you’ll define your versions and libraries:

[versions]
kotlin = "1.8.20"
androidx-core = "1.10.1"
retrofit = "2.9.0"
okhttp = "4.11.0"
my-app-version = "1.2.3" // Project version

[libraries]
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }

[bundles]
network = ["retrofit", "okhttp"] 
  • [versions]: Declare your dependency versions and your project version here.
  • [libraries]: Define your dependencies, referencing the versions you declared.
  • [bundles]: Group related libraries together (optional, but helpful).

2. Accessing in Your Build Files

Now, let’s see how to use these versions in your different Gradle setups:

a) Groovy Gradle (build.gradle)

android {
    defaultConfig {
        versionName libs.versions.myAppVersion.get()
        versionCode 123 // You'll usually calculate this based on versionName
        //... other configurations
    }
}

dependencies {
    implementation libs.androidx.core
    implementation libs.network // Using a bundle
}

b) Kotlin Gradle (build.gradle.kts)

android {
    defaultConfig {
        versionName = libs.versions.myAppVersion.get()
        versionCode = 123 // You'll usually calculate this based on versionName
        //... other configurations
    }
}

dependencies {
    implementation(libs.androidx.core)
    implementation(libs.network) 
}

Important Notes on Versioning:

  • versionName: This is the user-facing version string (e.g., “1.2.3”).
  • versionCode: This is an integer used internally by app stores to determine update order. It must increase with each release. You’ll often have a scheme to calculate versionCode from versionName components.

Benefits of Version Catalogs

  • Centralized Management: Single source of truth for all your dependency versions and your project version.
  • Type Safety: Reduces errors caused by typos or incorrect version names (especially helpful in Kotlin).
  • Improved Readability: Makes your build files cleaner and easier to understand.
  • Maintainability: Updating versions becomes much simpler – just change the value in the libs.versions.toml file.

Go Forth and Catalog!

Version catalogs are a powerful tool for managing dependencies and project versions in your Gradle projects. Embrace them and bring order to the chaos of versioning!


Discover more from GhostProgrammer - Jeff Miller

Subscribe to get the latest posts sent to your email.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.