Blog Infos
Author
Published
Topics
Published
Topics

Photo by Brett Jordan on Unsplash

 

Gradle is a build automation tool used for building, testing, and deploying software. Gradle version catalogs are a tool that helps developers manage the versions of their dependencies in a single place. In this blog, we will explore what Gradle version catalogs are, why they are important, and how to use them in Android development with practical examples.

What are Gradle version catalogs?

Gradle version catalogs are a feature that was introduced in Gradle 7.0. A Gradle version catalog is a file that contains a list of versions for a specific dependency. The version catalog file is separate from your build.gradle file, and it contains only the version information. By using a version catalog, you can easily manage the versions of your dependencies in one place.

Why are Gradle version catalogs important?

Gradle version catalogs are important because they allow you to manage the versions of your dependencies in a centralized location. This makes it easier to manage dependencies across multiple projects, as you can update the version in one place and have it reflect across all your projects. Additionally, version catalogs make it easier to ensure that all developers on a project are using the same version of a particular dependency.

How to use Gradle version catalogs in Android development

Using Gradle version catalogs in Android development is straightforward. Here’s how to get started:

Step 1: Create a version catalog file

The first step is to create a version catalog file. You can create this file in any directory in your project, and you can name it anything you like. For example, you might create a file named “versions.properties” in the root directory of your project.

The contents of the version catalog file should look something like this:

# versions.properties

junit.jupiter.api=5.8.0-M1
junit.platform.commons=1.8.0-M1
junit.vintage.engine=5.8.0-M1
Step 2: Configure your build.gradle file

Next, you need to configure your build.gradle file to use the version catalog. Here’s an example of how to do this:

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-api:${versions.junit.jupiter.api}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    testImplementation("org.junit.platform:junit-platform-commons:${versions.junit.platform.commons}")
    testRuntimeOnly("org.junit.vintage:junit-vintage-engine:${versions.junit.vintage.engine}")
}

repositories {
    mavenCentral()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}

dependencies {
    //...

    implementation(enforcedPlatform("com.google.android.gms:play-services-bom:${versions.playServices}"))
}

dependencies {
    //...

    implementation(enforcedPlatform("androidx.core:core-ktx:${versions.androidXCore}"))
}

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
    versionCatalogs {
        create("versions") {
            from("$rootDir/versions.properties")
        }
    }
}

This code block does a few things:

  1. It declares the dependencies that should use the version catalog.
  2. It sets up the repositories that Gradle will use to find the dependencies.
  3. It declares the version catalog and specifies the location of the version catalog file.

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

Step 3: Use the versions in your code

Once you have set up the version catalog, you can use the versions in your code. Here’s an example of how to use the versions in a test class:

@Test
fun `verify that version catalog works`() {
    assertEquals("5.8.0-M1", Versions.junit.jupiter.api)
    assertEquals("1.8.0-M1", Versions.junit.platform.commons)
    assertEquals("5.8.0-M1", Versions.junit.vintage.engine)
}

In this example, we are accessing the versions of the JUnit dependencies using the Versions object. The Versions object is automatically generated by Gradle based on the version catalog file.

Conclusion

Gradle version catalogs are a powerful tool for managing the versions of your dependencies in Android development. By using a version catalog, you can ensure that all developers on a project are using the same version of a particular dependency, and you can easily update the version in one place across all your projects. I hope this blog has helped you understand what Gradle version catalogs are and how to use them in Android development.

Read more:

  1. Gradle documentation on version catalogs: https://docs.gradle.org/current/userguide/platforms.html#sec:version-catalogs
  2. Gradle release notes for version 7.0: https://docs.gradle.org/7.0/release-notes.html#version-catalogs
  3. Gradle Summit 2020 talk on version catalogs: https://www.youtube.com/watch?v=_Xq3cZrjvJM
  4. Gradle blog post on version catalogs: https://blog.gradle.org/gradle-version-catalogs
  5. Gradle community forums discussion on version catalogs: https://discuss.gradle.org/t/gradle-version-catalogs/37555

These resources can provide readers with more information and detailed explanations on Gradle version catalogs, their usage, and their benefits.

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