androidgradleandroid-livedataandroid-viewmodelandroid-mvvm

"Using ViewModel and LiveData Without Explicit Dependency Declaration in build.gradle"


I've noticed that in some of my Android projects, I'm able to access ViewModel and LiveData classes without explicitly adding the following dependencies to my build.gradle file:

implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")

I'm curious as to why this is happening. Shouldn't I need to declare these dependencies to use ViewModel and LiveData in my project? Could there be any factors or configurations that allow me to access these components?

I'd appreciate any insights into how this is possible...


Solution

  • You could get those 2 as transitive dependencies, if you included some dependecy that depends on ViewModel and LiveData. You are probably getting those when you are using some other dependencies that have ViewModel and LiveData bundled. For example, if you use "androidx.navigation:navigation-fragment-ktx", you would get those 2 bundled.

    You can easily check this with command

    ./gradlew app:dependencies