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


    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, , ,

Ultimate Iteration Speeds with Gradle Configuration Cache

A dive into what is Gradle Configuration Cache and how it works, why you want to have it enabled, and how to debug unexpected configuration cache misses for local and CI workflows.
Watch Video

Ultimate Iteration Speeds with Gradle Configuration Cache

Aurimas Liutikas
Software Engineer
Google / Gradle Fellow

Ultimate Iteration Speeds with Gradle Configuration Cache

Aurimas Liutikas
Software Engineer
Google / Gradle Fell ...

Ultimate Iteration Speeds with Gradle Configuration Cache

Aurimas Liutikas
Software Engineer
Google / Gradle Fellow

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
Recently, I needed to upgrade a dependency to a beta version (androidx.navigation:navigation-compose, version 2.8.0-beta02…
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