Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

A Jetpack Compose preview that fails to render when the screen uses Koin

ErrorThe Compose preview pane shows a render error instead of the screen

Short answer

Previews do not run your Application class, so startKoin never runs and the preview has no Koin container. Wrap the previewed composable in KoinApplicationPreview and give it a small module with fakes. Also check that the screen does not read the global Koin through KoinPlatform.getKoin().

  • Applies to Koin 4.1+, Jetpack Compose, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the preview's error panel in Android Studio and read the stack trace. Then search the previewed screen for every place it asks Koin for something (koinViewModel, koinInject, get, KoinPlatform).

    grep -rnE "koinViewModel|koinInject|KoinPlatform|getKoin" app/src/main/java
  2. 2Match what you see, then jump to its fix

    1. 1The composable calls koinViewModel() or koinInject(), and the preview fails while the app itself runs fineNo Koin container exists inside the previewFix
    2. 2Only some screens fail: the ones that call KoinPlatform.getKoin() or a similar global lookup instead of a Compose functionThe screen or a helper reads the global Koin instanceFix

What does the error look like?

  • The Compose preview pane shows a render error instead of the screen
  • The screen works on a device but its @Preview does not render

Cause 1 of 2

No Koin container exists inside the preview

1Check

Koin normally starts in your Application class. Compose previews do not run that class, so nothing has started Koin when the preview renders. Check whether the preview function wraps the screen in anything Koin-related.

grep -rn "KoinApplicationPreview" app/src/main/java || echo "no preview wrapper found"

2Fix

Wrap the composable in KoinApplicationPreview and pass a module that provides what the screen asks for. The wrapper builds a local container for that preview only, so several previews can render side by side.

import org.koin.compose.KoinApplicationPreview

val previewModule = module {
    single<UserRepository> { FakeUserRepository(sampleUsers) }
    viewModel { UserViewModel(get()) }
}

@Preview
@Composable
fun UserScreenPreview() {
    KoinApplicationPreview(application = { modules(previewModule) }) {
        UserScreen()
    }
}

3Verify

Refresh the preview (Build and Refresh). The screen renders with the fake data. Add a second preview with a failing fake to see the error state.

./gradlew :app:compileDebugKotlin

Confirmed by insert-koin.io

Did this fix it?

Cause 2 of 2

The screen or a helper reads the global Koin instance

1Check

KoinApplicationPreview builds its container with koinApplication, not startKoin, so it is not the global one. Code that asks the global context does not see the preview's container.

grep -rn "KoinPlatform" app/src/main/java

2Fix

In UI code, resolve through the composition instead of the global lookup. Koin's docs recommend koinInject() for UI code, and getKoin() inside a composable returns the Koin instance of the current composition.

@Composable
fun UserScreen() {
    val repo: UserRepository = koinInject()
    // ...
}

3Verify

The preview that failed on that screen now renders.

./gradlew :app:compileDebugKotlin

Confirmed by insert-koin.io

Did this fix it?

How do I stop it coming back?

  • Give every screen with Koin dependencies a preview wrapped in KoinApplicationPreview from the start.
  • Keep one small preview module of fakes and reuse it across previews.
  • Use koinInject() or koinViewModel() in composables, and keep global Koin lookups out of UI code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android Studio cannot pair the phone over Wi-Fi

The Pair devices over Wi-Fi dialog lists no device

Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.

4 causes, each with a fix

Mobile apps

adb tcpip 5555 fails because no device is found

adb: more than one device/emulator

adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.