'ViewModel in Kotlin: Unresolved Reference

I am trying to implement ViewModel in a 100% Kotlin app. Every piece of documentation I can find says I want to use this to get the ViewModel instance:

ViewModelProviders.of(this).get(CustomViewModel::class.java)

According to the docs, I should be able to import this with:

import android.arch.lifecycle.ViewModelProviders

This import is unresolved though. I am using the following in my build file:

def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycler:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"

Why can't I access ViewModelProviders?



Solution 1:[1]

In my case I was missing :

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

Solution 2:[2]

In addition to what Sup suggested, you'll have to correct lifecycler:compiler to lifecycle:compiler - the Gradle sync shouldn't even complete successfully with this typo.

Secondly, the standard android annotation processing ("annotationProcessor") doesn't really work with Kotlin. Instead, use Kotlin's kapt.

At the top of your build.gradle file, add the following:

apply plugin: 'kotlin-kapt'.

And in your dependencies section, replace occurences of annotationProcessor (like the above one) with kapt, e.g.

kapt "android.arch.lifecycle:compiler:1.1.1"

Solution 3:[3]

ViewModelProviders is deprecated in 1.1.0

Check this android docs https://developer.android.com/topic/libraries/architecture/viewmodel

I am using 2.3.1 and add below dependency in build.gradle

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'

Declare ViewModel in your Kotlin code as per below:

private val cartViewModel: CartViewModel by viewModels()

Still, if you facing below error like

Unresolved reference: viewModels

Then need to add below below dependency in build.gradle

implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.2.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.fragment:fragment-ktx:1.3.6'

OR

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

Solution 4:[4]

I solve this by doing like this. implement this dependency

implementation "android.arch.lifecycle:extensions:1.1.1"

then call my ViewModel class

homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)

Solution 5:[5]

I faced this kind of problem in AndroidStudio 3.0.1 and solved by adding following dependencies in the proper build.gradle:

implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

version code may be different as AndroidStudio tells you if it differs.

Solution 6:[6]

If the above "adding/updating dependecies" did not resolve your issue then try to restart your android studio. It´s just to root, I do not see any major issue with it. Hope it helps.

Solution 7:[7]

Resolved with this dependency for me

 implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

Solution 8:[8]

If someone want androidx, then, first - in build.gradle (app):

implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

Second - in file with code:

import androidx.lifecycle.ViewModelProvider

And third - use it:

ViewModelProvider(this).get(CustomViewModel::class.java)

Or use it like this (if needed ViewModelFactory):

ViewModelProvider(this, CustomViewModelFactory).get(CustomViewModel::class.java)

Solution 9:[9]

Not really sure why there are soo many solutions to this problem however while none of these worked for me, I did find a solution in my case.

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"

Solution 10:[10]

This can also be resolved by targeting minSdkVersion to 21.

If you have minSdkVersion 21 or higher, you won't need implementation "android.arch.lifecycle:extensions:1.1.1".

Solution 11:[11]

android {
    kotlinOptions {
        jvmTarget = '1.8'
    } 
}

project e app level

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Louis
Solution 2 Julian Os
Solution 3
Solution 4 Md. Shofiulla
Solution 5 Ishwor Khanal
Solution 6 developer
Solution 7 Omid Zakeri
Solution 8
Solution 9 DevinM
Solution 10 WSBT
Solution 11 J. Soares