a full stack enthusiast interested in Javascript / Typescript / Kotlin / Python / React / React Native / (Android & iOS) native / Cloud


Why unresolved reference for ViewModelProviders

I encountered a weird thing today. In Android Studio 3.1.1, import android.arch.lifecycle.ViewModelProvider is totally fine while import android.arch.lifecycle.ViewModelProviders is not fine. Because there is no ViewModelProviders under lifecycle package.

The solution is to add the packages by yourself. It doesn’t even get mentioned in the official doc and most of the tutorials online. And after working with some already setup project. It finally bites me.

Just add these 2 lines to the proper build.gradle:

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

The version number maybe different in the future but it’s fine, because Android Studio will tell the version number for the latest version.

Hope it helps.

Thanks for reading!

Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.