Migrate Your Android App To Gradle Version Catalogs

Search for a command to run...

No comments yet. Be the first to comment.
I've been a professional software developer in the mobile space for around 15 years now. I started in the Android 2.1 / Eclipse / Ant days and iOS Objective-C days pre-ARC on Xcode 4 (if memory servic

Intro I recently learned about friendPaths as part of the Kotlin Gradle Plugin while browsing an Android Slack group. From the documentation on friendPaths: "Paths to the output directories of the friend modules whose internal declarations should be ...

If you've ever tried to compare the behavior of Row in Google's Jetpack Compose, HStack in Apple's SwiftUi, and something like display: flex in the web world, you'll see some interesting results. First, let's take a look at how HStack renders two lon...

Said another way, how to supercharge your development workflow with a debug drawer. As Android developers, we're constantly looking for ways to streamline our workflow. One often-overlooked tool that

As Jetpack Compose becomes more widely used across Android (and Multiplatform projects!), detecting regressions via tooling becomes more important to shift left and detect regressions earlier in the d

As an Android developer, you know that keeping track of dependencies and their versions can be a challenging task. With each new version, libraries can introduce breaking changes, and it can be difficult to ensure that your project stays up-to-date with the latest updates across all of your application's modules.
One solution to this problem is to use a version catalog. A version catalog is a file that defines the versions of your project's dependencies, making it easier to manage and update them. In this blog post, we'll take a look at how to migrate an Android project to using a version catalog.
The first step in migrating your Android project to use a version catalog is to create the catalog file. The version catalog is a simple TOML (more info about TOML files can be found here) file that lists the dependencies and their respective versions. You can create the file manually by right-clicking on your gradle folder and selecting New -> File and adding libs.versions.toml. It is possible to change the catalog file name, however, this requires changing your build files so it is not recommended at the current time.
In your libs.versions.toml file, add these sections:
[versions]
[libraries]
[plugins]
We will be migrating the following dependency to being defined in the version catalog.
This can be defined in the .toml file by adding a version and a library like so:
[versions]
core-ktx = "1.10.0"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
[plugins]
Finally, you need to update your project's dependencies to use the version catalog. You can do this by replacing the first line with a reference to the version catalog. For example:
implementation 'androidx.core:core-ktx:1.10.0'
can be replaced with:
implementation(libs.androidx.core.ktx)
In the root build.gradle file of your project, numerous plugins are defined. Using a version catalog also allows you to define these plugin versions as well.
First, you will add the plugin versions and definitions to the .toml file.
[versions]
core-ktx = "1.10.0"
# Plugin versions
androidGradlePlugin = "7.4.2"
kotlin = "1.8.0"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Then, you can change your root build.gradle file to use the newly defined plugins like so:
plugins {
alias libs.plugins.android.application apply false
alias libs.plugins.android.library apply false
alias libs.plugins.kotlin.android apply false
}
By using a version catalog, you can easily manage your project's dependencies across modules and ensure that you are using the latest versions. You can update the versions in the catalog file, and have a single source of truth for versions of dependencies in your application.
In conclusion, migrating an Android project to use a version catalog is a simple process that can save you a lot of time and effort in managing your dependencies. By following the steps outlined in this blog post, you can easily migrate your project to use a version catalog and enjoy the benefits of easier dependency management.