赞
踩
“版本目录是一份依赖项列表,以依赖坐标表示,用户在构建脚本中声明依赖项时可以从中选择。”
我们可以使用版本目录将所有依赖项声明及其版本号保存在单个位置。这样,我们可以轻松地在模块和项目之间共享依赖项和版本配置列表。
有了IDE插件支持,将依赖项导入到我们的项目中会更容易,因为它提供了基于我们在版本目录中定义的内容的自动完成功能。
最好的方式是创建一个TOML(Tom's Obvious Minimal Language)文件作为其可移植性。请注意,此TOML文件不是唯一的真相来源,因为我们始终可以在脚本的不同位置硬编码其他依赖项和版本,而IDE不会强制将所有内容保存在版本目录中。
创建版本目录只需要几个步骤。大部分的工作将花在准备TOML文件上,这取决于我们在项目中所拥有的依赖项。
gradle 7.4 版本中的版本编目是稳定的,当使用 7.4+ 版本时,此步骤不是必需的。
升级 Gradle 运行./gradlew wrapper --gradle-version=7.6
以将项目更新到 7.6 版本。
在旧版 Gradle 上使用 enableFeaturePreview 请注意,在 Gradle 8.0+ 上不再需要 enableFeaturePreview
。
在settings.gradle
或 settings.gradle.kts
中进行更改:
Kotlin DSL
- enableFeaturePreview("VERSION_CATALOGS")
- include(":app")
Groovy
- enableFeaturePreview("VERSION_CATALOGS")
- include ':app'
同样的语法,再次将其添加到相同的settings.gradle
或settings.gradle.kts
中。
- dependencyResolutionManagement {
- repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
- repositories {
- google()
- mavenCentral()
- }
- }
libs.versions.toml
文件中声明依赖项• 创建gradle/libs.versions.toml
文件
• 值得注意的是,要检查您的 .gitignore
文件是否忽略了 /gradle/
目录,以便您的版本库会保留lib.versions.toml
文件。
• Android Studio 应该已经捆绑了 Toml 插件;对于 IntelliJ IDEA,您可以双重检查以确保您已经为扩展 IDE 支持安装了该插件。
我的样例libs.versions.toml文件 是一个早期版本,需要继续完善。请注意,文件中包含以下四个部分:
• versions
• libraries
• plugins
• bundles
同时需要注意的是,如果名称中有“-”,在Gradle构建脚本中引用时必须将其改成“.”。例如,如果我们定义了hilt-android
,那么在调用时它将变成libs.hilt.android
。
- [versions]
- # Define the dependency versions
- minSdk = "26"
- targetSdk = "33"
- compileSdk = "33"
-
- accompanist = "0.27.0"
- androidx-activity-compose = "1.6.1"
- androidx-compose-bom = "2022.11.00"
- androidx-core-ktx = "1.9.0"
- androidx-lifecycle-runtime-compose = "2.6.0-alpha03"
- androidx-navigation-compose = "2.5.3"
- coil = "2.2.2"
- compose-compiler = "1.3.2"
- coroutines = "1.6.4"
- espresso = "3.5.0"
- gradle-plugin = "7.2.2"
- androidBuildTools = "7.2.2"
- hilt = "2.44.1"
- hilt-navigation-compose = "1.0.0"
- junit = "4.13.2"
- kotest = "5.5.4"
- kotlin = "1.7.20"
- kotlinx-serialization-json = "1.4.1"
- kover = "0.5.0"
- ktlint = "11.0.0"
- ktlint-plugin = "7.1.0"
- ktor = "2.1.3"
- leakcanary = "2.10"
- mockk = "1.13.2"
- room = "2.4.3"
- test-rules = "1.5.0"
- text-ext = "1.1.4"
- timber = "5.0.1"
-
- [libraries]
- kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
- android-gradlePlugin = { module = "com.android.tools.build:gradle", version.ref = "androidBuildTools"}
-
- androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidx-core-ktx" }
- androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle-runtime-compose" }
- androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" }
- androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation-compose" }
-
- androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" } # kapt
- androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
- androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
- androidx-room-testing = { module = "androidx.room:room-testing", version.ref = "room" }
-
- junit = { module = "junit:junit", version.ref = "junit" }
- androidx-test-junit4 = { module = "androidx.test.ext:junit", version.ref = "text-ext" }
- androidx-test-rules = { module = "androidx.test:rules", version.ref = "test-rules" }
- androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" }
- androidx-test-espresso-idling-resource = { module = "androidx.test.espresso:espresso-idling-resource", version.ref = "espresso" }
-
- androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "androidx-compose-bom" }
- androidx-compose-material = { module = "androidx.compose.material:material" }
- androidx-compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
- androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
- androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" }
- androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }
-
- timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
-
- coil-kt = { module = "io.coil-kt:coil", version.ref = "coil" }
- coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
-
- kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
-
- kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
- kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
- kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" }
-
- kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
- kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
- kotest-property = { module = "io.kotest:kotest-property", version.ref = "kotest" }
-
- ktor-client-android = { module = "io.ktor:ktor-client-android", version.ref = "ktor" }
- ktor-client-serialization = { module = "io.ktor:ktor-client-serialization", version.ref = "ktor" }
- ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
- ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
- ktor-client-logging-jvm = { module = "io.ktor:ktor-client-logging-jvm", version.ref = "ktor" }
- ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
- ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "ktor" }
-
- hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
- hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hilt" }
- hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hilt-navigation-compose" }
- hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
- hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", version.ref = "hilt" }
-
- mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
- mockk-agent-jvm = { module = "io.mockk:mockk-agent-jvm", version.ref = "mockk" }
- mockk-android = { module = "io.mockk:mockk-android", version.ref = "mockk" }
- accompanist-webview = { module = "com.google.accompanist:accompanist-webview", version.ref = "accompanist" }
- accompanist-swiperefresh = { module = "com.google.accompanist:accompanist-swiperefresh", version.ref = "accompanist" }
- leakcanary-android = { module = "com.squareup.leakcanary:leakcanary-android", version.ref = "leakcanary" }
- ktlint-gradle = { module = "org.jlleitschuh.gradle:ktlint-gradle", version.ref = "ktlint" }
-
- [bundles]
- # Define bundles/groups of libraries
- coil = ["coil-kt", "coil-kt-compose"]
- room = ["androidx-room-ktx", "androidx-room-runtime"]
- kotest = ["kotest-runner-junit5", "kotest-assertions-core", "kotest-property"]
- ktor = ["ktor-client-android", "ktor-client-serialization", "ktor-client-cio", "ktor-client-content-negotiation", "ktor-client-logging-jvm", "ktor-serialization-kotlinx-json"]
- coroutines = ["kotlinx-coroutines-core", "kotlinx-coroutines-android"]
-
- [plugins]
- # android-application = { id = "com.android.application", version.ref = "gradle-plugin" }
- kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
- hilt-android-plugin = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
- kover-plugin = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
- kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
- kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
- android-library = { id = "com.android.library", version.ref = "gradle-plugin" }
- ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint-plugin" }
更新依赖
- // change from
- implementation 'com.google.dagger:hilt-android:2.43.2'
- kapt 'com.google.dagger:hilt-compiler:2.43.2'
-
- // change to
- implementation libs.hilt.android
- kapt libs.hilt.complier
我们还可以将minSdk和targetSdk移动到版本目录中。
Groovy
https://docs.gradle.org/7.0/userguide/platforms.html#sub:central-declaration-of-dependencies https://proandroiddev.com/gradle-version-catalogs-for-an-awesome-dependency-management-f2ba700ff894 https://umang91.medium.com/integrating-a-version-catalog-c5c7d45516db
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。