Search code examples
kotlinviewmodel

Can't find ViewModelProviders class, only ViewModelProvider


I am trying to create lifecycle-aware view models. But I can't find ViewModelProviders class in my Android project, only ViewModelProvider. There seems to be no android.arch.lifecycle.ViewModelProviders package for me to import as well. What's happening


Solution

  • You probably have this dependency included in your project:

    implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"
    

    That contains ViewModelProvider (and just 4 other classes), but ViewModelProviders is in a different package:

    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    

    Here are the contents of these packages for reference (as of version 1.1.1):

    The contents of the extensions and viewmodel packages


    For the record, you can find this out yourself by looking up the docs for the ViewModelProviders class, where it says up top:

    added in version 1.1.0

    belongs to Maven artifact android.arch.lifecycle:extensions:1.1.1