ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Gradle version catalogs for an awesome dependency management

El Ateneo Grand Splendid Bookshop
El Ateneo Grand Splendid

Managing dependencies in a single module project is pretty simple, but when you start scaling and adding modules you have different ways to handle this:

  • Manual management: Involves having in many places the same string containing the libraries group, artifact and version.
    When a library needs to be updated, we need to change it manually in every Gradle file, this is very error-prone.
  • ext blocks: This is the recommended solution by Google in their documentation and solves the problems of manual management. The only downside is that the IDE doesn’t offer code completion.
  • buildSrc: In this approach, you use a special Gradle module and define your dependencies using Kotlin code. The major benefit of this option is auto-completion & navigation while the downside is that on any change to a version, the entire module needs to be recompiled.

In order to provide a standard for managing this, the Gradle team introduced version catalogs as an experimental feature in version 7.0 and promoted it to stable in version 7.4 🎉. This feature enables sharing dependencies in a centralized way, using an easy syntax and without compromising the build speed.

You can create the version catalogs manually in settings.gradle but the easiest way is to use a TOML file which will be really easy to understand.

Implementing Version Catalogs:

1️. Make sure you are using Gradle 7.4 or newer

This is easily checked using:

./gradlew --version 

In case you need to upgrade, this task can be done by executing:

./gradlew wrapper --gradle-version=7.4.2

2. Create the TOML file

Let’s create the gradle/libs.versions.tomlfile that Gradle will use by default to create the libs catalog. This file can contain four sections:

  • [versions] is used to declare the version numbers that will be referenced later by plugins and libraries.
  • [libraries] Define the libraries that will be later accessed in our Gradle files.
  • [bundles] Are used to define a set of dependencies.
  • [plugins] Were added in Gradle 7.2, are used to define plugins.

ℹ️ In the [libraries]you will find different ways of expressing a library or a plugin in the TOML file.

ℹ️ You can use separators such as -, _, . that will be normalized by Gradle to . in the catalog and allow you to create subsections.

ℹ️ Variables defined are CamelCase.

3. Configure your root build.gradle file:

Replace the old dependencies block

for the plugins block.

⚠️ You should include here all the plugins you are going to use in all your modules with the apply false suffix. The exception to this rule is the one that you only use in this root build.gradle file.

4. Now it's the turn of your application build.gradle file

Define your plugins at the top of your build.gradle file and the implementation

⚠️ If you are using Jetpack Compose, you can configure it in this way:

IDE Support

Android Studio has this support for TOML files:

👍 TOML file is checked for correctness when you compile your project.

👎 Gradle file auto-completion

👎 Dependency update suggestion

Dependencies update plugin:

The community comes to the rescue until the IDE catches up 🎉

This plugin created by Hugo Visser allows you to update all your dependencies running a Gradle task, is easy to configure and you can also pin some libraries to avoid them being updated.

Do you want to learn more? Read the official documentation or check this GitHub repository:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Matias Calvo

I do Android at Travelperk. @matu_calvo

Responses (5)