android - 如何解决错误消息 : "Inheritance from an interface with ' @JvmDefault' members is only allowed with -Xjvm-default option"

标签 android android-studio kotlin

我是 Android 开发新手,目前正在构建
我的第一个真正的应用程序。我正在尝试实现 MVVM 架构
正因为如此,我为每个 fragment 都有一个 viewModel 和
每个 viewModel 都有一个 viewModelFactory。至少,我是这样的
明白它必须是。
我使用每个人似乎都用于工厂的样板代码:

class ExampleViewModelFactory(private val exampleDao: ExampleDao) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(exampleViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return ExampleViewModel(exampleDao) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}
现在的问题是,编译器给了我以下错误:

e: C:\Users\ ...\ExampleViewModel.kt: (64, 7): Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option


这个错误是由我在viewModel中实现的viewModelFactory类产生的。我真的不知道这意味着什么,我找不到任何有用的东西,甚至找不到与我的具体问题相关的东西。我基本上遵循了一些关于创建你的第一个应用程序的基本教程,但我不断遇到这样的错误。在大多数情况下,我能够自己解决问题,但这次不同。
我知道你们中的很多人都有很多经验和知识,所以我希望你们中的一些人能抽出时间来帮助我,给我一个提示,我可以做些什么来解决这个问题。

最佳答案

取决于 Lifecycle 2.5.0-alpha01 ,您似乎是直接或间接(通过其他库) .
根据 this issue :

You need to temporarily add following to your build.gradle:

tasks.withType(KotlinCompile).configureEach {
    kotlinOptions {
        freeCompilerArgs += [
                "-Xjvm-default=all",
        ]
    }
}

Note that in certain circumstances you may want to use all-compatibility instead of all, learn more about that in Jebrain's blogpost.

Starting with kotlin 1.6.20 you won't need touch build.gradle for more information see KT-47000

关于android - 如何解决错误消息 : "Inheritance from an interface with ' @JvmDefault' members is only allowed with -Xjvm-default option",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70992947/

相关文章:

lambda - 对理解 lambda 和接收器感到困惑

Android Room,如何保存一个实体,其中一个变量是密封的类对象

android - 在android中显示图像和视频

android - 更新 Android Studio 4.1.2 到 2020.3.1 失败

android - 如何查找两个 apk 是否使用相同的 key 签名?

android - 同一 xml 中的选择器、图层列表和形状/位图

android-studio - Android Studio gradle 的 "calculate task graph"耗时太长

java - 整数 n 作为 List<String>.sublist(fromIndex, toIndex) 的第二个参数传递,但堆栈跟踪显示 Kotlin 中的 toIndex 是 n + 2

android - 在 Sqlite android 中插入整数值

android - 如何在 Android 中绘制圆圈并在触摸时移动/拖动它