A Jetpack Compose preview that fails to render when the screen uses Koin
The Compose preview pane shows a render error instead of the screenShort 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().
Fix it fast
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/java2Match what you see, then jump to its fix
- 1
The composable calls koinViewModel() or koinInject(), and the preview fails while the app itself runs fineNo Koin container exists inside the previewFix - 2
Only 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
- 1
What does the error look like?
The Compose preview pane shows a render error instead of the screenThe 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:compileDebugKotlinConfirmed by insert-koin.io
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/java2Fix
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:compileDebugKotlinConfirmed by insert-koin.io
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
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile 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 fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile 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