LopsemPyier
LopsemPyier

Reputation: 751

Unresolved reference: viewModelScope - Kotlin Android

I try to add viewModelScope to a basic viewModel but android studio doesn't recognize it.

I tried to change my gradle build file with some solution I found but nothing works.

Here an extract of my build.gradle app

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"

When I type viewModelScope in my viewModel it say Unresolved reference: viewModelScope.

Upvotes: 61

Views: 48497

Answers (14)

Adel b
Adel b

Reputation: 31

Add lifecycle-viewmodel-ktx to your app/build.gradle dependency list:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"

Upvotes: 2

GiridharaSPK
GiridharaSPK

Reputation: 547

adding lifecycle-viewmodel-ktx implementation didn't work for me.

    implementation("androidx.activity:activity-ktx:1.8.0")
//    implementation("androidx.lifecycle:lifecycle-view-model-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")

Implmenting activity-ktx and lifecycle-runtime-ktx as above worked

Upvotes: 0

Hossein Zare
Hossein Zare

Reputation: 1

your ViewModel must extend androidx.lifecycle.ViewModel. if it doesn't, the Android studio doesn't recognize viewModelScope.

class RegisterViewModel() : ViewModel()  {
    fun postRegister(registerBody: RegisterBody) = viewModelScope.lunch{

    }
}

Upvotes: 2

Sagar Balyan
Sagar Balyan

Reputation: 658

In Build.gradle (App Level)

Change your code from:

def lifecycle_version = "2.0.0" or if you are using any lower version than this to:

def lifecycle_version = "2.2.0"

viewModelScope was launched in 2.2.0 version of lifecycle module so you won't find it before that.

Upvotes: 1

Ashik Lanjewar
Ashik Lanjewar

Reputation: 91

ViewModelScope only added in version 2.2.0, not available on higher versions too. I tried with 2.6.0 but got same error.

Upvotes: 0

Jayesh Suthar
Jayesh Suthar

Reputation: 59

Remove below config from build.gradle(:app)

configurations {
    all {
        exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
    }
}

Upvotes: 3

Sanket Bhangale
Sanket Bhangale

Reputation: 164

Maybe you are not extending the activityViewModel with ViewModel class

class SampleActivityViewModel: ViewModel() {

 fun getData(){
     viewModelScope.launch{
       // Make an API call
     }
   }
}

Upvotes: 5

ilker
ilker

Reputation: 61

Also check that you are in the correct file. I had the same problem for a moment and I came to this page, but later on, I realized I accidentally tried to run viewModelScope.launch on my Fragment.

viewModelScope.launch is only available in your ViewModels and lifecycleScope.launch in your lifecycle aware components.

Upvotes: 6

j2emanue
j2emanue

Reputation: 62519

for now its in alpha, so please update your gradle to use the following dependencies:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

Upvotes: 112

gotwo
gotwo

Reputation: 870

viewModelScope was introduced with release 2.1.0, see here.

Check whether lifecycle-viewmodel-ktx-2.2.0-alpha01.aar is installed. For me there is no error message with the settings you wrote. However, there is an error message when using an earlier version:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0"

But this works:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"

Upvotes: 2

lukman nudin
lukman nudin

Reputation: 428

In my case i forgot to extends ViewModel in that class, the class you use for viewModelScope must be like yourModelClass : ViewModel() in kotlin and for java yourModelClass extends ViewModel

Hope its help

Upvotes: 26

Fernando Prieto Moyano
Fernando Prieto Moyano

Reputation: 925

I've had the same issue and I've just imported: "androidx.navigation:navigation-fragment-ktx:2.2.0-rc03" "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-rc03" Even though I thought fragment-ktx was not really related. Took me a while to figure that out. Hope it helps!

Upvotes: 8

Rakhi Dhavale
Rakhi Dhavale

Reputation: 1206

For latest version of the artifact refer Maven Repository Android Lifecycle ViewModel Kotlin Extensions

In app level build.gradle file add the following :-

def lifecycle_version = "2.2.0-rc03"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

Don't forget to add apply plugin: 'kotlin-kapt' at the top of app/build.gradle file

Upvotes: 3

Cory Roy
Cory Roy

Reputation: 5609

It looks like you've got two different versions of the androidX lifecycle libraries in use.

Change your app/build.gradle to be:

...
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
...

Upvotes: 0

Related Questions