曾经我们使用config.gradle文件进行版本依赖配置,然后在project的build.gradle.kts中使用如下方式引入:
Kotlin
apply(from = "./config.gradle")
缺点:在project的module中引用无任何提示,无法跳转到指定引用
一、创建version catalog文件
创建catalog文件步骤如上图,名称使用默认名libs(也可以修改名称,在settings中引入的时候使用修改的文件明就行)创建libs.version.toml
此文件生成之后在如下目录,此时文件不用在settings.gradle中进行配置,如果文件名不是libs还是需要自行引入。
建议:文件名仍然使用libs,但是将文件libs.versions.toml移动到Project的根目录,然后在settings.gradle中显示引入。
版本目录TOML文件格式,TOML 文件由 4 个主要部分组成:
- [versions] 用于声明可以被依赖项引用的版本
- [libraries] 用于声明引用的依赖包的别名
- [bundles] 用于声明依赖包,可以将步骤2中定义的依赖包统一引入
- [plugins] 用于声明插件
举例:
Kotlin
[versions]
groovy = "3.0.5"
ksp = "1.9.21-1.0.16"
[libraries]
groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
[bundles]
groovy = ["groovy-core", "groovy-json", "groovy-nio"]
[plugins]
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
例如在app.module中可以单独引入 libraties中的依赖包或者引入bundles中的一组依赖包
Kotlin
dependencies {
implementation(libs.bundles.groovy)
// 或者分开引入
implementation(libs.groovy.core)
implementation(libs.groovy.json)
implementation(libs.groovy.nio)
}
二、Settings.gradle中引入libs.versions.toml
Kotlin
dependencyResolutionManagement {
......
// 引入Catalogs
versionCatalogs {
create("libs") {
from(files("./libs.versions.toml"))
}
}
}
三、在project build.gradle和module的build.gradle都能开始使用
libs.versions.toml文件
Groovy
[versions]
compileSdk = "34"
minSdk = "24"
targetSdk = "34"
versionCode = "1"
versionName = "1.0"
# 插件
application = "8.3.2"
kotlin = "1.9.10"
ksp = "1.9.21-1.0.16"
# 三方库
core-ktx = "1.10.1"
appcompat = "1.6.1"
material = "1.10.0"
constraintlayout = "2.1.4"
junit = "4.13.2"
android-junit = "1.1.5"
espresso-core = "3.5.1"
room = "2.5.1"
moshi = "1.15.1"
[libraries]
core-ktx = { module = "androidx.core:core-ktx", version.ref = "core-ktx" }
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
material = { module = "com.google.android.material:material", version.ref = "material" }
constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
junit = { module = "junit:junit", version.ref = "junit" }
android-junit = { module = "androidx.test.ext:junit", version.ref = "android-junit" }
espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso-core" }
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-testing = { module = "androidx.room:room-testing", version.ref = "room" }
moshi = { module = "com.squareup.moshi:moshi-kotlin-codegen", version.ref = "moshi" }
[bundles]
room = ["room-runtime"]
[plugins]
application = { id = "com.android.application", version.ref = "application" }
kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
3.1 引用插件
Project build.gradle
Groovy
plugins {
alias(libs.plugins.application) apply false
alias(libs.plugins.kotlin) apply false
alias(libs.plugins.ksp) apply false
}
App module build.gradle
Groovy
plugins {
alias(libs.plugins.application)
alias(libs.plugins.kotlin)
alias(libs.plugins.ksp)
}
3.2 引入版本
Groovy
android {
compileSdk = libs.versions.compileSdk.get().toInt()
defaultConfig {
applicationId = "xxxxxx"
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = libs.versions.versionCode.get().toInt()
versionName = libs.versions.versionName.get()
}
}
3.3 引入依赖
Groovy
dependencies {
implementation(libs.core.ktx)
implementation(libs.appcompat)
implementation(libs.material)
implementation(libs.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.android.junit)
androidTestImplementation(libs.espresso.core)
// Room
implementation(libs.room.runtime)
annotationProcessor(libs.room.compiler)
// To use Kotlin Symbol Processing (KSP)
ksp(libs.room.compiler)
// optional - Kotlin Extensions and Coroutines support for Room
implementation(libs.room.ktx)
// 测试数据库
testImplementation(libs.room.testing)
// moshi
ksp(libs.moshi)
}