androidgradleviewmodelkoin

Android Koin - not found after update to 3.2.0


I was using Koin in my Android app. I was using koin version 3.1.5 and had the libs.versions.toml like that

koin = "3.1.5"

koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" }
koin-androidx-workmanager = { module = "io.insert-koin:koin-androidx-workmanager", version.ref = "koin" }
koin-androidx-navigation = { module = "io.insert-koin:koin-androidx-navigation", version.ref = "koin" }
koin-androidx-compose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin" }

[bundles]  
koin-android = ["koin-core", "koin-android", "koin-androidx-workmanager", "koin-androidx-compose", "koin-androidx-navigation"]

It all worked fine. In the code I was importing

import org.koin.androidx.viewmodel.koin.getViewModel

and called getViewModel without any issues.

Now I updated to version 3.2.0 but in my code I have that error now:

import org.koin.androidx.viewmodel.koin.getViewModel

Unresolved reference: koin

It seems the package org.koin.androidx.viewmodel.koin I was using before can't be found anymore.

I first assumed I needed to add more imports and added all the coin libs I could find

koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" }
koin-androidx-workmanager = { module = "io.insert-koin:koin-androidx-workmanager", version.ref = "koin" }
koin-androidx-navigation = { module = "io.insert-koin:koin-androidx-navigation", version.ref = "koin" }
koin-androidx-compose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin" }
koin-android-compat = { module = "io.insert-koin:koin-android-compat", version.ref = "koin" }
koin-androidx-viewmodel = { module = "org.koin:koin-androidx-viewmodel", version.ref = "koin" }
koin-androidx-scope = { module = "org.koin:koin-androidx-scope", version.ref = "koin" }
koin-androidx-fragment = { module = "org.koin:koin-androidx-fragment", version.ref = "koin" }
koin-androidx-lifecycle = { module = "androidx.lifecycle:lifecycle-viewmodel-savedstate", version.ref = "koin" }

but none is working. Without that my code getKoin().getViewModel(...) won't work. I also tried newer versions like 3.5.0 but had no success either.

Where is that package gone? Or if it is merged, where can I find that method?

--- EDIT ---

There seems to be a package import org.koin.androidx.viewmodel.ext.android.getViewModel that offers getViewModel but this is not compatible with my function call from the code. It still cant find getViewModel here.

getKoin().getViewModel(owner = {
  ViewModelOwner.fromAny(activity)
})

Solution

  • For everyone having the same issue,

    the solution was a bit different. So instead of

    getKoin().getViewModel(owner = {
       ViewModelOwner.fromAny(activity)
    })
    

    I had to get the viewModel directly like that:

    activity.viewModelScoped<MySharedViewModel>(getKoin().scopeRegistry.rootScope).value