VersionCatalog gradle7.0后,统一版本管理文件
versionCatalogs {
create("libs") { //默认提供了libs,如果使用libs,就不需要create了,直接在./gradle 创建libs.versions.toml即可
from(files("./gradle/libs.versions.toml"))
}
create("mylibs"){//创建自定义版本文件toml(不能使用libs,否则报错)
from(files("./gradle/my.libs.versions.toml")) //注意:自定义的toml文件(不能使用libs.versions.toml,否则会报错)
}
}
settings.gradle.kts
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
// 如果不需要自定义,此处可以不写,androidstudio中 默认提供了create(libs),直接使用即可
```
// versionCatalogs {
// create("libs") {
// from(files("./gradle/ants-libs.versions.toml"))
// }
// }
}
rootProject.name = "Demo"
include(":app")
project build.gradle.kts
@Suppress("DSL_SCOPE_VIOLATION") //添加DSL_SCOPE_VIOLATION,处理alias报错
plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
app build.gradle.kts
plugins {
id("com.android.application")
id("kotlin-android")
id("kotlin-kapt")
}
android{
...
}
dependencies {
//通过libs引用依赖 libs.versions.toml文件中 -变成. 例如:androidx-core-ktx ----> libs.androidx.core.ktx
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.tooling.preview)
debugImplementation(libs.androidx.compose.ui.tooling)
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.test.manifest)
implementation(libs.androidx.compose.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.espresso.core)
}
libs.versions.toml
[versions] #版本号
androidGradlePlugin = "7.3.1"
kotlin = "1.6.10"
compose = "1.3.1"
coreKtx = "1.7.0"
lifecycleRuntimeKtx = "2.3.1"
material3 = "1.0.0-alpha02"
junit = "4.13.2"
testExtJunit = "1.1.3"
testEspressoCore= "3.4.0"
[libraries] #依赖
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-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "compose" }
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "compose" }
androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview", version.ref = "compose" }
androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling", version.ref = "compose" }
androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4", version.ref = "compose" }
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest", version.ref = "compose" }
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3", version.ref = "material3" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "testExtJunit" }
androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "testEspressoCore" }
[plugins] #插件
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
android-test = { id = "com.android.test", version.ref = "androidGradlePlugin" }