android - Dagger-Hilt: @ViewModelInject is not injecting MyViewModel and crash? - Stack Overflow
Libraries provide dependencies (reusable code instead of you coding the code functionality from scratch).
Plugins extend (add extra functionality similar to Kotlin extension functions) the build system like the Gradle. Plugins enable specific build features, they can modify the build process and also add new gradle tasks.
For example, the Hilt plugin configures the project to use Hilt for dependency injection and generates the necessary code to use during the build process. Hilt Library on the other hand is just a dependency.
Not all libraries require plugins though - apart from Hilt, other libraries like Kotlin (yes Kotlin), Room, Compose, KSP, Parcelize etc may need the plugin set up if they need to interact or modify the build process or add custom gradle tasks
That said here is the new approach for Hilt Plugin and Hilt Library using Version Catalog.
gradle.build.kts [ Project ]
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
alias(libs.plugins.hilt.plugin) apply false
alias(libs.plugins.ksp.plugin) apply false
alias(libs.plugins.compose.compiler) apply false
}
gradle.build.kts [ app ]
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.hilt.plugin)
alias(libs.plugins.ksp.plugin)
}
dependencies { ...
//Dagger Hilt
implementation(libs.hilt.android)
ksp(libs.hilt.compiler)
implementation(libs.hilt.navigation.compose)
}
Version Catalog - libs.versions.toml
[versions]
agp = "8.5.1"
kotlin = "2.0.0"
ksp = "2.0.0-1.0.23"
hilt = "2.51"
androidx-hilt-navigation-compose = "1.0.0"
[libraries]
hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }
hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "androidx-hilt-navigation-compose" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
hilt-plugin = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
ksp-plugin = { id = "com.google.devtools.ksp", version.ref = "ksp" }