Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewModel() not resolving #383

Closed
noman87 opened this issue Feb 28, 2019 · 21 comments
Closed

viewModel() not resolving #383

noman87 opened this issue Feb 28, 2019 · 21 comments
Labels
question Usage question status:checking currently in analysis - discussion or need more detailed specs type:issue

Comments

@noman87
Copy link

noman87 commented Feb 28, 2019

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

@Hiosdra
Copy link
Contributor

Hiosdra commented Mar 2, 2019

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

@noman87
Copy link
Author

noman87 commented Mar 2, 2019

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

@xyzwilliamxyz
Copy link

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

@erickok
Copy link
Contributor

erickok commented Mar 6, 2019

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

@xyzwilliamxyz
Copy link

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

@xyzwilliamxyz
Copy link

xyzwilliamxyz commented Mar 6, 2019

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?

@uOOOO
Copy link
Contributor

uOOOO commented Apr 9, 2019

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.

@uOOOO
Copy link
Contributor

uOOOO commented Apr 10, 2019

  • 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.

@victory1908
Copy link

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

@arnaudgiuliani arnaudgiuliani added question Usage question status:checking currently in analysis - discussion or need more detailed specs type:issue labels May 14, 2019
@haannnees
Copy link

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.

@pitoszud
Copy link

pitoszud commented Jul 4, 2019

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

@uOOOO
Copy link
Contributor

uOOOO commented Jul 5, 2019

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'

@haannnees
Copy link

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! 👍

@Szczypiorek44
Copy link

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 👍

@jgebbeken
Copy link

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.

@hamen
Copy link

hamen commented Aug 23, 2019

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

Thank you, my friend ❤️

@jgebbeken
Copy link

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.

@tajchert
Copy link
Contributor

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

@H43RO
Copy link

H43RO commented Jan 26, 2021

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

@raphael-dario
Copy link

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

yes, it does work. although I would have preferred an automatic import. I have to do this manually in all my fragments/activities.

@sanjeev-developer
Copy link

implementation "androidx.activity:activity-ktx:1.3.1"

just implement this lib and problem solve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question status:checking currently in analysis - discussion or need more detailed specs type:issue
Projects
None yet
Development

No branches or pull requests