android - 无法访问 'androidx.lifecycle.HasDefaultViewModelProviderFactory' ,它是 'FavoriteBottomDialogFragment' 的父类(super class)型。检查你的模块分类

标签 android dependencies classpath androidx bottom-sheet

我在扩展 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

该类在 app 模块中,该模块实现了另外两个模块:core 和presentation-core

构建.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'
 }

核心依赖
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"
}

演示核心
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

即使出现错误,我也可以在设备中运行项目

最佳答案

我今天遇到了同样的问题,并且能够解决。
原来问题是 androidx.lifecycle:lifecycle-viewmodel 的预期版本之间的版本不匹配由“失败”类所在的模块和其他一些相关代码中的更高版本使用。

因此,就我而言,我的模块使用的是该模块的 2.1.0 版本,但其中一个依赖项使用的是 2.2.0 版本。代码编译不会有问题,因为 gradle 将依赖解析为最新版本;然而,Android Studio 不知何故被这种情况弄糊涂了(并非总是如此,因为这种情况并非一直发生,但有时——这不是我第一次看到这种情况。)

因此解决方案是:找出这个库的最新版本在你的应用程序中并更新你的build.gradle。让这个模块指向 gradle 正在解析的相同版本。或者:

  • 运行 gradlew app:dependencies
  • 搜索 lifecycle-viewmodel 的结果
  • 更新 build.gradle在您的应用程序中依赖 lifecycle-viewmodel gradle 说它正在解析为
  • 的版本
  • 将项目与 gradle 文件同步
  • 关于android - 无法访问 'androidx.lifecycle.HasDefaultViewModelProviderFactory' ,它是 'FavoriteBottomDialogFragment' 的父类(super class)型。检查你的模块分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62285843/

    相关文章:

    android - 如何吃掉android应用程序中的所有android内存?

    android - 如何在不进入设置的情况下更改我的 Android 应用程序中的本地化?

    java - 安装程序未找到 Maven 依赖项,但它位于本地存储库中

    java - Eclipse 和 Gradle 类路径导致启动缓慢

    android - 如何在 android 中重复特定星期几的闹钟

    java - 根据音高、速度和速率应用声音效果

    java - maven - 如何解决此错误 : "The POM for XXX is invalid"?

    eclipse - 用ant构建eclipse项目

    java - 当 JUnit 类具有多个导入时,如何从 Windows CMD 窗口运行 JUnit 测试?

    java - 为什么在运行以下代码时会收到 `Could not find or load main class`?