How to manage dependencies between Gradle modules?
Tags: gradle, kotlin, android, java
Published: 2022-09-07 15:31:45

---
title: How to manage dependencies between Gradle modules?
published: true
description: Using Gradle version catalog for easier dependencies management in a multimodule project.
tags: #gradle #kotlin #android #java
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wauzftb0iwln4sqkld1b.jpg
---

In a multimodule project, managing dependencies manually can be challenging. For example, if you forget to update a library version after modifying a library version on another module, your project will have a duplicate library.

Starting from [Gradle 7.4.1](https://docs.gradle.org/7.4.1/release-notes.html), version catalog is the recommended way of managing dependencies between Gradle projects (also known as a module).

To use the version catalog, simply add `libs.versions.toml` file inside gradle folder (`yourproject/gradle/libs.versions.toml`)

```
.
├── app
│   ├── build.gradle.kts
│   ├── proguard-rules.pro
│   └── src
├── gradle
│   ├── libs.versions.toml
│   └── wrapper
│      ├── gradle-wrapper.jar
│      └── gradle-wrapper.properties
├── build.gradle.kts
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle.kts
```

Inside the `libs.versions.toml` file, you can add the dependencies of your projects.

```toml
[versions]
compose = "1.2.1"

[libraries]
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material = { module = "androidx.compose.material:material", version.ref = "compose" }
compose-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }

[bundles]
compose = ["compose-foundation", "compose-material", "compose-tooling", "compose-ui"]
```

That's it! The dependencies are available across your Gradle projects. 

Here is how you use the dependencies in your project's `build.gradle.kts`.

```kotlin
dependencies {
	implementation(libs.compose.foundation)
	implementation(libs.compose.material)
	implementation(libs.compose.tooling)
	implementation(libs.compose.ui)
}
```

Note that Gradle converts the dash (`-`) separator to dot (`.`). From `compose-foundation` to `compose.foundation`.

Other benefits of using version catalogs are:

- Centralized version for some libraries.

By using `version.ref`, we can assign the same compose version for each library. So, you just need to update a library version in one place.

- Grouping dependencies.

Oftentimes, we have many dependencies that should be declared together. We can make the dependencies declaration shorter by using `bundle`, like this.

```kotlin
dependencies {
	implementation(libs.bundles.compose)
}
```

## Summary

By using the version catalog, we can manage dependencies easier. This can be useful for a multimodule project, which is common in a mid-sized or large-sized software project.

Heads over to [the official Gradle documentation](https://docs.gradle.org/current/userguide/platforms.htm) for more detail.

Note:
- Gradle introduces version catalog since version [7.0](https://docs.gradle.org/7.0/release-notes.html), but it's still marked as an experimental feature.

- Photo taken by [redcharlie](https://unsplash.com/photos/Y--zr3CPaPs)