I am attempting to integrate some of the features presented here in my kotlin android app. I am able to use the viewModelScope fine in my ViewModels, although this only works when including implementation "androidx.fragment:fragment-ktx:1.1.0-alpha09" in my dependencies.
However, I am completely unable to use the liveData builder function highlighted in this section. When attempting to use liveData within my ViewModel, I get an unresolved reference error.
I have attempted the solution in this thread, but that doesn't solve my problem.
This is the relevant section of my build.gradle file with all of the dependencies related to ViewModel and LiveData
def lifecycle_version = "2.2.0-alpha01"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01"
implementation "androidx.fragment:fragment-ktx:1.1.0-alpha09"
It's worth mentioning again that I can't use viewModelScope either without including implementation "androidx.fragment:fragment-ktx:1.1.0-alpha09" in my dependencies.
Any help is greatly appreciated as I've done a bit of searching but can't find much as this is a relatively recent release. Thanks!
So, it seems like the
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
line doesn't work here nor does
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version",
and we need to declare dependencies for the kotlin versions of ViewModel and LiveData individually. The following is the updated dependencies section for ViewModel and LiveData (note the absence of the fragment dependency as well!)
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
With the above I am now able to use viewModelScope and the liveData builder function mentioned in the question post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With