I have an app with Koin where most injection happens on the ViewModel level with the Compose APIs, but there's also a
BroadcastReceiver
that I need to inject at some point. As my Koin context is set up at the Compose entry point, it might not have been initialized when this
BroadcastReceiver
is invoked.
So far I'm doing this, but wondering if it could be cleaner:
Copy code
val logger = KoinPlatformTools.defaultContext().getOrNull()?.getOrNull<Logger>()
👀 1
a
arnaud.giuliani
02/27/2025, 8:04 AM
Hey Márton, it's a component for Android only right? You would need to use it in that case?
z
zsmb
02/27/2025, 8:09 AM
Yes, this is specifically in the Android app
a
arnaud.giuliani
02/27/2025, 10:29 AM
Ok, then you have all your stuff in commonMain, but you have a case where you need to retrieve the BroadcastReceiver "by hand", in the android part. Use the "by inject", or
KoinPlatform.getKoin().get<BroadcastReceiver>()
or or your snippet
KoinPlatform.getKoin().getOrNull<Logger>()
z
zsmb
02/27/2025, 11:08 AM
Where does
by inject
come from, which dependency? It seems like I might not have that. The one I have included so far is
koin-compose-viewmodel-navigation
.
a
arnaud.giuliani
02/27/2025, 4:12 PM
by inject()
is available bye default in Android classes, or if you use