Model Catalog is a dependency administration instrument developed by the Gradle group. The good thing about utilizing it’s you needn’t laborious code the model in a number of locations, particularly in case you have a multi-modules undertaking.
This text supplies 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 incorporates the newest fixes.
Within the terminal, examine 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.
Should you’re interested in *.kts, it’s a Gradle Kotlin Script. For extra info, you may confer with this weblog submit.
2. Create libs.variations.toml
Create this .gradlelibs.variations.toml
file. Please ensure you title it accurately. I named it wrongly at first, and it took me some time to determine that out.
An instance of libs.variations.toml
appears to be like 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 = "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 might 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 / undertaking 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 superb, however you may nonetheless see the next errors.
val Challenge.libs: LibrariesForLibs’ cannot be referred to as on this context by implicit receiver. Use the express one if obligatory
It appears to be like like a identified bug right here. To eliminate this error, you may add @Suppress("DSL_SCOPE_VIOLATION")
[Updated – Jan 2, 2023]: Should you get the “Anticipating an expression” construct error, you need to add this
println("")
on the finish of the file as a workaround.
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
}
println("")
4. Replace kotlinCompilerExtensionVersion
As an alternative of laborious 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 Concern
The key subject 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 accessible: 1.9.0
There’s a third-party library right here, however I feel it nonetheless requires you to manually run the Gradle command.
Supply Code
GitHub Repository: Demo_CleanEmptyCompose(master_vercatalog department)
Convert to Model Catalog: diff