Model Catalog is a dependency administration instrument developed by the Gradle group. The good thing about utilizing it’s you needn’t exhausting code the model in a number of locations, particularly you probably have a multi-modules mission.
This text offers a step-by-step information to integrating Model Catalog into your Android app utilizing Android Studio. The instance is predicated on this clear empty Jetpack Compose app template.
1. Improve Gradle Model
The minimal Gradle model requirement to run Model Catalog is 7.4. Nevertheless, model 7.4.2 comprises the newest fixes.
Within the terminal, test your Gradle model.
./gradlew -version
Whether it is lower than 7.4.2, improve it.
./gradlew wrapper --gradle-version 7.4.2
After working the above command, the .wrappergradle-wrapper.jar
, .wrappergradle-wrapper.properties
and .construct.gradle.kts
are up to date.
If you happen to’re interested in *.kts, it’s a Gradle Kotlin Script. For extra data, you may confer with this weblog submit.
2. Create libs.variations.toml
Create this .gradlelibs.variations.toml
file. Please be sure you title it accurately. I named it wrongly at first, and it took me some time to determine that out.
Instance of libs.variations.toml
seems like this.
[versions]
androidGradlePlugin = "7.3.0"
androidxCore = "1.8.0"
androidxComposeCompiler = "1.2.0"
[plugins]
android-application = { id = "com.android.software", model.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", model.ref = "androidGradlePlugin" }
[libraries]
androidx-core-ktx = { module = "https://vtsen.hashnode.dev/androidx.core:core-ktx", model.ref = "androidxCore" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", model.ref = "androidxLifecycle" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", model.ref = "androidxLifecycle" }
[bundles]
androidx-lifeycle = ["androidx-lifecycle-runtime-ktx", "androidx-lifecycle-viewmodel-compose"]
-
[versions]
– declare variations which can be referenced by[plugins]
and[libraries]
-
[plugins]
– outline a set of plugins with model -
[libraries]
– outline a set of library dependencies with model -
[bundles]
– mix a number of libraries right into a single reference
[bundles]
works solely with libraries. It will not work for plugins. For instance, the next will not work.
[bundles]
android-gradle-plugin = ["android-application", "android-library"]
3. Replace the plugins block
construct.gradle.kts (root / mission degree)
Substitute
plugins {
id("com.android.software") model "7.2.2" apply false
id("com.android.library") model "7.2.2" apply false
id("org.jetbrains.kotlin.android") model "1.7.0" apply false
}
with
plugins {
alias(libs.plugins.android.software) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
}
appbuild.gradle.kts (app / module degree)
Substitute
plugins {
id ("com.android.software")
id ("org.jetbrains.kotlin.android")
}
with
plugins {
alias(libs.plugins.android.software)
alias(libs.plugins.kotlin.android)
}
It compiles and runs wonderful, however you may nonetheless see the next errors.
val Mission.libs: LibrariesForLibs’ cannot be known as on this context by implicit receiver. Use the specific one if crucial
It seems like a identified bug right here. To eliminate this error, you may add @Suppress("DSL_SCOPE_VIOLATION")
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
}
4. Replace kotlinCompilerExtensionVersion
As a substitute of exhausting coding kotlinCompilerExtensionVersion
composeOptions {
kotlinCompilerExtensionVersion = "1.2.0"
}
use libs.vesions.compose
which you outlined androidxComposeCompiler
within the [versions]
part in libs.variations.toml file
composeOptions {
kotlinCompilerExtensionVersion = libs.variations.androidxComposeCompiler.get()
}
5. Replace library dependencies
Instance 1 – Single library
dependencies {
implementation("androidx.core:core-ktx:1.8.0")
}
is changed with
dependencies {
implementation(libs.androidx.core.ktx)
}
Instance 2 – A number of libraries
dependencies {
val lifeCycleVersion = "2.5.1"
implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifeCycleVersion")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifeCycleVersion")
}
are changed with
dependencies {
implementation(libs.bundles.androidx.lifeycle)
}
Present Difficulty
The key difficulty with Model Catalog in my view is it will not immediate you the message when there’s a new model of the library. For instance:
A more moderen model of androidx.core:core-ktx than 1.8.0 is on the market: 1.9.0
There’s a third-party library right here, however I believe it nonetheless requires you to manually run the Gradle command.
Supply Code
GitHub Repository: Demo_CleanEmptyCompose(master_vercatalog department)
Convert to Model Catalog: diff