Migration Gradle Dependencies to Version Catalogs — libs.versions.toms (Part 3)

Nicos Nicolaou
6 min readMay 3, 2024

--

For this article we will see and explain the new way to initialize the Libraries and Plugins into libs.versions.toms file and after initialize them inside the Dependencies section in Gradle files. This is the third part of the series “Migration from Groovy to Kotlin DSL Gradle”.

The new way gives the possibilities to us (Developers) when we have multiple applications or multiple modules inside in one project to make the Libraries and Plugins fully generic for whole the project.

The Version Catalogs is separated into three sections: versions, libraries and plugins. The “libraries” section has group, name and version.ref for each library. For the “plugins” section has id and version.ref. for each plugin. For those keywords we will see an example below.

Important Note: When you initialize Libraries and Plugins into libs.versions.toms file, you have to Sync the Project, so, the Android Studio generated the dependencies.

[versions]
# initialize the versions number of the libraries and plugins

[libraries]
# initialize the libraries group, name and version number

[plugins]
# initialize the plugins id, and version number

Example with Library

Kotlin

val activityVersion by extra("1.9.0")

dependencies {
implementation("androidx.activity:activity-compose:$activityVersion")
}

Version Catalogs with Kotlin

The group section, androidx.activity, is the first part until : (Quote), and the name section is second part after : (Quote), activity-compose, and the last section is the library version number.

[version]
activityCompose = "1.9.0"

[libraries]
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }

Kotlin

dependencies {
implementation(libs.androidx.activity.compose)
}

Example with Plugins

Kotlin

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp")
}

Version Catalogs with Kotlin

[version]
agp = "8.5.0"
kotlin = "2.0.0"
kspVersion = "2.0.0-1.0.22"

# Here is the library section

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "kspVersion" }hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hiltVersion" }

Bonus (Most common Libraries and Plugins for your projects)

Note: I will try to add more Libraries and Plugins in the future. I will update the versions

Version Catalogs

[versions]
agp = "8.5.2"
kotlin = "2.0.10"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.4"
activityCompose = "1.9.1"
composeVersion = "1.6.8"
composeHiltNavigationVersion = "1.2.0"
composeNavigationVersion = "2.7.7"
uiComposeVersion = "1.6.8"
animationComposeVersion = "1.6.8"
foundationComposeVersion = "1.6.8"
composeBom = "2024.06.00"
roomVersion = "2.6.1"
kspVersion = "2.0.10-1.0.24"
retrofitVersion = "2.11.0"
okHttpVersion = "4.12.0"
coilVersion = "2.7.0"
coroutineVersion = "1.8.1"
materialVersion = "1.12.0"
hiltVersion = "2.52"
hiltCompilerVersion = "1.2.0"
swipeRefreshLayoutVersion = "1.1.0"
gsonVersion = "2.11.0"
paletteVersion = "1.0.0"
imagePickerAndroid = "2.0.15"
percentagesWithAnimationComposeVersion = "1.0.0"

[libraries]
# My Library - https://github.com/NicosNicolaou16/ImagePickerAndroid
image-picker-android = { group = "com.github.NicosNicolaou16", name = "ImagePickerAndroid", version.ref = "imagePickerAndroid" }
# My Library - https://github.com/NicosNicolaou16/PercentagesWithAnimationCompose
percentages-with-animation-compose = { group = "com.github.NicosNicolaou16", name = "PercentagesWithAnimationCompose", version.ref = "percentagesWithAnimationComposeVersion" }
# Architecture
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-swipe-refresh-layout = { group = "androidx.swiperefreshlayout", name = "swiperefreshlayout", version.ref = "swipeRefreshLayoutVersion" }
# Compose
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "uiComposeVersion" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-navigate-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "composeNavigationVersion" }
androidx-hilt-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "composeHiltNavigationVersion" }
androidx-compose-runtime = { group = "androidx.compose.runtime", name = "runtime", version.ref = "composeVersion" }
androidx-compose-runtime-livedata = { group = "androidx.compose.runtime", name = "runtime-livedata", version.ref = "composeVersion" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
androidx-animation = { group = "androidx.compose.animation", name = "animation", version.ref = "animationComposeVersion" }
androidx-foundation = { group = "androidx.compose.foundation", name = "foundation", version.ref = "foundationComposeVersion" }
# Room Database
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "roomVersion" }
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "roomVersion" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomVersion" }
# Retrofit
retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofitVersion" }
retrofit-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofitVersion" }
# OkHttp
okHttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okHttpVersion" }
# Gson
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gsonVersion" }
# Coil
coil = { group = "io.coil-kt", name = "coil-compose", version.ref = "coilVersion" }
# Coroutine
coroutine-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutineVersion" }
coroutine-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutineVersion" }
# Material
material = { group = "com.google.android.material", name = "material", version.ref = "materialVersion" }
# Hilt
dagger-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hiltVersion" }
dagger-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hiltVersion" }
hilt-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.ref = "hiltCompilerVersion" }
# Palette
androidx-palette-ktx = { group = "androidx.palette", name = "palette-ktx", version.ref = "paletteVersion" }
# Unit Test
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest", version.ref = "composeVersion" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }


[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "kspVersion" }
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hiltVersion" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

Application Gradle

plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
alias(libs.plugins.ksp)
alias(libs.plugins.hilt.android)
alias(libs.plugins.compose.compiler)
}
dependencies {

//My Library - https://github.com/NicosNicolaou16/ImagePickerAndroid
implementation(libs.image.picker.android)
//My Library - https://github.com/NicosNicolaou16/PercentagesWithAnimationCompose
implementation(libs.percentages.with.animation.compose)
//Architecture
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.swipe.refresh.layout)
implementation(libs.androidx.lifecycle.runtime.ktx)
//Compose
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.navigate.compose)
implementation(libs.androidx.hilt.compose)
implementation(libs.androidx.compose.runtime)
implementation(libs.androidx.compose.runtime.livedata)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.animation)
implementation(libs.androidx.foundation)
//Room Database
implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)
//Retrofit
implementation(libs.retrofit)
implementation(libs.retrofit.gson)
//OkHttp
implementation(libs.okHttp)
//Gson
implementation(libs.gson)
//Coil
implementation(libs.coil)
//Coroutines
implementation(libs.coroutine.core)
implementation(libs.coroutine.android)
//Material
implementation(libs.material)
//Hilt
implementation(libs.dagger.android)
ksp(libs.dagger.compiler)
ksp(libs.hilt.compiler)
//Palette
implementation(libs.androidx.palette.ktx)
//Unit Test
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}

Top Level Gradle

plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.jetbrainsKotlinAndroid) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.hilt.android) apply false
alias(libs.plugins.compose.compiler) apply false
}

I hope you find the article and sample project useful. Let me know your opinion under the comments, feel free to report any issue. I will appreciate it if you let a clap, so, if I have your support to continue writing.

--

--

Nicos Nicolaou

Senior Software Engineer, Android and Flutter Developer, Google Play Developer, Building libraries, Writing articles. 👇🔗 https://github.com/NicosNicolaou16