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.

Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.

See original GitHub issue

Module configuration

val healthRecordModule = module {
    viewModel { HealthRecordViewModel(get()) }
    viewModel { HealthVisitViewModel(get(), get()) }
    viewModel { RecordsViewModel() }
    viewModel { LabResultsViewModel(get()) }
    viewModel { DateFilterViewModel(get()) }
    factory { DateFilterMapper(androidContext()) }
    factory { HealthRecordDataMapper(androidContext()) }
    factory { HealthVisitDataMapper(androidContext()) }
    factory { ViewAllResultsDataMapper(androidContext()) }
    factory { LabResultsDataMapper() }
}

package com.hcahealthcare.mhom.domain.account

import com.hcahealthcare.mhom.di.account.accountModule
import com.hcahealthcare.mhom.di.appModule
import com.hcahealthcare.mhom.di.authentication.authenticationModule
import com.hcahealthcare.mhom.di.configuration.configurationModule
import com.hcahealthcare.mhom.di.dashboardModule
import com.hcahealthcare.mhom.di.healthrecord.healthRecordModule
import com.hcahealthcare.mhom.di.networkingModule
import com.hcahealthcare.mhom.di.opr.oprModule
import com.hcahealthcare.mhom.di.profile.profileModule
import org.junit.Test
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.dsl.koinApplication
import org.koin.test.AutoCloseKoinTest
import org.koin.test.check.checkModules

/**
 * Dry run configuration
 */
class CheckModulesTest : AutoCloseKoinTest() {

    @Test
    fun dryRunTest() {
        startKoin {
            printLogger(Level.DEBUG)
            modules(listOf(
                networkingModule, accountModule, authenticationModule, configurationModule,
                appModule, dashboardModule, oprModule, profileModule, healthRecordModule
            ))
        }.checkModules()
    }
}

this is failing with


_rg.koin.android.error.MissingAndroidContextException: Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.

	at org.koin.android.ext.koin.ModuleExtKt.androidContext(ModuleExt.kt:33)
	at com.hcahealthcare.mhom.di.healthrecord.HealthRecordModuleKt$healthRecordModule$1$9.invoke(healthRecordModule.kt:18)
	at com.hcahealthcare.mhom.di.healthrecord.HealthRecordModuleKt$healthRecordModule$1$9.invoke(healthRecordModule.kt)
	at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54)
	at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37)
	at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70)
	at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)
	at org.koin.core.scope.Scope.access$resolveInstance(Scope.kt:33)
	at org.koin.core.scope.Scope$get$$inlined$synchronized$lambda$1.invoke(Scope.kt:123)
	at org.koin.core.time.MeasureKt.measureDuration(Measure.kt:37)
	at org.koin.core.scope.Scope.get(Scope.kt:122)
	at org.koin.core.Koin.get(Koin.kt:107)
	at org.koin.test.check.CheckModulesKt.checkMainDefinitions(CheckModules.kt:60)
	at org.koin.test.check.CheckModulesKt.checkModules(CheckModules.kt:38)
	at org.koin.test.check.CheckModulesKt.checkModules(CheckModules.kt:25)
	at org.koin.test.check.CheckModulesKt.checkModules$default(CheckModules.kt:25)
	at com.hcahealthcare.mhom.domain.account.CheckModulesTest.dryRunTest(CheckModulesTest.kt:32)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code 255_

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
cortinicocommented, Aug 20, 2019

Hey @pollux- If you have modules that require a Context instance, you need to provide it with androidContex() inside your startKoin block.

So ideally you need to change your code to:

    fun dryRunTest() {
        startKoin {
            printLogger(Level.DEBUG)
            androidContext(...)
            modules(listOf(..., healthRecordModule))
        }.checkModules()
    }
val healthRecordModule = module {
    viewModel { HealthRecordViewModel(get()) }
    viewModel { HealthVisitViewModel(get(), get()) }
    viewModel { RecordsViewModel() }
    viewModel { LabResultsViewModel(get()) }
    viewModel { DateFilterViewModel(get()) }
    factory { DateFilterMapper(get()) }
    factory { HealthRecordDataMapper(get()) }
    factory { HealthVisitDataMapper(get()) }
    factory { ViewAllResultsDataMapper(get()) }
    factory { LabResultsDataMapper() }
}
0reactions
AdamMc331commented, Aug 19, 2020

I had a similar problem and was able to work around using MockK.

The module in question was creating shared prefs:

val coreModule = module {
    single {
        androidApplication().getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
    }
}

In order for my checkModules test to pass, I had to both mock the application and the prefs it returned:

class KoinModulesTest : KoinTest {
    private val mockApplication: Application = mockk()
    private val mockPreferences: SharedPreferences = mockk()

    @Test
    fun checkViewModelModules() {
        every {
            mockApplication.getSharedPreferences(any(), any())
        } returns mockPreferences

        koinApplication {
            androidContext(mockApplication)
            modules(appModules)
        }.checkModules()
    }
}

I’m not sure if this is a code smell, and there’s a better way to deal with SharedPreferences in Koin, but dropping this here to help the next person.

Read more comments on GitHub >

github_iconTop Results From Across the Web

org.koin.android.error.MissingAndroidContextException
MissingAndroidContextException: Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration. – ...
Read more >
Start Koin on Android
From your Application class​. From your Application class you can use the startKoin function and inject the Android context with androidContext as follow:....
Read more >
A brand new website interface for an even better experience!
Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.
Read more >
Dependency Injection using Koin - Medium
Koin is the dependency injection framework purely built in Kotlin. ... Use given Android context; by inject() : allows to retrieve instances ......
Read more >
I m trying to update to 2 0 0 beta 4 from 0 9 1 i WOULD have | Koin ...
applicationContext() I get a crash: Can't resolve Context instance. Please use androidContext() function in your KoinApplication configuration.
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