139

Hey I'm trying to get my ViewModel working, but no luck so far. Android Studio shows error Cannot resolve symbol 'ViewModelProviders'.

Every other question I found on this topic was correcting extends Activity to extends AppCompatActivity, but I am extending the right one. Not sure what I'm missing...
My code is based on This YouTube video

MainActivity.java

public class MainActivity extends AppCompatActivity implements
    TileAdapter.TileAdapterOnClickHandler {


private BaseViewModel viewModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //set Toolbar
    Toolbar myToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);


    //initialize viewModel
    viewModel = ViewModelProviders.of(this).get(BaseViewModel.class);

BaseViewModel.java

public class BaseViewModel extends ViewModel {

private Movie[] mMovie;

public void init (Movie[] movies){
    this.mMovie = movies;
}

public Movie[] getMovie() {
    return mMovie;
}
Sheler
  • 3,969
  • 3
  • 12
  • 21
  • 1
    "Cannot resolve symbol" means that either you do not have the `import` statement, or you do but you do not have the dependency in your Gradle setup. – CommonsWare Mar 21 '18 at 11:52
  • 11
    Check do you have dependecy for **android.arch.lifecycle:extensions** in build.gradle – Muthukrishnan Rajendran Mar 21 '18 at 11:52
  • Either he is using some library and added the dependency in gradle file so he is able to import ViewModel or he has custom class `ViewModel` under the different package and he is importing it from there – Akshay Katariya Mar 21 '18 at 11:54
  • Didn't have extensions as dependency. Thanks @MuthukrishnanRajendran – Sheler Mar 21 '18 at 13:08

14 Answers14

221

I didn't have both dependencies in my build, hence the problem.

implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:viewmodel:1.1.0"

Thanks @Muthukrishnan Rajendran

Sheler
  • 3,969
  • 3
  • 12
  • 21
  • 7
    @Sheler You're answer is correct but the docs is wrong. In the documentation it is specified that ViewModel and LiveDat both are in implementation "android.arch.lifecycle:extensions:1.1.1" – BraveEvidence May 08 '18 at 11:16
  • 2
    Hmm this solution has still did not solved my issue. I have implementation 'android.arch.lifecycle:extensions:1.1.1' in my app gradle and allprojects { repositories { google() jcenter() } } Is there something else that needs added? – musterjunk Jun 26 '18 at 15:03
  • 1
    I see ViewModelProvider class but it does not have the .of() method. ViewModelProviders is still not defined. I am on a mac if that matters. – musterjunk Jun 26 '18 at 15:06
  • Is there a min sdk or java 8 definition that needs to be added to the project? – musterjunk Jun 26 '18 at 15:14
  • Never mind. Make sure you sync your gradles files if it does not show up. – musterjunk Jun 26 '18 at 15:43
  • @Sheler. Doesn't work for me. By the way my gradle files have this support library : 'com.android.support:appcompat-v7:27.1.1'. Does that affect? – zulkarnain shah Aug 03 '18 at 07:17
  • @musterjunk this is for ViewModelProviders not ViewModelProvider – Meysam Hadigheh Mar 26 '20 at 20:55
112

If you are using androidx you need this:

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
Babao
  • 1,273
  • 1
  • 8
  • 3
  • 1
    For anybody else having this problem, you also need this in your app gradle: apply plugin: 'androidx.navigation.safeargs' and this in your android gradle: classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha06" If you have trouble where to put these things, look in the android-sunflower demo app from google. – findusl Oct 24 '18 at 17:34
  • 2
    check this link for the latest androidx version https://developer.android.com/jetpack/androidx/migrate – user2301281 Feb 10 '19 at 07:12
  • 1
    What's the stupidity! ViewModelProvider is available without dependency but for ViewModelProviders we need an extra dependency. – Rahul Rastogi Oct 28 '19 at 07:15
31

android.arch.lifecycle:extensions is deprecated use

def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

Create instance of viewmodel like this:

Java

Yourclass obj = new ViewModelProvider(context).get(ClassViewModel.class);

Kotlin

var obj = ViewModelProvider(context).get(ClassViewModel::class.java)
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
15

If you are using compiled sdk version 28 or higher you only need to add single dependecy to get ViewModel and LiveData

dependencies {
    //...
    def lifecycle_version = "1.1.1"

    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
}
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
  • To know latest version of the `android.arch.lifecycle:extensions` you can search `extensions` in https://maven.google.com/web/index.html and see latest version – Sanjeev Oct 08 '19 at 06:33
  • 2
    `androidx.lifecycle:lifecycle-extensions` is deprecated https://developer.android.com/jetpack/androidx/releases/lifecycle – Jack Jun 23 '21 at 14:11
10

In my case (Android Studio 3.6.3 ~ 4.1.2), in AppCompatActivity to successfully do:

MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);

it requires both of these:

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

(alone with lifecycle-extentions is not sufficient)

Update

It seems that you have no longer to include the gradle implementation line ('androidx.lifecycle:lifecycle-extensions:2.2.0') to instantiate ViewModelProvider.

hata
  • 11,633
  • 6
  • 46
  • 69
  • `androidx.lifecycle:lifecycle-extensions` is deprecated developer.android.com/jetpack/androidx/releases/lifecycle – Jack Jun 23 '21 at 14:12
5

you should add library in your project's build.gradle

def lifecycle_version = "2.0.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
5

I solve this problem from Android official documentation. Add below to build.grale

def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
duyuanchao
  • 3,863
  • 1
  • 25
  • 16
4

In implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" and up ViewModelProviders is deprecated,use

viewModel = ViewModelProvider(this).get(BaseViewModel.class);

or in Kotlin

viewModel = ViewModelProvider(this).get(BaseViewModel::class.java);

instead of

viewModel = ViewModelProviders.of(this).get(BaseViewModel.class);
Mihai
  • 26,325
  • 7
  • 66
  • 81
3

In the build.gradle file, add these lines in the dependencies block

dependencies {
...
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
//if not using java 8,use the following line
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"
//if using java 8,ignore above line and add the following line
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
...
}

Sample Image of build.gradle file

Sulav Timsina
  • 703
  • 7
  • 20
3

-'ViewModelProviders' is deprecated now

  • Now alternative
  • In java

viewModel = ViewModelProvider(this).get(BaseViewModel.class);

  • In kotlin

var viewModel = ViewModelProvider(this).get(BaseViewModel::class.java)

Refs - https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders

aksBD
  • 189
  • 1
  • 6
2

Use androix libraries

Change

implementation 'com.android.support:appcompat-v7:28.0.0'

to

implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'

You can use

Refactor>Migrate to AndroidX
2

Works fine in my app(java) before

loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory())
                .get(LoginViewModel.class);

changes to

loginViewModel = new ViewModelProvider(this,new LoginViewModelFactory()).get(LoginViewModel.class);

hope it could help

s wang
  • 21
  • 1
2

In My Case, I am facing below issue i.e:

** cannot access androidx.lifecycle.has a default viewmodelprovider factory which is a enter image description heresubclass of your class Name, check your module classpath check your model conflicting dependencies **

I added below dependencies in my project build.gradle.

def lifecycle_version = "2.2.0"

implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

Then I create my class in a module project and I'm facing this issue, then I add these libraries in module build.gradle file and the issue is resolved.

matthias_h
  • 11,356
  • 9
  • 22
  • 40
Zeeshan Akhtar
  • 441
  • 1
  • 4
  • 9
0

I had the same problem. None of the other solutions helped me.

I realized that I was using import androidx.lifecycle.ViewModelProvider; instead of import androidx.lifecycle.ViewModelProviders;.

So make sure you are using import androidx.lifecycle.ViewModelProviders;. That is ViewModelProviders with an s.

SedJ601
  • 12,173
  • 3
  • 41
  • 59