How to fix “No compatible definition found for type 'Context'” from Koin lib?

How to fix “No compatible definition found for type 'Context'” from Koin lib?



I just start using Koin lib in android (to replace Dagger 2) and in project which was prepared for tests I have issue with android app context in module:


val M = module
val ctx = androidApplication() //here error



Koin is started in App class:


import android.app.Application
import android.content.Context
import org.koin.android.ext.android.startKoin

class App : Application()

override fun onCreate()
super.onCreate()
startKoin(this, listOf(M))




I get log:


D/App: onCreate()
I/KOIN: [context] create
E/KOIN: [ERROR] - Error while resolving instance for class 'android.app.Application' - error: org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'Application'. Check your module definition



and app crashes. Did I miss something in configuration of Koin? In target project I have few modules which deeply depends on application context. And I don't want to use global reference to this context.




2 Answers
2



Try not to create a val for the applicationAndroid() context but use it directly inside the factory/single closure as a parameter for one of your dependencies.
What I'm doing in my project is something like:


val


applicationAndroid()


val appModule = module(override = true)
factory<Navigator> MyNavigator(androidApplication())



where the MyNavigator class is:


class MyNavigator(private val context: Context): Navigator

override fun goToDetail(detailId: String)
context.startActivity(DetailActivity.getIntent(context, detailId))




p.s.: I did also some experiments with Koin 1.0.0 and I noticed that you can also write something like:


val appModule = module(override = true)
factory<Navigator> MyNavigator(get())



That get() will retrieve the context for you even if there's no dependency in the graph for a Context instance; neither a factory nor a singleton. It might be that Koin does something behind the scenes. I tried to use it with different type of dependencies and it aways works.


get()






I will check but see: github.com/InsertKoinIO/koin/issues/190 author of Koin said that both ways are ok :)

– LunaVulpo
Sep 19 '18 at 8:43







I've seen that issue but in my case, the context declaration as a val doesn't work at all.

– nicopasso
Sep 19 '18 at 13:24






another solution: val ctx by lazy androidApplication()

– LunaVulpo
Sep 20 '18 at 9:03



Solution is easy but not so obvious.



Somehow Android Studio imports standalone startKoin function instead of specific android function.



So you had to replace


import org.koin.standalone.StandAloneContext.startKoin



To


import org.koin.android.ext.android.startKoin



in Application class


Application



Do tell if this works or not.






nope :( Look at edited question. It's easy to reproduction: Just setup new project and configure Koin.

– LunaVulpo
Sep 15 '18 at 12:19







What if I want to do this from Java (not Kotlin)?

– IgorGanapolsky
Dec 27 '18 at 22:32



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)