Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access 'androidx.lifecycle.HasDefaultViewModelProviderFactory' which is a supertype of 'FavoriteBottomDialogFragment'. Check your module cla

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

like image 863
Oussèma Hassine Avatar asked Jun 09 '20 15:06

Oussèma Hassine


3 Answers

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
like image 150
David Pisoni Avatar answered Nov 18 '22 22:11

David Pisoni


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'
...
like image 21
mrtexaz Avatar answered Nov 18 '22 22:11

mrtexaz


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

like image 15
Yeray Avatar answered Nov 18 '22 20:11

Yeray