Hey! My name is Ksenia Kaisheva, I write Android applications at 65apps. Today I will talk about a new feature that allows you to centrally describe dependencies on projects with the Gradle build system.
At the moment, there are many options for describing dependencies in projects using Gradle. There is no recommended standard, so a variety of approaches are used: someone simply lists the dependencies in the dependencies block , someone puts the dependencies in a separate file, an ext block , etc. 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. This function is at the preview stage, and to use it in the settings.gradle (.kts) file you need to add the line:
enableFeaturePreview("VERSION_CATALOGS")
( ) build.gradle :
dependencies {
implementation libs.lifecycle.runtime
implementation libs.lifecycle.viewmodel.ktx
implementation libs.lifecycle.extentions
implementation libs.lifecycle.livedata.ktx
}
:
libs —
lifecycle.runtime — .
settings.gradle(.kts) :
dependencyResolutionManagement {
versionCatalogs {
libs {
alias('lifecycle-runtime').to('androidx.lifecycle:lifecycle -runtime:2.2.0')
alias('lifecycle-viewmodel-ktx').to('androidx.lifecycle', 'lifecycle-viewmodel-ktx')
.version {
strictly '[2.2.0, 2.3.0['
prefer '2.3.1'
}
}
}
}
(alias) , , .
.
, . .
,
lifecycle-runtime
lifecycle_runtime
lifecycle.runtime
junit5-test-core
spek-runner-junit5
, . , lifecycle-viewmodel lifecycle-viewmodel-ktx.
Gradle .
, lifecycleViewmodel lifecycleViewmodelKtx.
:
dependencyResolutionManagement {
versionCatalogs {
libs {
version('lifecycle', '2.3.1')
alias('lifecycle-viewmodel-ktx').to('androidx.lifecycle', 'lifecycle-viewmodel-ktx')
.versionRef('lifecycle')
}
}
}
build.gradle :
version = libs.versions.lifecycle.get()
, . . — :
dependencyResolutionManagement {
versionCatalogs {
libs {
version('lifecycle', '2.3.1')
alias('lifecycle-runtime').to(
'androidx.lifecycle’, ' lifecycle -runtime').versionRef(' lifecycle ')
alias ('lifecycle-viewmodel-ktx').to(
'androidx.lifecycle’, ' lifecycle -viewmodel - ktx').versionRef(' lifecycle ')
bundle ('lifecycle', ['lifecycle-runtime', 'lifecycle-viewmodel-ktx']
)
}
}
}
:
dependencies {
implementation libs.bundles.lifecycle
}
.
settings.gradle(.kts) , — toml- gradle: libs.versions.toml.
, .
libs.versions.toml libs . , :
dependencyResolutionManagement {
defaultLibrariesExtensionName.set('deps')
}
Toml- 3 :
[versions] -
[libraries] -
[bundles] -
,
[versions]
lifecycle = "2.3.1"
[libraries]
lifecycle-runtime = { module = "androidx.lifecycle:lifecycle-runtime", version.ref = "lifecycle" }
lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
[bundles]
dagger = ["lifecycle-runtime", "lifecycle-viewmodel-ktx"]
, , , :
[versions]
any-lib1 = ‘1.0’
any-lib2 = { strictly = "[1.0, 2.0[", prefer = "1.2" }
, :
[libraries]
any-lib = "com.company:anylib:1.4"
any-other-lib = { module = "com.company:other", version="1.4" }
any-other-lib2 = { group = "com.company", name="alternate", version="1.4" }
anylib-full-format = { group = "com.company", name="alternate", version={ require = "1.4" } }
, [versions] , version.ref:
[versions]
some = "1.4"
[libraries]
any-lib = { group = "com.company", name="anylib", version.ref="some" }
toml-. , :
dependencyResolutionManagement {
versionCatalogs {
testLibs {
from(files('gradle/test-libs.versions.toml'))
}
}
}
, .
Groovy build.gradle , , . . — Kotlin DSL.