How to get Context when using Koin in an Android library?
See original GitHub issueHi,
I’m creating an android library that contains UI components and I’ve managed to use Koin by implementing a custom KoinComponent:
KoinKomponent.kt
import org.koin.android.ext.koin.androidLogger
import org.koin.core.Koin
import org.koin.core.KoinComponent
import org.koin.dsl.koinApplication
private val koinInstance = koinApplication {
androidLogger()
modules(modules)
}
interface CustomKoinComponent : KoinComponent {
override fun getKoin(): Koin = koinInstance.koin
}
And this is my modules list so far:
KoinModule.kt
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.perf.FirebasePerformance
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
private val firebaseModule = module {
single { FirebasePerformance.getInstance() }
single { FirebaseAnalytics.getInstance(get()) } // Can't resolve Context instance.
}
val modules = listOf(
firebaseModule
)
But then I get an error Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for 'android.content.Context' has been found. Check your module definitions.
. What is fair since I’m not starting Koin from an Application nor declaring a context instance.
My question is, how do I get Context in this scenario? Is there a proper way using Koin or should I find a way to parametrize the context from the main module?
I’m using org.koin:koin-android:2.0.1
.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Context Isolation - Koin
Context Isolation ; // start a KoinApplication and register it in Global context startKoin { // declare used modules modules(coffeeAppModule) ; // create...
Read more >How to Inject application context from 'app' module ...
Application context is available inside a module through the function androidContext() .
Read more >Dependency Injection with Koin on Android. | by Daniel Luz
The most common ways to resolve dependencies with Koin is by: implementing the KoinComponent interface or providing the required dependency through constructor ...
Read more >Android Koin scope and Context - Professional Programmer2
Koin scopeIn the previous entry, I explained about simple usage of Koin. (Android Koin Get Started) In this time, I expl.
Read more >Applying Koin
As with most things, Koin comes from a library. Actually, Koin is made up of several libraries, to handle different scenarios. For example,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @cassioso
Yes, I use it that way in commercially available SDK and it works 🙂
and
that worked for me pretty well !!
from the application that use the AAR containing Koin, you just need to call : TcxKoinContext.init(context) // where context is application context. and of course do not forget to extend the TcxKoinComponent in your Class that use injections !