question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get Context when using Koin in an Android library?

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
pawelmarchewkacommented, Aug 6, 2019

Hi @cassioso

Yes, I use it that way in commercially available SDK and it works 🙂

object TcxKoinContext {
    private lateinit var appContext: Context

    val koin: Koin by lazy {
        koinApplication {
            androidContext(appContext)
            if (Log.ON) androidLogger()
            modules(KoinModules.all())
        }.koin
    }

    @Synchronized
    fun init(context: Context) {
        check(!::appContext.isInitialized) { "Already initialized!" }

        appContext = context.applicationContext
    }
}

and

interface TcxKoinComponent : KoinComponent {
    override fun getKoin(): Koin = TcxKoinContext.koin
} 
5reactions
ThePredatorscommented, Aug 13, 2020

Hi @cassioso

Yes, I use it that way in commercially available SDK and it works 🙂

object TcxKoinContext {
    private lateinit var appContext: Context

    val koin: Koin by lazy {
        koinApplication {
            androidContext(appContext)
            if (Log.ON) androidLogger()
            modules(KoinModules.all())
        }.koin
    }

    @Synchronized
    fun init(context: Context) {
        check(!::appContext.isInitialized) { "Already initialized!" }

        appContext = context.applicationContext
    }
}

and

interface TcxKoinComponent : KoinComponent {
    override fun getKoin(): Koin = TcxKoinContext.koin
} 

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 !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found