Blog Infos
Author
Published
Topics
Published
Topics

https://www.pexels.com/photo/a-woman-sitting-at-a-desk-in-a-library-6549635/

 

In complex and modular Android projects, managing dependencies can be a daunting and time-consuming task. Gradle Version Catalogs, introduced in Gradle 7.0 and promoted to stable in version 7.4, offer an elegant solution to streamline dependency management. In this comprehensive guide, we will explore the benefits of Version Catalogs, demonstrate how to implement them in an Android project with code snippets and provide tips to help you get the most out of this powerful feature.

Why Use Version Catalogs?
  1. Centralized and shareable configuration: By consolidating dependency coordinates in a single file, Version Catalogs simplify management and promote consistent configuration across multiple projects.
  2. Improved performance: Compared to buildSrc, Version Catalogs offer better performance, as updating dependency versions no longer requires a complete rebuild.
  3. Flexibility: Version Catalogs support the creation of dependency bundles, enabling developers to add a single implementation line for a set of libraries in their Gradle files.
  4. Version Catalogs support third-party plugins for automatic version updates and offer better performance compared to buildSrc solutions.Type-safe dependencies: Version Catalogs encourage type-safe dependency declarations, reducing typos and improving IDE support for content assistance.
Implementing Version Catalogs in an Android Project:

Follow these straightforward steps to integrate Version Catalogs into your Android project:

  1. Ensure your project uses Gradle 7.4 or newer. Check or upgrade by executing the appropriate Gradle Wrapper commands.
  2. Create a libs.versions.toml file in your root gradle folder to store your dependency definitions, including versions, libraries, bundles, and plugins.
  3. Here’s a snippet with a few examples :
[versions]
compileSdk = "33"
minSdk = "24"
targetSdk = "33"

voyager = "1.0.0-rc04"
material-icon-version = "1.3.0"
[libraries]

voyager-navigator = { module = "cafe.adriel.voyager:voyager-navigator", version.ref = "voyager" }
voyager-bottomSheetNavigator = { module = "cafe.adriel.voyager:voyager-bottom-sheet-navigator", version.ref = "voyager" }
voyager-tabNavigator = { module = "cafe.adriel.voyager:voyager-tab-navigator", version.ref = "voyager" }
voyager-transitions = { module = "cafe.adriel.voyager:voyager-transitions", version.ref = "voyager" }
material-icon-extended = {module = "org.jetbrains.compose.material:material-icons-extended", version.ref = "material-icon-version"}

4. Configure your settings.gradle file to enable Version Catalogs

enableFeaturePreview("VERSION_CATALOGS")
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

5. Now sync gradle files and rebuild your project.

6. Update your application build.gradle file to define your plugins and implementations using the Version Catalog:

plugins {
  id 'com.android.application'
  id 'kotlin-android'
}

android {
  // Your Android configuration here
}

dependencies {
  implementation(libs.voyager.navigator)
  implementation(libs.voyager.transitions)
  implementation(libs.voyager.bottomSheetNavigator)
  api(compose.material)
}

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Becoming and Staying a Productive Developer with Build Scans, Build Validation Scripts and Gradle

We will start by discussing some of the top Gradle issues that affect the productivity of Android developers. We will cover build cache misses and configuration cache issues as well as how to debug and…
Watch Video

Becoming and Staying a Productive Developer with Build Scans, Build Validation Scripts and Gradle

Etienne Studer & Nelson Osacky
SVP of Engineering & Lead Solutions Engineer
Gradle

Becoming and Staying a Productive Developer with Build Scans, Build Validation Scripts and Gradle

Etienne Studer & N ...
SVP of Engineering & ...
Gradle

Becoming and Staying a Productive Developer with Build Scans, Build Validation Scripts and Gradle

Etienne Studer & ...
SVP of Engineering & Lead ...
Gradle

Jobs

7. You are done.

Important Points to Know
  • Version Catalogs support automatic version updates using tools such as RenovateBot for GitHub projects. This bot reads your libs.versions.toml file and creates Pull Requests to update your dependencies.
  • Version Catalogs do not currently support precompiled script plugins. Dependencies declared in libs.versions.toml will be visible in build.gradle files of all your modules, but not in custom/precompiled ones. You can work around this limitation by creating an extension function to manually expose the dependency.
Conclusion

Version Catalogs offer an improved way to manage dependencies in Android projects. They simplify maintenance, leverage Gradle’s flexibility, and are increasingly being adopted by the community. By following the steps outlined in this article, you can easily integrate Version Catalogs into your Android projects and enjoy the benefits of better dependency management.

Further Resources

I am on Twitter and Linkedin, let’s connect.

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Managing dependencies in a single module project is pretty simple, but when you start…
READ MORE
blog

Running Instrumented Tests in a Gradle task

During the latest Google I/O, a lot of great new technologies were shown. The…
READ MORE
blog
Many of us have faced Groovy difficulties and used to convert it to Kotlin…
READ MORE
blog
This is the accompanying blog post for my recent Droidcon Berlin 2023 talk “How…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu