37

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

Oussèma Hassine
  • 741
  • 1
  • 7
  • 18
  • 2
    I have been running into that as well. There's a bug in an inspection or Lint rule somewhere. I filed [a bug report](https://issuetracker.google.com/issues/152245564), 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 Jun 09 '20 at 15:19
  • 2
    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 Jun 09 '20 at 18:15
  • 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. – Raj Narayanan May 17 '21 at 12:45

7 Answers7

48

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
David Pisoni
  • 3,317
  • 2
  • 25
  • 35
21

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'
...
mrtexaz
  • 663
  • 7
  • 22
16

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

Yeray
  • 1,265
  • 1
  • 11
  • 23
9

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.

maXp
  • 1,428
  • 1
  • 15
  • 23
3

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"
Eric Cen
  • 3,616
  • 1
  • 13
  • 17
0

Your androidx.lifecycle:lifecycle-XXX dependency versions must be the same.

in my case:

implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
Amir Hossein
  • 392
  • 3
  • 11
0

me too have the same problem with this ->

implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0'

I changed the version of the lifecycle-runtime-ktx dependency from:

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0'

To

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'

and now it work.

Black4Guy
  • 641
  • 11
  • 28