Android Studio Gradle Version Catalogs

ibrahimertanylmz
Huawei Developers
Published in
4 min readMay 7, 2024
Android Cover

Introduction

Hi everyone! đź‘‹ This article will help you to understand implement dependencies for Android Studio Iguana and latest gradle versions as version catalogs. There will be examples to implementing a dependency and adding a plugin to your project and help you to understand version catalogs and usage of libs.versions.toml file.

Gradle Version Catalogs with Examples

Gradle version catalogs enable you to add and maintain dependencies and plugins in a scalable way. Using Gradle version catalogs makes managing dependencies and plugins easier when you have multiple modules. The updates of dependencies are easier with the usage of versions, libraries and plugins sections and with creating a central version catalog of dependencies, there can be type-safe references with multimodules.

Let’s see the examples for adding plugins and dependencies with version catalogs.

Adding Kapt as Plugin with Version Catalogs

First create a project and open your libs.versions.toml generated by Android Studio. There are three sections as versions, libraries and plugins. Under versions we define the version of the libraries and plugins and for kapt plugin we shall define the Kotlin version.

versions section
[versions]
...
kotlin = "1.9.0"

Then the kapt id and version reference should be set under plugins section. For old usage remember plugin usage as Groovy below:

plugins {
id "org.jetbrains.kotlin.kapt" version "1.9.24"
}

The id should be set the same under plugins section and also version reference should be set as kapt or kotlin. You can define a different version reference for kapt or you can use kotlin version as reference.

So, under plugins section you should add the code below to implement kapt.

[plugins]
...
kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }

We are all set for libs.versions.toml file. We have the kapt id and version and the plugin should be applied as alias in app level and project level build gradle files as below.

Project level build.gradle.kts file:

plugins {
...
alias(libs.plugins.kapt) apply false
}

App level build.gradle.kts file:

plugins {
...
alias(libs.plugins.kapt)
}

That’s all for the adding kapt as Plugin with version catalogs.

Adding Glide as Dependency with Version Catalogs

Adding a library is not much different from adding a plugin. We need a version reference under versions, and we need to add Glide library under libraries section this time.

libraries section

First let’s define the Glide version on our libs.versions.toml file.

[versions]
...
glide = "4.14.2"

Then under libraries section define glide with it’s group, name and version reference this time. Version reference is the glide version we defined under versions tab.

[libraries]
...
glide = { group = "com.github.bumptech.glide", name = "glide", version.ref = "glide" }

If you remember old way adding as dependency you can see until “:” is group and after “:” there is name and version in the implementation line.

dependencies {
implementation 'com.github.bumptech.glide:glide:4.14.2'
}

We are all set for the libs.versions.toml file. Then on app level build.gradle.kts file under dependencies add the library we defined as below.

dependencies {
...
implementation(libs.glide)
}

If you cannot add as libs.glide remember that you might need to sync or build first after configurations on libs.versions.toml file. That’s all for the adding Glide as dependency with version catalogs.

Conclusion

I hope you understand the examples of adding plugins and dependencies with version catalogs. There is one example for each (adding plugin and adding dependency) in this article to help you understand usage of adding dependencies with new Android Studio versions and plugins. Also advantages of Gradle Version Catalog is explained.

I hope, you found this article helpful and if you have any comments or questions, please let me know in the comments below.

--

--

ibrahimertanylmz
Huawei Developers

Android Developer @Huawei 💻, ESOGU Computer Engineering Graduate 🎓, Proactive Self-Starter, Quick Learner, Team Player 👨‍👦‍👦