As datta said in their answer, Gradle now has something called version catalogs.
Here is an example for Kotlin DSL (*.kts files). Note that I'm using Gradle 7.4.
Defining dependencies in settings.gradle.kts:
// Configure dependencies aspects applied to all projects
dependencyResolutionManagement {
// By default, repositories declared by a project will override the ones here.
// You can change this behavior with the repositoriesMode property.
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
// Define repositories for all projects
repositories {
mavenCentral()
maven("https://jitpack.io")
}
versionCatalogs {
create("libs") {
// Versions are useful specially when you have libraries with the same
// group and version which are updated together with the same version
version("room", "2.4.1")
// │ │
// │ └───> The version notation
// └───> Your desired name (alias)
library("material", "com.google.android.material:material:1.4.0")
// │ │
// │ └───> The dependency notation (coordinates)
// ├───> Your desired name (alias); only letters, digits and _ - .
// └───> Note that _ - . will all be normalized to .
// You can configure the version as you would in a regular build file
// Note that the group and module are separate parameters
library("junit5", "org.junit.jupiter", "junit-jupiter").version {
prefer("5.8.0")
}
// Using the same version for multiple dependencies
library("room-ktx", "androidx.room", "room-ktx").versionRef("room")
library("room-runtime", "androidx.room", "room-runtime").versionRef("room")
}
}
}
Usage in build.gradle[.kts]:
dependencies {
implementation(libs.material)
implementation(libs.room.ktx)
implementation(libs.room.runtime)
testImplementation(libs.junit5)
}
As you can see, not only can you declare dependencies, but also you can declare the repositories here as well (instead of using allprojects
block in your top-level build script to define repositories for all subprojects).
For groovy syntax of above solution and for more information about version catalogs and centralizing your repository and dependency configurations, see Gradle official guides.