Cannot access 'androidx.lifecycle.HasDefaultViewModelProviderFactory' which is a supertype of 'FavoriteBottomDialogFragment'. Check your module cla
I experienced this exact same problem today, and was able to solve.
Turns out the issue was a version mismatch between the expected version of androidx.lifecycle:lifecycle-viewmodel
used by the module where the "failing" class is and a later version in some other dependent code.
So in my case, my module was using version 2.1.0 of this module, but one of the dependencies was using version 2.2.0. The code will compile no problem, because gradle resolves the dependency to the latest version; however Android Studio is somehow confused by this situation (not always, because this doesn't happen all the time, but sometimes – this isn't the first time I've seen this.)
Therefore solution is: figure out what the latest version of this library is in your app and update your build.gradle
for this module to point to the same version that gradle is resolving to. Or:
- run
gradlew app:dependencies
- search the result for
lifecycle-viewmodel
- update
build.gradle
in your app to depend onlifecycle-viewmodel
with the version that gradle says it is resolving to - Sync project with gradle files
In my case, the problem was solved adding the lifecycle viewmodel dependency in the module of the troubled class.
in my case (an Android module), Android Studio 4.0.1, I get a number of warnings in the IDE related to androidx.lifecycle.HasDefaultViewModelProviderFactory, I have solved the issue by adding this line to build.gradle:
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
thus the beginning of build.gradle becomes:
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
...