Home > Blockchain >  New Project on Android Studio with Login Activity throws error
New Project on Android Studio with Login Activity throws error

Time:10-03

My goal is to make a simple app with Android Native. However, the Android Studio throws error Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option on file app/java/com.x.x/ui/login/LoginViewModelFactory on line 12. This happens when I tried to run the app to the virtual device.

The culprit is the ViewModelProvider.Factory from the androidx.lifeccle.ViewModelProvider. I tried to see the code with right-click Go To Declaration or Usages. However, the I don't see the @JvmDefaults on top of the interface Factory declaration.

What I've did:

  1. I installed Android Studio on Mac Mini M1, I believe a month ago.
  2. I installed Java JDK 8 and 17. The Android Studio terminal return version 8.
    openjdk version "1.8.0_292"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
    
  3. Create New Project, I selected Kotlin and Login Activity on the menu.

LoginViewModelFactory

package com.x.x.ui.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.x.x.data.LoginDataSource
import com.x.x.data.LoginRepository

/**
 * ViewModel provider factory to instantiate LoginViewModel.
 * Required given LoginViewModel has a non-empty constructor
 */
class LoginViewModelFactory : ViewModelProvider.Factory {

CodePudding user response:

  1. Try putting these line inside of android{} block in module build.gradle file

     kotlinOptions{
         freeCompilerArgs  = [
            "-Xjvm-default=all",
         ]
    }
    
  • Related