Why am I getting an "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option" error. How can I fix this?

Viewed 99

What I am trying to do is inherit the ViewModel class in my ViewModelFactory class:

class FavDishViewModel (private val repository: FavDishRepository) : ViewModel(){
    fun insert(dish : FavDish) = viewModelScope.launch {
    repository.insertFavDishData(dish)
  }
}

class FavDishViewModelFactory(private val repository : FavDishRepository) : 
    ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass : Class<T>) : T
    {
      if(modelClass.isAssignableFrom(FavDishViewModel::class.java)){
        @Suppress("UNCHECKED_CAST")
        return FavDishViewModel(repository) as T
    }
    throw IllegalArgumentException("Unknown ViewModel Class")
  }
}

However, I am encountering this error when I run the program and I don't know how to fix it:

e: C:\..\FavDishViewModel.kt:(19, 7): Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option
1 Answers

You can add kotlin compiler argument for jvm-default.

build.gradle

kotlinOptions {
    // ...
    freeCompilerArgs += "-Xjvm-default=all"
}

// or 
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
  // ...
  kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
}
Related