Koin: viewModel() not resolving

Created on 28 Feb 2019  ·  19Comments  ·  Source: InsertKoinIO/koin

viewModel not resolving

I am using following dependencies
org.koin:koin-android:1.0.2
org.koin:koin-androidx-scope:1.0.2
org.koin:koin-androidx-viewmodel:1.0.2

screen shot 2019-02-28 at 7 36 20 pm

question check issue

Most helpful comment

After some deep investigation I was able to find the source of the issue. The extension function "viewModel" is meant to be an extension of the androidx.lifecycle.LifecycleOwner which is an interface implemented by the class ComponentActivity when using androidx (just out of curiosity, it's SupportActivity for the support version of libs, which is fine).

So, the Koin implementation is actually correct for androidx:core on version 1.0.1 (latest stable version). However on version 1.1-alpha the interface LifecycleOwner is not implemented anymore. It could be a temporary issue because it's still on alpha.

All in all, for those who had this issue using the alpha version suggested by Android Studio, just downgrade it to version 1.0.1 and it will work fine:

implementation 'androidx.core:core:1.0.1'

@noman87 could you try it?

All 19 comments

Hey, did you registered your UserViewModel in your koin modules?

yes already did
screen shot 2019-03-02 at 4 09 10 pm

Having the same issue here. I did some investigation, and it's most likely related to the ViewModel class being on different packages.

You should list your imports. You should import the viewModel extenion funciton for injection, not the viewModel definition dsl.

You should list your imports. You should import the viewModel extenion funciton for injection, not the viewModel definition dsl.

Hi thanks for the response. I checked the libraries and managed to find out the package structure. Oddly, the import is ignored by the declaration of "viewModel". As I said, it might be related to the expected parameter being on a different package. I'll give it some further investigation to have a better comprehension.

image

After some deep investigation I was able to find the source of the issue. The extension function "viewModel" is meant to be an extension of the androidx.lifecycle.LifecycleOwner which is an interface implemented by the class ComponentActivity when using androidx (just out of curiosity, it's SupportActivity for the support version of libs, which is fine).

So, the Koin implementation is actually correct for androidx:core on version 1.0.1 (latest stable version). However on version 1.1-alpha the interface LifecycleOwner is not implemented anymore. It could be a temporary issue because it's still on alpha.

All in all, for those who had this issue using the alpha version suggested by Android Studio, just downgrade it to version 1.0.1 and it will work fine:

implementation 'androidx.core:core:1.0.1'

@noman87 could you try it?

As @xyzwilliamxyz mentioned, ComponentActivity implementation was changed from android.core:core:1.1.0-alpha0x. So I changed androidx core library to 1.0.1 and koin libraries to 1.0.2 not latest 2.0.0-rc-2. Then it worked.

  • androidx.core.app.ComponentActivity <- androidx.fragment.app.FragmentActivity <- androidx.appcompat.app.AppCompatActivity
  • androidx.core.app.ComponentActivity <- androidx.activity.ComponentActivity(This class implemented LifecycleOwner.)

Since androidx.core:1.1.0-alpha0x, there are two ComponentActivity classes in androidx.core and androidx.activity packages.

So how to config koin 2.0 to work with androidx viewmodel? I have tried lower version androidx to latest stable 1.0.2 but did not work

I have the same issue with the viewmodel from androidx.lifecycle:lifecycle-viewmodel:2.0.0@aar and the Koin injection from org.koin:koin-androidx-viewmodel:2.0.1 with by viewModel() (not resolving).
Lowering androix to 1.0.2 also didnt work.

Did you try to import "org.koin.android.viewmodel.ext.android.viewModel" manually?

FYI. This is working version combination.

appcompat : '1.0.2'
coreKtx : '1.0.2
cardview : '1.0.0'
constraintlayout : '2.0.0-beta1'
paging . : '2.1.0'
koin : '2.0.1'

Did you try to import "org.koin.android.viewmodel.ext.android.viewModel" manually?

Yes of course :D

FYI. This is working version combination.

appcompat : '1.0.2'
coreKtx : '1.0.2
cardview : '1.0.0'
constraintlayout : '2.0.0-beta1'
paging . : '2.1.0'
koin : '2.0.1'

That worked, thanks! 👍

appcompat : '1.0.2'
coreKtx : '1.0.2
cardview : '1.0.0'
constraintlayout : '2.0.0-beta1'
paging . : '2.1.0'
koin : '2.0.1'

I had everything the same except my constrainlayout was in 2.0.0-beta2.
I was pulling my hair out, then I've changed to to 2.0.0-beta1 and it started working :+1:

Just curious has this been resolved yet? Today I tried to upgrading my androidx.navigation:navigation-fragment-ktx to 2.1.0-rc01 and my viewModel() is not resolving.

I was pulling my hair out, then I've changed to to 2.0.0-beta1 and it started working 👍

Thank you, my friend ❤️

After looking at the issue deeply. I found that my androidx.core:core-ktx:1.0.2 was causing issues with viewModel() not resolving. I upgraded this to androidx.core:core-ktx:1.1.0-rc03 and everything worked. I was able to upgrade my ConstraintLayout to 2.0.0 beta2 and navigation libraries to 2.1.0 rc01.

In my case it was to update
androidx.core:core-ktx
from 1.2.0 to 1.3.2
🤷‍♂️

I've had the same problem, but I've found a definite solution.
If you manually import the following sentence, it will work perfectly and clearly!

import org.koin.androidx.viewmodel.ext.android.viewModel
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fmobus picture fmobus  ·  4Comments

ILAgent picture ILAgent  ·  3Comments

leodeleon22 picture leodeleon22  ·  4Comments

caleb-allen picture caleb-allen  ·  4Comments

AHarazim picture AHarazim  ·  3Comments