ViewModel: Unresolved reference

  android, android-viewmodel, kotlin

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"

Source: Android Questions

One Reply to “ViewModel: Unresolved reference”

  • I’m following the kotlin tutorial, on the “Shared ViewModel Across Fragments” step of “Advanced navigation app examples”.

    I found that adding the following to build.gradle (:app) dependencies, re-syncing Gradle and restarting Android Studios resolved the reference issue.

    def activity_version = “1.2.0”
    def fragment_version = “1.3.0”
    implementation “androidx.activity:activity-ktx:$activity_version”
    implementation “androidx.fragment:fragment-ktx:$fragment_version”

LEAVE A COMMENT