35
votes

I am getting error in the whole class that extends BottomSheetDialogFragment

Cannot access 'androidx.lifecycle.HasDefaultViewModelProviderFactory' which is a supertype of 'FavoriteBottomDialogFragment'. Check your module classpath for missing or conflicting dependencies

enter image description here

enter image description here

The class is in app module and this module implements two other modules : core and presentation-core

build.gradle

dependencies {

     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation project (':core')
     implementation project (':presentation-core')

     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41"
     implementation 'androidx.appcompat:appcompat:1.1.0'
     implementation 'androidx.core:core-ktx:1.2.0'
     testImplementation 'junit:junit:4.12'
     implementation "com.google.android.material:material:1.1.0"

     //Rx
     implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
     implementation "io.reactivex.rxjava2:rxjava:2.2.9"
     //Architecture component
     implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
     implementation 'androidx.room:room-runtime:2.0.0'
     kapt 'androidx.room:room-compiler:2.0.0'
     kapt 'androidx.lifecycle:lifecycle-common-java8:2.0.0'
     implementation 'androidx.room:room-rxjava2:2.0.0'
     implementation 'androidx.room:room-guava:2.0.0'

     implementation 'androidx.recyclerview:recyclerview:1.1.0'
     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
 }

core dependencies

dependencies {
     api fileTree(dir: 'libs', include: ['*.jar'])
     api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61"
     api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
     api 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
     //library to serialize Java Objects between Contexts
     implementation 'org.parceler:parceler-api:1.1.11'
     kapt 'org.parceler:parceler:1.1.11'

     //testing dependencies
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     testImplementation "org.mockito:mockito-core:2.24.5"
     androidTestImplementation "org.mockito:mockito-android:2.24.5"

     //architecture component
     implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
     implementation "androidx.lifecycle:lifecycle-livedata:2.0.0"

     //RxJava2
     implementation "io.reactivex.rxjava2:rxjava:2.2.9"
     implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
}

presentation-core

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
}

BottomSheetDialogFragment enter image description here

Even with errors i got, I can run the project in a device

5
I have been running into that as well. There's a bug in an inspection or Lint rule somewhere. I filed a bug report, which Google declined. For some book samples, I wound up upgrading to the latest pre-release 1.2.0 edition of appcompat to get rid of the problem. - CommonsWare
Yes, it seems somthing like that. upgrading appcompat and com.google.android.material:material to 1.3.0-alpha01 fix the issue - Oussèma Hassine
I also got rid of this error message by upgrading appcompat to the latest version which is 1.3.0-rc01. But, I didn't have to upgrade the material library. - rajndev

5 Answers

42
votes

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:

  1. run gradlew app:dependencies
  2. search the result for lifecycle-viewmodel
  3. update build.gradle in your app to depend on lifecycle-viewmodel with the version that gradle says it is resolving to
  4. Sync project with gradle files
19
votes

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'
...
14
votes

In my case, the problem was solved adding the lifecycle viewmodel dependency in the module of the troubled class.

8
votes

Add dependency into you build gradle androidx.fragment:fragment-ktx:x.x.x with actual version. For activity you must add androidx.activity:activity-ktx:x.x.x. I hope this helps you, in my case it solved the problem.

2
votes

I added the following lines to build.gradle to solve the same issue in a module:

def archLifecycleVersion = '2.2.0'
implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$archLifecycleVersion"