`by viewModels()` not resolving

by viewModels() not resolving in Android Studio

I have the following dependencies in build.gradle(Module: GeoQuiz:app)

implementation 'androidx.activity:activity-ktx'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx'

I have found lots of conflicting info regarding the correct dependencies to use here when I googled for more info. Advice is welcome. Thanks.

Make sure that you have synced your changes with Gradle. If Android Studio still can’t find the import after that, you can add it in manually. It is:

import androidx.activity.viewModels

Thanks, but I still cannot get this to work. Curious if others are encountering this same issue while working through the book.

What versions of those libraries are you using? The Gradle code that you provided at the beginning is missing the versions of each library. The full lines should be:

implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'

Thank you! All good now.

1 Like