国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Gradle版本目錄(Version Catalog)

這篇具有很好參考價(jià)值的文章主要介紹了Gradle版本目錄(Version Catalog)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Gradle版本目錄(Version Catalog)

Gradle版本目錄(Version Catalog)

“版本目錄是一份依賴項(xiàng)列表,以依賴坐標(biāo)表示,用戶在構(gòu)建腳本中聲明依賴項(xiàng)時(shí)可以從中選擇。”

我們可以使用版本目錄將所有依賴項(xiàng)聲明及其版本號保存在單個(gè)位置。這樣,我們可以輕松地在模塊和項(xiàng)目之間共享依賴項(xiàng)和版本配置列表。

有了IDE插件支持,將依賴項(xiàng)導(dǎo)入到我們的項(xiàng)目中會更容易,因?yàn)樗峁┝嘶谖覀冊诎姹灸夸浿卸x的內(nèi)容的自動(dòng)完成功能。

最好的方式是創(chuàng)建一個(gè)TOML(Tom’s Obvious Minimal Language)文件作為其可移植性。請注意,此TOML文件不是唯一的真相來源,因?yàn)槲覀兪冀K可以在腳本的不同位置硬編碼其他依賴項(xiàng)和版本,而IDE不會強(qiáng)制將所有內(nèi)容保存在版本目錄中。

創(chuàng)建版本目錄只需要幾個(gè)步驟。大部分的工作將花在準(zhǔn)備TOML文件上,這取決于我們在項(xiàng)目中所擁有的依賴項(xiàng)。

啟用版本目錄(Version catalog)

gradle 7.4 版本中的版本編目是穩(wěn)定的,當(dāng)使用 7.4+ 版本時(shí),此步驟不是必需的。

升級 Gradle
運(yùn)行./gradlew wrapper --gradle-version=7.6以將項(xiàng)目更新到 7.6 版本。

在舊版 Gradle 上使用 enableFeaturePreview
請注意,在 Gradle 8.0+ 上不再需要 enableFeaturePreview

settings.gradlesettings.gradle.kts 中進(jìn)行更改:

Kotlin DSL

enableFeaturePreview("VERSION_CATALOGS")
include(":app")

Groovy

enableFeaturePreview("VERSION_CATALOGS")
include ':app'

獲取版本目錄(Version catalog)

同樣的語法,再次將其添加到相同的settings.gradlesettings.gradle.kts中。

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

Gradle版本目錄(Version Catalog)

在 libs.versions.toml 文件中聲明依賴項(xiàng)

  • 創(chuàng)建gradle/libs.versions.toml文件
  • 值得注意的是,要檢查您的 .gitignore 文件是否忽略了 /gradle/ 目錄,以便您的版本庫會保留lib.versions.toml文件。
  • Android Studio 應(yīng)該已經(jīng)捆綁了 Toml 插件;對于 IntelliJ IDEA,您可以雙重檢查以確保您已經(jīng)為擴(kuò)展 IDE 支持安裝了該插件。

我的樣例libs.versions.toml文件
是一個(gè)早期版本,需要繼續(xù)完善。請注意,文件中包含以下四個(gè)部分:

  • versions
  • libraries
  • plugins
  • bundles

同時(shí)需要注意的是,如果名稱中有“-”,在Gradle構(gòu)建腳本中引用時(shí)必須將其改成“.”。例如,如果我們定義了hilt-android,那么在調(diào)用時(shí)它將變成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

Gradle版本目錄(Version Catalog)

擴(kuò)展用法

我們還可以將minSdk和targetSdk移動(dòng)到版本目錄中。

Groovy
Gradle版本目錄(Version Catalog)

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文章來源地址http://www.zghlxwxcb.cn/news/detail-488021.html

到了這里,關(guān)于Gradle版本目錄(Version Catalog)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包