At the moment, there are many options for describing dependencies in projects using Gradle. However, there is no recommended standard. So, a variety of approaches are used - and for new developers, it is not always obvious what, where, and how is used in a large and multi-module project. The 7th version of Gradle introduces a new feature that allows you to describe all dependencies in a centralized way.
A version catalog is a replacement for the previous patterns, supported by Gradle, without the drawbacks of the previous approaches.
To add support for version catalogs, you need to enable the feature by adding the following in your settings file settings.gradle.kts
enableFeaturePreview("VERSION_CATALOGS")
Gradle provides an API to declare catalogs. This API is designed for type safety.
Version catalogs can be declared in the settings.gradle.kts
dependencyResolutionManagement {
...
versionCatalogs {
create("libs") {
version("core", "1.6.0")
version("activity", "1.3.1")
alias("core-ktx").to("androidx.core", "core-ktx").versionRef("core")
alias("activity-ktx").to("androidx.activity", "activity-ktx").versionRef("activity")
alias("activity-compose").to("androidx.activity", "activity-compose").versionRef("activity")
bundle("activity", listOf("activity-ktx", "activity-compose"))
}
}
}
This will generate an extension in your Gradle build named libs
app/build.gradle.kts
dependencies {
implementation(libs.core.ktx)
implementation(libs.bundles.activity)
}
If you are using an external directory, you must use this API. This allows users can get recommendations.
To do that, create a file deps.toml
[versions]
koin = "3.1.2"
kotlin = "1.5.31"
[libraries]
# Koin
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" }
koin-android-compat = { module = "io.insert-koin:koin-android-compat", version.ref = "koin" }
koin-androidx-workmanager = { module = "io.insert-koin:koin-androidx-workmanager", version.ref = "koin" }
koin-androidx-compose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin" }
[bundles]
koin = ["koin-android", "koin-android-compat", "koin-androidx-workmanager", "koin-androidx-compose"]
[plugins]
kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
The TOML file consists of 4 major sections:
the [versions]
section is used to declare versions that 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
Version catalogs can be declared settings.gradle(.kts)
dependencyResolutionManagement {
...
versionCatalogs {
create("dependencies") {
from(files("deps.toml"))
}
}
}
This will generate an extension in your Gradle build named deps
app/build.gradle.kts
dependencies {
implementation(deps.bundles.koin)
}
This plugin provides a task to determine which dependencies have updates. Additionally, the plugin checks for updates to Gradle itself.
You can add this plugin to your top-level build script using the following configuration.
settings.gradle.kts
pluginManagement {
...
plugins {
id("com.github.ben-manes.versions") version "0.39.0"
}
}
build.gradle.kts
plugins {
...
id ("com.github.ben-manes.versions")
}
android {
...
}
dependencies {
...
}
tasks {
named<com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask>("dependencyUpdates") {
checkForGradleUpdate = true
outputFormatter = "html"
outputDir = "build/dependencyUpdates"
reportfileName = "report"
}
}
Displays a report of the project dependencies that are up-to-date exceed the latest version found, have upgrades, or failed to be resolved.
After you added the above code snippet to the file, you can simply type
gradle dependencyUpdates
It will check if there are newer versions for your dependencies, and this displays a report to the console
------------------------------------------------------------
:app Project Dependency Updates (report to plain text file)
------------------------------------------------------------
The following dependencies are using the latest milestone version:
- androidx.constraintlayout:constraintlayout:2.1.0
- com.android.application:com.android.application.gradle.plugin:7.1.0-alpha12
- com.github.ben-manes.versions:com.github.ben-manes.versions.gradle.plugin:0.39.0
- io.insert-koin:koin-android:3.1.2
- io.insert-koin:koin-android-compat:3.1.2
- io.insert-koin:koin-androidx-compose:3.1.2
- io.insert-koin:koin-androidx-workmanager:3.1.2
The following dependencies have later milestone versions:
- androidx.activity:activity-compose [1.3.1 -> 1.4.0-alpha02]
https://developer.android.com/jetpack/androidx/releases/activity#1.4.0-alpha02
- androidx.activity:activity-ktx [1.3.1 -> 1.4.0-alpha02]
https://developer.android.com/jetpack/androidx/releases/activity#1.4.0-alpha02
- androidx.appcompat:appcompat [1.3.1 -> 1.4.0-alpha03]
https://developer.android.com/jetpack/androidx/releases/appcompat#1.4.0-alpha03
- androidx.core:core-ktx [1.6.0 -> 1.7.0-beta01]
https://developer.android.com/jetpack/androidx/releases/core#1.7.0-beta01
- com.google.android.material:material [1.4.0 -> 1.5.0-alpha03]
https://github.com/material-components/material-components-android
- org.jacoco:org.jacoco.ant [0.8.3 -> 0.8.7]
http://jacoco.org
- org.jetbrains.kotlin.android:org.jetbrains.kotlin.android.gradle.plugin [1.5.31 -> 1.6.0-M1]
- org.jetbrains.kotlinx:kotlinx-coroutines-android [1.3.9 -> 1.5.2]
https://github.com/Kotlin/kotlinx.coroutines
Gradle release-candidate updates:
- Gradle: [7.2: UP-TO-DATE]
Generated report file build\dependencyUpdates\report.html
sample (full code)
https://gist.github.com/tkrest/98410f8b7ac4490e82df10838f14f4e3
Sharing dependency versions between projects
https://docs.gradle.org/current/userguide/platforms.html
Gradle version plugin
https://github.com/ben-manes/gradle-versions-plugin
Declaring Rich Versions
https://docs.gradle.org/current/userguide/rich_versions.html#rich-version-constraints