ViewModel in Kotlin: Unresolved Reference
Solution 1
Include the following as a dependency:
implementation "android.arch.lifecycle:extensions:1.1.1"
The equivalent AndroidX dependency is:
"androidx.lifecycle:lifecycle-extensions:VERSION"
in where VERSION can be replaced for Current Stable, Beta or Alpha values given in this official link
This dependency is for both ViewModel and LiveData and thus would not require you to give separate dependencies for the same either; i.e. the first two dependencies indicated by you can be replaced by the aforementioned lifecycle extensions dependency.
Solution 2
In my case I was missing :
implementation "androidx.fragment:fragment-ktx:1.1.0"
Solution 3
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 4
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 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.
![RedBassett](https://i.stack.imgur.com/WdFnA.jpg?s=256&g=1)
Comments
-
RedBassett over 2 years
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
?