Managing dependencies like a pro in Android with version catalogs

While building multi-modular android applications it can be a painful task to manage dependencies especially when one have to maintain project one inherited…haha, in this post we will see better ways of setting up our dependencies so they can be man…


This content originally appeared on DEV Community and was authored by Chibueze felix

While building multi-modular android applications it can be a painful task to manage dependencies especially when one have to maintain project one inherited...haha, in this post we will see better ways of setting up our dependencies so they can be managed better form one single file even in large projects.
In this article we will cover the following:

  1. What is version catalog
  2. Steps How to Migrate to version catalog **

What is version catalog

**
Before now, how you add dependencies to your project in a modern android application it painfully like so:

// .....
//other codes.....
dependencies {

    implementation("androidx.core:core-ktx:1.10.1")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
    implementation("androidx.activity:activity-compose:1.7.0")
    implementation(platform("androidx.compose:compose-bom:2023.08.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.08.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}

Hold on a second! imagine for a moment, you have to do this for every build.gradle file for every module in a multi modular project say with about 20 modules; Welcome back, yes many survived it,I did, however its time consuming especially when you face managing compatibility and versions between modules. This is where version catalogs becomes a hero for saving your development time as well as future managements and dependencies upgrade, isn't that great? just have one place to look for all application dependencies.

According to google developers,

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. Instead of hardcoding dependency names and versions in individual build files and updating each entry whenever you need to upgrade a dependency, you can create a central version catalog of dependencies that various modules can reference in a type-safe way with Android Studio assistance.

So basically , a version catalog file is a TOML file that is used to manage dependencies in modern android development.

The TOML file consists of 4 major sections:
the [versions] section is used to declare versions which can be referenced by dependencies
the [libraries] section is used to declare the aliases to coordinates
the [bundles] section is used to declare dependency bundles
the [plugins] section is used to declare plugins

Steps How to Migrate to version catalog

For brevity, there for about six simple steps to migrate and enjoy the benefits of version catalogs:

  1. Create a version catalog file: In the root project gradle folder create the libs.versions.toml Add these sections to it:
[versions]

[libraries]

[plugins]

Each. sections hold version numbers, gradle libraries and plugins respectively.

  1. Add the new entry to the file Next we will add implementation("androidx.core:core-ktx:1.10.1") to the newly created file, by first adding the version number then the. library in. the respective sections:
[versions]
ktx-version ="1.10.1"

[libraries]
androidx-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx-version" }
[plugins]
....
  1. Sync the Android project
    Once the previous step is done rightly you may sync gradle to update the new settings of your project.

  2. Replace the previous string declaration with the catalog type-safe accessor:
    Do the previous steps 2 and 3 for every new dependency you add to the libs.versions.toml file.

  3. Add the plugins to the plugins block :
    Just like the previous steps you would need to. add the version of th e plugin in the versions section but this time the dependency goes to the plugin section of the file, like so:

[versions]
ktx-version ="1.10.1"
androidGradlePlugin = "7.4.1"
//....
[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
  1. Referencing the catalog : In your build.gradle file (app) you can refer to the plugins with alias ,and libraries with implementation , see below for examples:
// Top-level build.gradle.kts
plugins {
   alias(libs.plugins.android.application) apply false

}

// module build.gradle.kts
plugins {
   alias(libs.plugins.android.application)

}

dependencies {
   implementation(libs.androidx.ktx)

}

Now you may go ahead and add more dependencies for all modules, as much as you want. Each module will reference same libs.versions.toml file so no issues with incompatible versions or wrong version

Pro Tip

  • Use alias for plugins in the version catalog aand id for plugins not in the version catalog.
  • If you are using Gradle version below 8.1 you need to annotate the plugins{} block with @Suppress("DSL_SCOPE_VIOLATION") when using version catalogs.

Well done, you have read this far !! Do well to share....


This content originally appeared on DEV Community and was authored by Chibueze felix


Print Share Comment Cite Upload Translate Updates
APA

Chibueze felix | Sciencx (2024-09-18T01:37:44+00:00) Managing dependencies like a pro in Android with version catalogs. Retrieved from https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/

MLA
" » Managing dependencies like a pro in Android with version catalogs." Chibueze felix | Sciencx - Wednesday September 18, 2024, https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/
HARVARD
Chibueze felix | Sciencx Wednesday September 18, 2024 » Managing dependencies like a pro in Android with version catalogs., viewed ,<https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/>
VANCOUVER
Chibueze felix | Sciencx - » Managing dependencies like a pro in Android with version catalogs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/
CHICAGO
" » Managing dependencies like a pro in Android with version catalogs." Chibueze felix | Sciencx - Accessed . https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/
IEEE
" » Managing dependencies like a pro in Android with version catalogs." Chibueze felix | Sciencx [Online]. Available: https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/. [Accessed: ]
rf:citation
» Managing dependencies like a pro in Android with version catalogs | Chibueze felix | Sciencx | https://www.scien.cx/2024/09/18/managing-dependencies-like-a-pro-in-android-with-version-catalogs/ |

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.