Koin: Crash in Jetpack Compose alpha07 (but not alpha06)

Created on 12 Nov 2020  路  4Comments  路  Source: InsertKoinIO/koin

Describe the bug
Crash when calling getViewModel from Composable.

Crash Log
java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/runtime/SlotTable$Companion; in class Landroidx/compose/runtime/SlotTable; or its superclasses (declaration of 'androidx.compose.runtime.SlotTable' appears in /data/app/com.troy.tersive-sXbO-PmO4LZyaJ_OdajsiA==/base.apk)
at com.troy.tersive.ui.main.MainActivityKt.MainPage(MainActivity.kt:295)

To Reproduce
Steps to reproduce the behavior:

  1. Get a working app using Compose alpha06, Compose Navigation alpha01, and Koin 2.2.0, which also uses getViewModel from Koin
  2. Change to Compose alpha07
  3. Launch app
  4. App crashes when calling getViewModel

Expected behavior
The app should work the same and not crash.

Koin project used and used version (please complete the following information):
implementation "org.koin:koin-androidx-viewmodel:2.2.0"
implementation "org.koin:koin-androidx-compose:2.2.0"
implementation "org.koin:koin-androidx-ext:2.2.0"

android check issue

Most helpful comment

@mykhailo-gaidai I have the same crash with you. I used only compose alpha07 + compose-navigation alpha01. Then I upgraded compose-navigation alpha02. That fix it. But there is a bug on alpha02. Not working backPressed button after rotated screen.

All 4 comments

Meanwhile i am using this workaround:

class MyViewModel : ViewModel() {

    // TODO: this is a workaround for koin 2.2.0 not working with compose 1.0.0-alpha07
    private val koin = GlobalContext.get()
    private val repository = koin.get<MyRepository>()

}

and inject the viewmodel into my composable by using androidx.compose.ui.viewinterop.viewModel (which requires default viewmodel constructor).

@Composable
fun MyView(
    myViewModel: MyViewModel = viewModel()
) {
    ...
}

I have the same crash although I'm not using Koin - only compose alpha07 + compose-navigation alpha01
crash occurs on NavHost initialization:

Scaffold {
    NavHost( // <-- right here
        navController = navController,
        startDestination = "splash",
    ) {
        composable("splash") { SplashScreen() }
    }
}

so it might not be caused by or related to Koin

@mykhailo-gaidai I have the same crash with you. I used only compose alpha07 + compose-navigation alpha01. Then I upgraded compose-navigation alpha02. That fix it. But there is a bug on alpha02. Not working backPressed button after rotated screen.

will be updated in 2.2.1 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

luna-vulpo picture luna-vulpo  路  4Comments

haroldadmin picture haroldadmin  路  3Comments

dakuenjery picture dakuenjery  路  4Comments

AHarazim picture AHarazim  路  3Comments

mubarak1361 picture mubarak1361  路  3Comments