40

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?

rafa
  • 1,319
  • 8
  • 20
RedBassett
  • 3,469
  • 3
  • 32
  • 56

12 Answers12

47

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.

MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
Supriya
  • 1,940
  • 24
  • 23
11

In my case I was missing :

implementation "androidx.fragment:fragment-ktx:1.1.0"
Louis
  • 1,913
  • 2
  • 28
  • 41
6

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'
Pratik Dodiya
  • 2,337
  • 1
  • 19
  • 12
  • All the other answer spam did not work. The real solution was to provide `// Main val main_version = "1.4.0" implementation("androidx.activity:activity-ktx:$main_version") implementation("androidx.fragment:fragment-ktx:$main_version")`. Thank you. – Akito Nov 18 '21 at 15:17
4

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"

Julian Os
  • 281
  • 1
  • 15
  • I figured this suggestion would show. I did test this, but kept that separate since it didn't fix the unresolved reference issue. Thanks for the not, and the typo catch (which did sync for some reason). – RedBassett Apr 20 '18 at 06:27
2

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)
Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
2

Resolved with this dependency for me

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

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.

Ishwor Khanal
  • 1,312
  • 18
  • 30
1

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.

developer
  • 81
  • 8
1

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)
Kurowsky
  • 154
  • 1
  • 9
1

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"
DevinM
  • 1,112
  • 1
  • 12
  • 29
  • The original question and answer are old enough that the dependency coordinates and contents have changed since they were posted, and folks have contributed answers over time that are relevant to different causes of the same general issue. I'm glad you found a solution to the issue as it stands for you now, and I might recommend posting your own question and answer for this problem to better reflect the current way the dependencies are set up! – RedBassett May 22 '22 at 04:31
0

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".

WSBT
  • 33,033
  • 18
  • 128
  • 133
0
android {
    kotlinOptions {
        jvmTarget = '1.8'
    } 
}

project e app level

J. Soares
  • 99
  • 4