Error Message I cant resolve: "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"

I’m new to android development and I’m currently building
my first real app. I’m trying to implement a MVVM architecture
and because of that I’m having a viewModel for each fragment and
each viewModel has a viewModelFactory. At least this is how I
understood it has to be.

I use the boilerplate code everyone seems to use for the factory:

    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")
    }
}

Now the Problem is, that the Compiler is giving me the following error:

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

And this error is produced by the viewModelFactory class I have implemented in the viewModel.
I realy can’t tell what this means and I cant find anything helpfull or even
related to my specific problem. I basically followed some basic tutorials
about creating your first app but I keep on running into errors like this.
In most cases I was able to fix the problem by myself but this time its different.

I know that a lot of you have a lot of experience and knowledge, so I hope
that some of you find the time to help me and give me a hint what I can do
to fix this.

If you need anymore information about my project feel free to ask and I’ll
provide it right away.

Thanks in advance and best regards

>Solution :

It seems like you are either directly or indirectly (through some other library) depending on Lifecycle 2.5.0-alpha01.

As per 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

Leave a Reply