Koin.error.NoBeanDefFoundException: No definition found for ViewModel class. Check your definitions

class LoginFragment : Fragment() {

private lateinit var binding: FragmentLoginBinding

private val viewModel : LoginViewModel by lazy {initViewModel()}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    viewModel
    // Inflate the layout for this fragment
    binding = FragmentLoginBinding.inflate(inflater, container, false)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    initListeners()
    spannableClickListener()
}

private fun initViewModel(): LoginViewModel {
    return getViewModel(clazz = LoginViewModel::class)
}

I have inject ViewModels in viewModelsModule:

val viewModelsModule = module{
   viewModel { MovieViewModel(get()) }
   viewModel { AboutMovieViewModel(get()) }
   viewModel { LoginViewModel() }
   viewModel { RegistrationViewModel(get()) }

}

this is my App :

class Application : Application() {
  override fun onCreate() {
    super.onCreate()
    Timber.plant(Timber.DebugTree())

    startKoin {
        androidLogger(Level.NONE)
        androidContext([email protected])
            remoteDataSourceModule
            repositoriesModule
            viewModelsModule
            databaseModule
    }
}

}

dependencies(app/build.gradle)

// Koin AndroidX Scope features
implementation "org.koin:koin-androidx-scope:2.2.1"
// Koin AndroidX ViewModel features
implementation "org.koin:koin-androidx-viewmodel:2.2.1"
// Koin AndroidX Fragment features
implementation "org.koin:koin-androidx-fragment:2.2.1"
// Koin AndroidX WorkManager
implementation "org.koin:koin-androidx-workmanager:2.2.1"

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.leskov.moviedb, PID: 14528
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for class:’com.leskov.moviedb.views.auth.login.LoginViewModel’. Check your definitions!
at org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:282)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:251)
at org.koin.core.scope.Scope.get(Scope.kt:204)
at org.koin.androidx.viewmodel.factory.StateViewModelFactory.create(StateViewModelFactory.kt:21)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at org.koin.androidx.viewmodel.ViewModelResolverKt.get(ViewModelResolver.kt:23)
at org.koin.androidx.viewmodel.ViewModelResolverKt.resolveInstance(ViewModelResolver.kt:12)
at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:86)
at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:72)
at org.koin.androidx.viewmodel.koin.KoinExtKt.getViewModel(KoinExt.kt:41)
at org.koin.androidx.viewmodel.ext.android.FragmentExtKt.getViewModel(FragmentExt.kt:71)
at org.koin.androidx.viewmodel.ext.android.FragmentExtKt.getViewModel$default(FragmentExt.kt:69)
at com.leskov.moviedb.views.auth.login.LoginFragment.initViewModel(LoginFragment.kt:75)
at com.leskov.moviedb.views.auth.login.LoginFragment.access$initViewModel(LoginFragment.kt:20)
at com.leskov.moviedb.views.auth.login.LoginFragment$viewModel$2.invoke(LoginFragment.kt:24)
at com.leskov.moviedb.views.auth.login.LoginFragment$viewModel$2.invoke(LoginFragment.kt:24)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.leskov.moviedb.views.auth.login.LoginFragment.getViewModel(LoginFragment.kt:24)
at com.leskov.moviedb.views.auth.login.LoginFragment.onCreateView(LoginFragment.kt:30)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 14528 SIG: 9

Source: Android Questions

LEAVE A COMMENT