Blog Infos
Author
Published
Topics
Published
Topics

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.
[versions]
gradleplugin = "7.1.3"
kotlin = "1.6.10"
room = "2.4.2"
compose = "1.1.1"
[libraries]
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-paging = { module = "androidx.room:room-paging", version.ref = "room" }
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
android-test-junit4 = { module = "androidx.test.ext:junit:1.1.3", version = "1.1.3" }
gson = "com.google.code.gson:gson:2.9.0"
[bundles]
room = ["room-ktx", "room-paging", "room-runtime"]
[plugins]
android-application = { id = "com.android.application", version.ref = "gradleplugin" }
android-library = { id = "com.android.library", version.ref = "gradleplugin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }

ℹ️ 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

buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
}
}
view raw build.gradle hosted with ❤ by GitHub

for the plugins block.

plugins {
alias libs.plugins.android.application apply false
alias libs.plugins.android.library apply false
alias libs.plugins.kotlin.android apply false
alias libs.plugins.kotlin.kapt apply false
}
view raw build.gradle hosted with ❤ by GitHub

⚠️ 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

plugins {
alias libs.plugins.android.application
alias libs.plugins.kotlin.android
alias libs.plugins.hilt.plugin
alias libs.plugins.kotlin.kapt
}
// ....
implementation libs.bundles.room
kapt libs.room.compiler
implementation libs.gson
view raw build.gradle hosted with ❤ by GitHub

Job Offers

Job Offers


    Senior C++ Developer for Android Automotive (m/w/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Senior Android Developer (Remote)

    Komoot
    Europe
    • Full Time
    apply now

    Talent Acquisition Manager – Technology

    FanDuel
    New York, NY; Atlanta, GA
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

,

Writing backwards-compatible Gradle plugins

Gradle plugins are powerful – you can integrate custom logic into the build process to make it fit your needs. In the Android world, most Gradle plugins will have to interact with the Android Gradle…
Watch Video

Writing backwards-compatible Gradle plugins

SIMON SCHILLER
Software Engineer
Spotify

Writing backwards-compatible Gradle plugins

SIMON SCHILLER
Software Engineer
Spotify

Writing backwards-compatible Gradle plugins

SIMON SCHILLER
Software Engineer
Spotify

Jobs

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

android {
composeOptions {
kotlinCompilerExtensionVersion libs.versions.compose.get()
}
}
view raw build.gradle hosted with ❤ by GitHub
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:

 

This article was originally published on proandroiddev.com on May 07, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

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
Mobile device concept is one step ahead of the trend in technology, bringing an…
READ MORE
blog
According to the Gradle documentation dependency resolution is a process that consists of two…
READ MORE
blog
When developing a modularized Android project it is expected to have many modules and…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu