7

I am working on Android ViewModel architecture component but I am getting the above mentioned error when trying to initialize a ViewModel in an AppCompatActivity.

import android.arch.lifecycle.ViewModelProviders;
ViewModelProviders.of(this).get(CounterViewModel.class);

There are a few questions and articles related to this, and they pointed towards adding the lifecycle:extensions and lifecycle:viewmodel dependencies in the app gradle file, but I am still getting the error.

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

The package android.arch.lifecycle does not contain the class ViewModelProviders and it only has ViewModelProvider class.

What else needs to be added to access the ViewModelProviders class?

Edit :

Dependencies in app/build.gradle:

dependencies {
    implementation project(':lifecycle')
    implementation project(':base')
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}

enter image description here

ashwin mahajan
  • 1,654
  • 5
  • 27
  • 50
  • check this https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders. at the top right mentions adding extensions dependency should work. So it should work in your case – Raghunandan Jun 05 '18 at 12:29
  • @Raghunandan Like I have mentioned I have added the dependencies already, it is still not showing up in the lifecycle package. Thanks – ashwin mahajan Jun 05 '18 at 12:39
  • 1
    and of course you have `import android.arch.lifecycle.ViewModelProviders;` in your imports – pskink Jun 05 '18 at 12:50
  • @pskink No it is not there even after adding the dependencies. – ashwin mahajan Jun 05 '18 at 12:52
  • you dont have `import android.arch.lifecycle.ViewModelProviders;` in your code? – pskink Jun 05 '18 at 12:53
  • I do have it in my code which is where it was throwing the error, I have added it in the question as well now. Thank you. – ashwin mahajan Jun 05 '18 at 12:55
  • - For both ViewModel and LiveData there is a new version 2.0.0 you can add it to your app build.gradle file using this line:
    `implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'` - also you can watch the available versions of any google lib here:
    https://dl.google.com/dl/android/maven2/index.html
    you can expand any lib. for versions and sub-modules
    – Mohammad Desouky Jan 14 '19 at 11:56

4 Answers4

14

android.arch.lifecycle:extensions:1.1.1 definitely has android.arch.lifecycle.ViewModelProviders. You can see it in Android Studio, if you open the "External Libraries" portion of the project tree and examine the library contents:

android.arch.lifecycle:extensions:1.1.1 Contents

Some possible reasons for not finding the import include:

  • You have implementation "android.arch.lifecycle:extensions:1.1.1" in the wrong place (it should be in the dependencies closure of the module's build.gradle file, such as app/build.gradle)

  • You did not sync Android Studio with the Gradle build files (you are normally prompted to do this, but you can do it manually from File > Sync Project with Gradle Files from the Android Studio main menu)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, the file is present under the external libraries. And I have followed the two instructions, but ViewModelProviders is still not showing up. Thank you. – ashwin mahajan Jun 05 '18 at 13:22
  • @ashwinmahajan: Perhaps you have a typo. Go into "External Libraries" and fold open the `android.arch.lifecycle:extensions` line as I have in the screenshot. Right-click over the `ViewModelProviders` entry and choose "Copy Reference" from the context menu. In your list of imports, add an `import ` starting point, then paste in the `android.arch.lifecycle.ViewModelProviders` that you should get from the "Copy Reference" menu item. Add a `;` at the end, and you should wind up with a valid `import` statement for this class. If that gives you an error, add a screenshot of it to your question. – CommonsWare Jun 05 '18 at 13:30
  • You can refer to the screenshot I have added. – ashwin mahajan Jun 05 '18 at 13:40
  • 1
    @ashwinmahajan: You have three modules (at least) in this project. Are you sure that this class is from the module that has the `extensions` dependency? For example, this activity is in the `:lifecycle` module, perhaps you do not have the `extensions` dependency in the `build.gradle` for `:lifecycle`. – CommonsWare Jun 05 '18 at 13:42
  • Yes, that was the problem, I had it under different module. Thank you very much for helping me. – ashwin mahajan Jun 05 '18 at 13:44
2

You do not need both lifecycle:extensions and lifecycle:viewmodel in your build.gradle file, remove

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

and it should be fine now. Also, you may want to migrate to AndroidX and use the 2.0.0 versions of the library.

EAM
  • 361
  • 2
  • 5
2

ViewModelProviders is now deprecated. Use ViewModelProvider instead.

Ian
  • 324
  • 5
  • 12
0

If you are configuring in libary, you can modify the implementation to api

lyy
  • 21
  • 3