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

Compose Multiplatform using koinNavViewModel() to create a ViewModel will cause a crash on Android #1926

Closed
Reach2027 opened this issue Jul 29, 2024 · 7 comments

Comments

@Reach2027
Copy link

Describe the bug
In the compose multiplatform project, using koinNavViewModel() to create a ViewModel will cause a crash on Android, it is normal on iOS and desktop.

FATAL EXCEPTION: main
Process: com.reach.kmp, PID: 6159
java.lang.NoSuchMethodError: No virtual method getArguments()Landroidx/core/bundle/Bundle; in class Landroidx/navigation/NavBackStackEntry; or its super classes (declaration of 'androidx.navigation.NavBackStackEntry' appears in /data/app/~~0V-y2iYYV0J5BlmEyyRcCQ==/com.reach.kmp-JhRe6IHzM29St0VX0yPN4A==/base.apk!classes16.dex)
at org.koin.viewmodel.CreationExtrasExtKt.defaultNavExtras(CreationExtrasExt.kt:16)
at com.reach.kmp.feature.bingwallpaper.BingWallpaperScreenKt.BingWallpaperRoute(BingWallpaperScreen.kt:213)
at com.reach.kmp.feature.bingwallpaper.BingWallpaperScreenKt.access$BingWallpaperRoute(BingWallpaperScreen.kt:1)
at com.reach.kmp.feature.bingwallpaper.ComposableSingletons$BingWallpaperScreenKt$lambda-1$1.invoke(BingWallpaperScreen.kt:56)
at com.reach.kmp.feature.bingwallpaper.ComposableSingletons$BingWallpaperScreenKt$lambda-1$1.invoke(BingWallpaperScreen.kt:55)

To Reproduce
Launch android app

Expected behavior
Create NavBackStackEntry ViewModel success on Android.

Koin module and version:
koin-compose-viewmodel:4.0.0-RC1

Snippet or Sample project to help reproduce
https://github.com/Reach2027/Kmp/blob/master/feature/bingwallpaper/src/commonMain/kotlin/com/reach/kmp/feature/bingwallpaper/BingWallpaperScreen.kt

line:62

@kmbisset89
Copy link

@arnaudgiuliani I looked into this and saw that the class should have this field. I have to figure that this would be fixed by stabilization in the navigation library.

@renaudmathieu
Copy link
Contributor

Able to reproduce on my side as well

@sayah-y
Copy link

sayah-y commented Aug 25, 2024

It's mainly due to the fact that the Navigation component library was significantly refactored in recent version and the getArguments() method was removed from ViewModelStoreOwner.

public interface ViewModelStoreOwner {

    /**
     * The owned [ViewModelStore]
     */
    public val viewModelStore: ViewModelStore
}

But, koin continues to use it:

@OptIn(KoinInternalApi::class)
fun defaultNavExtras(viewModelStoreOwner: ViewModelStoreOwner): CreationExtras = when {
    viewModelStoreOwner is NavBackStackEntry && viewModelStoreOwner.arguments != null -> viewModelStoreOwner.arguments?.toExtras(viewModelStoreOwner) ?: CreationExtras.Empty
    viewModelStoreOwner is HasDefaultViewModelProviderFactory -> viewModelStoreOwner.defaultViewModelCreationExtras
    else -> CreationExtras.Empty
}

Actually, koin-compose-viewmodel:4.0.0-RC1 cannot be use with an updated Android project.

@kmbisset89
Copy link

@sayah-y

I am looking at the documentation and the NavBackstackEntry still implements ViewModelStoreOwner and has the getArguments() function according to what I found here.

https://developer.android.com/reference/androidx/navigation/NavBackStackEntry

When I debugged the code the is NavBackstackEntry is true, is there a better place to pull documentation?

@arnaudgiuliani arnaudgiuliani added this to the 4.0-RC2 milestone Aug 27, 2024
@arnaudgiuliani arnaudgiuliani added the status:checking currently in analysis - discussion or need more detailed specs label Aug 27, 2024
@arnaudgiuliani
Copy link
Member

yes known issue on current jetbrains API

@arnaudgiuliani
Copy link
Member

waiting for JB feedback

@arnaudgiuliani arnaudgiuliani added status:accepted accepted to be developed status:wait_feedback and removed status:checking currently in analysis - discussion or need more detailed specs labels Aug 30, 2024
@arnaudgiuliani arnaudgiuliani modified the milestones: 4.0-RC3, 4.0-RC2 Aug 30, 2024
@arnaudgiuliani
Copy link
Member

Solution found in this slack thread: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1725012875898249
Fixed in RC2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants