Katalog dependensi Gradle di Android
Solusi mudah untuk memusatkan semua dependensi proyek Anda dan membagikannya.
Kontrol versi dan dependensi di platform Android tidaklah sulit, tetapi terkadang bisa berantakan, menyebabkan versi yang tidak kompatibel di proyek Anda.
Dalam proyek besar, dengan tim berbeda pada basis kode yang sama, Anda mungkin akan mengalami konflik dan kita semua tahu bahwa mereka dapat menghabiskan waktu kita untuk menyelesaikannya.
Dengan mengingat hal itu, Katalog Versi adalah cara luar biasa untuk menyelesaikan masalah ini, mari kita bicarakan solusi ini.
Katalog Ketergantungan
Solusi mudah untuk memusatkan semua dependensi proyek Anda dan mempertahankan visibilitasnya agar dapat diakses di mana saja dalam bangunan Anda.
Katalog dibuat dengan menggunakan skrip gradle dan file toml. Mari kita lihat semua langkah untuk membuat pendekatan file toml bekerja dengan cara yang mudah.
- Buat file .toml dengan nama konvensi " libs.versions.toml " di folder gradle yang terletak di direktori root Anda.
[versi] -> nyatakan versi
[perpustakaan] -> nyatakan alias ketergantungan
[bundel] -> nyatakan bundel ketergantungan
[plugin] -> nyatakan plugin
2. Pengaturan plugin dasar.
[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
Referensi versi yang kami nyatakan di bagian [versi] :
[versions]
androidGradlePlugin = "7.4.0"
kotlin = "1.8.0"
[libraries]
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidxLifecycle" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidxActivity" }
androidx-compose-ui = { module = "androidx.compose.ui:ui", version.ref = "androidxComposeUi" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "androidxComposeUi" }
androidx-compose-material = { module = "androidx.compose.material:material", version.ref = "androidxComposeMaterial" }
#test dependencies
junit = { module = "junit:junit", version.ref = "junit" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidxTestExt" }
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "androidxComposeUi" }
androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "androidxComposeUi" }
[versions]
androidGradlePlugin = "7.4.0"
kotlin = "1.8.0"
androidxCore = "1.9.0"
androidxLifecycle = "2.5.1"
androidxActivity = "1.6.1"
androidxComposeUi = "1.3.3"
androidxComposeMaterial = "1.3.1"
junit = "4.13.2"
androidxTestExt = "1.1.5"
espressoCore = "3.5.1"
File lengkap.
#/gradle/libs.versions.toml
[versions]
androidGradlePlugin = "7.4.0"
kotlin = "1.8.0"
androidxCore = "1.9.0"
androidxLifecycle = "2.5.1"
androidxActivity = "1.6.1"
androidxComposeUi = "1.3.3"
androidxComposeMaterial = "1.3.1"
junit = "4.13.2"
androidxTestExt = "1.1.5"
espressoCore = "3.5.1"
[libraries]
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidxLifecycle" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidxActivity" }
androidx-compose-ui = { module = "androidx.compose.ui:ui", version.ref = "androidxComposeUi" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "androidxComposeUi" }
androidx-compose-material = { module = "androidx.compose.material:material", version.ref = "androidxComposeMaterial" }
#test dependencies
junit = { module = "junit:junit", version.ref = "junit" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidxTestExt" }
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "androidxComposeUi" }
androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "androidxComposeUi" }
[bundles]
[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
Mendeklarasikan libs.versions.tomlfile tidak menjadikannya satu-satunya sumber kebenaran untuk dependensi: ini adalah lokasi konvensional tempat dependensi dapat dideklarasikan. Segera setelah Anda mulai menggunakan katalog, sangat disarankan untuk mendeklarasikan semua dependensi Anda dalam katalog dan bukan string grup/artefak/versi hardcode dalam skrip build. Ketahuilah bahwa mungkin saja plugin menambahkan dependensi, yang merupakan dependensi yang ditentukan di luar file ini.
5. Terakhir, ganti dependensi kita, perhatikan bahwa Anda dapat mendeklarasikan alias dengan "-", "_" dan ".", tetapi di file gradle, itu akan diganti dengan ".'".
#/app/build.gradle
implementation 'androidx.core:core-ktx:1.9.0'
# became
implementation libs.androidx.core.ktx
#./build.gradle
dependencyResolutionManagement {
defaultLibrariesExtensionName.set('projectLibs') # Add this line
}
#app/build.gradle
implementation 'androidx.core:core-ktx:1.9.0'
# became
implementation projectLibs.androidx.core.ktx
#/app/build.gradle
id 'com.android.application'
# became
alias(libs.plugins.android.application)
#/gradle/libs.versions.toml
[versions]
androidGradlePlugin = "7.4.0"
kotlin = "1.8.0"
androidxCore = "1.9.0"
androidxLifecycle = "2.5.1"
androidxActivity = "1.6.1"
androidxComposeUi = "1.3.3"
androidxComposeMaterial = "1.3.1"
androidxComposeCompiler = "1.4.0" # Add the compiler version
junit = "4.13.2"
androidxTestExt = "1.1.5"
espressoCore = "3.5.1"
# app/build.gradle
composeOptions {
# get the compile version
kotlinCompilerExtensionVersion libs.versions.androidxComposeCompiler.get()
}
#/app/build.gradle
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
}
Tapi, dengan groovy, ini mungkin tidak berfungsi pada awalnya, ada solusinya , tambahkan baris itu di build.gradle root Anda.
#./build.gradle
buildscript {
dependencies {
classpath files(libs.class.superclass.protectionDomain.codeSource.location)
}
}
Jadi begitulah cara saya mengelola dependensi saya.
Obs.: Itu berfungsi persis sama dengan gradle kotlin dan groovy.
Apa lagi yang bisa kita lakukan?
- Gunakan satu file dengan banyak proyek, karena kita dapat menentukan path untuk file tersebut.
dependencyResolutionManagement {
versionCatalogs {
libs {
from(files("../../mainVersionFile/libs.versions.toml"))#path to the file
#Add that config in all of your project to use one catalog to all of them
}
}
}
#/settings.gradle
#add a list of file location
versionCatalogs {
libs {
from(files("./gradle/main.libs.versions.toml"))
}
custom {
#that list od dependency will be accessed with custom prefix
#implementation custom.androidx.activity.compose
from(files("./gradle/custom.libs.versions.toml"))
}
}
#full code on branch: multi-toml-file
#/build.gradle
buildscript {
dependencies {
classpath files(libs.class.superclass.protectionDomain.codeSource.location)
#add that line
classpath files(custom.class.superclass.protectionDomain.codeSource.location)
}
}
Dan dokumentasi resmi.
https://docs.gradle.org/current/userguide/platforms.html

![Apa itu Linked List? [Bagian 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































