koin icon indicating copy to clipboard operation
koin copied to clipboard

Unresolve reference viewModule() when updating from 3.1.5 to 3.1.6+

Open andrey2ag opened this issue 2 years ago • 2 comments

Describe the bug After updating from v3.1.5 to v3.1.6 lazy initialization by viewModel() shows an error. The error still shows up in version 3.2.0.

Screen Shot 2022-08-22 at 10 27 46

To Reproduce Steps to reproduce the behavior:

  1. Update Project's Koin library from v3.1.5 to v3.1.6

Expected behavior Delegate initialization to not be recognized as an error.

Koin project used and used version (please complete the following information):

Working Fine

implementation "io.insert-koin:koin-android:3.1.5" 

Showing error

implementation "io.insert-koin:koin-android:3.1.6" // Issue Introduced
implementation "io.insert-koin:koin-android:3.2.0" // Still Existing

Additional moduleDefinition Add any other moduleDefinition about the problem here.

module {
    viewModel {
        SignUpViewModel(Dependencies(get(), get()))
    }
}

andrey2ag avatar Aug 22 '22 16:08 andrey2ag

Did you try import the extension? Seems to be an IDE thing 🤔

arnaudgiuliani avatar Aug 26 '22 15:08 arnaudgiuliani

yes the extension is imported, this is happening in any project instance and for other people in my team - We did check but it's weird to see that behavior by doing a minor lib update. 3.1.5 -> 3.1.6 or 3.1.5 -> 3.2.0

I'll check again but do you have any other insight about it?

andrey2ag avatar Aug 30 '22 18:08 andrey2ag

I was able to replicate this behavior after I added Koin as a dependency to a project that was halfway underway. It seems to be fixed by performing an Invalidate caches/Restart. After I did that, I was able to import the viewModel() extension function. This is on Android Studio Dolphin 2021.3.1

drawers avatar Oct 08 '22 06:10 drawers

I think the issue may be the change from: inline fun <reified T : ViewModel> ComponentCallbacks.viewModel( to inline fun <reified T : ViewModel> ComponentActivity.viewModel(

This appears to cause the issue with activities that are based on AppCompatActivity?
Which used to not have an issue in 3.15

it looks like by Inject() may work for these activities though since it uses ComponentCallbacks not the ComponentActivity?

There appears to be some sort of ambiguity of the ViewModel extensions for ComponentActivity and Fragment for AppCompatActivity that didn't seem to exist in 3.15 when the activity used the ComponentCallbacks extension?

It's weird because it compiles fine, it just can't seem to auto import the right extension and if you manually import org.koin.androidx.viewmodel.ext.android and click on the by viewModel() it can't figure out which declaration to go to.

Still happens in 3.2.2 - Invalidate caches/Restart doesn't seem to affect it.

@arnaudgiuliani

jt-gilkeson avatar Oct 18 '22 08:10 jt-gilkeson

This may be related to this bug in AndroidStudio/IntelliJ: https://issuetracker.google.com/u/1/issues/178403178#comment17 somehow AndroidStudio gets confused about the Fragment unless you do the resolution strategy outlined in the comment which seems to fix the errors (the IDE can now resolve the methods). The changes in 3.2.0 seem to have exposed this problem with the wayby viewmodel()was changed.

jt-gilkeson avatar Jan 25 '23 10:01 jt-gilkeson

Great, thanks for the investigation @jt-gilkeson 👍

arnaudgiuliani avatar Jan 27 '23 16:01 arnaudgiuliani

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 26 '23 17:06 stale[bot]