How to manage dependencies between Gradle modules?

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…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Aldo Wachyudi

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, 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 the root project.

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

[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.

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.

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 for more detail.

Note:

  • Gradle introduces version catalog since version 7.0, but it's still marked as an experimental feature.

  • Photo taken by redcharlie


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Aldo Wachyudi


Print Share Comment Cite Upload Translate Updates
APA

Aldo Wachyudi | Sciencx (2022-09-07T15:31:45+00:00) How to manage dependencies between Gradle modules?. Retrieved from https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/

MLA
" » How to manage dependencies between Gradle modules?." Aldo Wachyudi | Sciencx - Wednesday September 7, 2022, https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/
HARVARD
Aldo Wachyudi | Sciencx Wednesday September 7, 2022 » How to manage dependencies between Gradle modules?., viewed ,<https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/>
VANCOUVER
Aldo Wachyudi | Sciencx - » How to manage dependencies between Gradle modules?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/
CHICAGO
" » How to manage dependencies between Gradle modules?." Aldo Wachyudi | Sciencx - Accessed . https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/
IEEE
" » How to manage dependencies between Gradle modules?." Aldo Wachyudi | Sciencx [Online]. Available: https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/. [Accessed: ]
rf:citation
» How to manage dependencies between Gradle modules? | Aldo Wachyudi | Sciencx | https://www.scien.cx/2022/09/07/how-to-manage-dependencies-between-gradle-modules/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.