Automate Dependency Updates With GitHub Action

Emmanuel Iyke
Stackademic
Published in
4 min readNov 15, 2023

--

Modern Android projects usually include a lot of Gradle dependencies, keeping up to date with them however can be a huge tedious task for the developer, that is precious time spent that can be used in completing other tasks

In this article, we are going to talk about how to automatically upgrade our dependency and generate pull Request to our main branch. We will be using the Version Catalog Update Plugin to declare the dependencies and GitHub Actions to build, test, and generate pull Request to our main branch

How Gradle Version Catalog works

Gradle Version Catalog is a powerful tool for dependency management, It provides a centralized way to declare and control the versions of external dependencies used in your project. With the Gradle Version Catalog, you can define a catalog file that lists the versions of your dependencies, plugins, and other artifacts.

Gradle version catalogs works by enabling you to add and maintain dependencies and plugins in a scalable way. So instead of hardcoding dependency names and versions in individual build files we define them in the catalog and use them in all of your modules.

Benefits of using Gradle Version Catalogs

  • Centralization: With a single source of truth for dependency versions, it becomes easier to ensure consistency across projects.
  • Simplified Updates: When a new version of a library is released, you can update it in the catalog, and all projects referencing that catalog will receive the update.
  • Version Conflict Resolution: The catalog helps to avoid version conflicts by enforcing the specified versions throughout your projects.
  • Ease of Collaboration: By sharing the catalog across teams, you foster collaboration and maintain a unified standard across your organization.

How to use Gradle Version Catalogs on Android

Step 1: Create a version catalog file

The first step is to create alibs.versions.toml file in the root of your gradle folder, it's important to name it libs.versions.toml because Gradle looks for the catalog name by default

Step 2: Install the plugins

the next step is to install the plugins, we do that by adding the code below to our build.gradle:

plugins {
id "com.github.ben-manes.versions" version "0.41.0"
id "nl.littlerobots.version-catalog-update" version "<latest version>"
}

Once the plugins are installed, to update the catalog file, you can execute the command ./gradlew versionCatalogUpdate at any time. This command handles the following tasks

Step 3: Implement the dependencies

Define the versions for your dependencies in libs.versions.toml and Specify the dependencies using the declared versions:

[versions]
exoplayer = "2.19.1"
androidx = "1.9.0"
core = "3.5.1"

[libraries]
exoplayer = { module = "com.google.android.exoplayer:exoplayer", version.ref = "exoplayer" }
androidx = { module = "androidx.core:core-ktx", version.ref = "androidx" }
core = { module = "androidx.test.espresso:espresso-core", version.ref = "core" }
  • The Versions declare the versions used in other sections.
  • The library declares the dependencies used in the versions

Now all you need is to implement the dependencies by referencing the library file inside build.gradle

    implementation (libs.rxjava)
implementation(libs.exoplayer)
androidTestImplementation(libs.core)

Step 4: Create a .yml file for your GitHub action

Now to automate the process using Github action we need to create a .yml. Within your project folder, establish the path github/workflows/dependency_update.yml and insert the following code:

name: Update Dependencies

on:
workflow_dispatch:
schedule:
- cron: '00 6 1 * *'

jobs:
update-dependencies:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v1
with:
java-version: 17
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Perform dependencies resolution and update
run: ./gradlew versionCatalogUpdate
- name: Build and Test
run: ./gradlew clean build test
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.GITHUB_TOKEN}}
commit-message: update dependencies
committer: GitHub <noreply@github.com>
author: GitHub <noreply@github.com>
title: Update dependencies
body: |
Dependencies update.
Check the Version Catalog to see which dependencies need an update.
branch: update-dependencies
delete-branch: true
labels: |
dependencies update

This YAML configuration establishes a GitHub Actions workflow, “Update Dependencies,” triggered manually or scheduled for 6:00 AM on the first day monthly. The workflow, within the update-dependencies job, executes key steps like checking out the repository, setting up Java, granting execute permission for gradlew, updating dependencies, and creating a pull request with commit details and a descriptive body. This workflow streamlines dependency management in your project, providing automation for essential tasks. Adjust the YAML code as per your project's needs.

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.

Stay tuned! Happy reading 📚 Happy coding 💻

If you have any queries related to Android, I’m always happy to help you. You can reach me on Twitter and LinkedIn.

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.

--

--