https://www.pexels.com/photo/a-woman-sitting-at-a-desk-in-a-library-6549635/
In complex and modular Android projects, managing dependencies can be a daunting and time-consuming task. Gradle Version Catalogs, introduced in Gradle 7.0 and promoted to stable in version 7.4, offer an elegant solution to streamline dependency management. In this comprehensive guide, we will explore the benefits of Version Catalogs, demonstrate how to implement them in an Android project with code snippets and provide tips to help you get the most out of this powerful feature.
Why Use Version Catalogs?
- Centralized and shareable configuration: By consolidating dependency coordinates in a single file, Version Catalogs simplify management and promote consistent configuration across multiple projects.
- Improved performance: Compared to buildSrc, Version Catalogs offer better performance, as updating dependency versions no longer requires a complete rebuild.
- Flexibility: Version Catalogs support the creation of dependency bundles, enabling developers to add a single implementation line for a set of libraries in their Gradle files.
- Version Catalogs support third-party plugins for automatic version updates and offer better performance compared to buildSrc solutions.Type-safe dependencies: Version Catalogs encourage type-safe dependency declarations, reducing typos and improving IDE support for content assistance.
Implementing Version Catalogs in an Android Project:
Follow these straightforward steps to integrate Version Catalogs into your Android project:
- Ensure your project uses Gradle 7.4 or newer. Check or upgrade by executing the appropriate Gradle Wrapper commands.
- Create a libs.versions.toml file in your root gradle folder to store your dependency definitions, including versions, libraries, bundles, and plugins.
- Here’s a snippet with a few examples :
[versions] compileSdk = "33" minSdk = "24" targetSdk = "33" voyager = "1.0.0-rc04" material-icon-version = "1.3.0" [libraries] voyager-navigator = { module = "cafe.adriel.voyager:voyager-navigator", version.ref = "voyager" } voyager-bottomSheetNavigator = { module = "cafe.adriel.voyager:voyager-bottom-sheet-navigator", version.ref = "voyager" } voyager-tabNavigator = { module = "cafe.adriel.voyager:voyager-tab-navigator", version.ref = "voyager" } voyager-transitions = { module = "cafe.adriel.voyager:voyager-transitions", version.ref = "voyager" } material-icon-extended = {module = "org.jetbrains.compose.material:material-icons-extended", version.ref = "material-icon-version"}
4. Configure your settings.gradle file to enable Version Catalogs
enableFeaturePreview("VERSION_CATALOGS") enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
5. Now sync gradle files and rebuild your project.
6. Update your application build.gradle file to define your plugins and implementations using the Version Catalog:
plugins { id 'com.android.application' id 'kotlin-android' } android { // Your Android configuration here } dependencies { implementation(libs.voyager.navigator) implementation(libs.voyager.transitions) implementation(libs.voyager.bottomSheetNavigator) api(compose.material) }
Job Offers
7. You are done.
Important Points to Know
- Version Catalogs support automatic version updates using tools such as RenovateBot for GitHub projects. This bot reads your libs.versions.toml file and creates Pull Requests to update your dependencies.
- Version Catalogs do not currently support precompiled script plugins. Dependencies declared in libs.versions.toml will be visible in build.gradle files of all your modules, but not in custom/precompiled ones. You can work around this limitation by creating an extension function to manually expose the dependency.
Conclusion
Version Catalogs offer an improved way to manage dependencies in Android projects. They simplify maintenance, leverage Gradle’s flexibility, and are increasingly being adopted by the community. By following the steps outlined in this article, you can easily integrate Version Catalogs into your Android projects and enjoy the benefits of better dependency management.
Further Resources
- Official Gradle Documentation
- Cédric Champeau’s Article on Version Catalogs
- GitHub — android/nowinandroid: A fully functional Android app built entirely with Kotlin and Jetpack
- A KMP template by me to help you get started with KMP development with compose multiplatform
I am on Twitter and Linkedin, let’s connect.
This article was previously published on proandroiddev.com