Cannot resolve symbol ViewModelProviders on AppCompatActivity

If you are using androidx you need this:

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

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


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)