Search code examples
androidkotlinandroid-viewmodel

ViewModel: Unresolved reference


I want to implement ViewModel into my app, but I cant import viewModels() or activityViewModels(). Trying to follow tutorial but its still unresolved reference for me.

private val userViewModel: ProfileFlowFragment.UserViewModel by viewModels()

Imports:

implementation "androidx.fragment:fragment-ktx"
implementation "androidx.activity:activity-ktx"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"

Solution

  • Did you specify the version in implementation "androidx.fragment:fragment-ktx" ? Anyway, I have these dependencies in my Gradle file, and everything is fine.

     implementation "androidx.core:core-ktx:$corektxVersion"
     implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_stdlibVersion"
    

    Also add

    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'