Koin module unresolved reference

4.3k views Asked by At

I have all of my classes that I would like to inject in a modules class as such.

    class modules {
        val myModule = module {
            MainActivityViewModel()
            single { MyRepo() }
        }
    }

I have an Application class that looks like this, which is declared in the manifest.

    class MyApplication : Application() {
        override fun onCreate() {
            super.onCreate()
    
            // start Koin!
            startKoin {
                // declare used Android context
                androidContext(this@MyApplication)
                // declare modules
                modules(listOf(myModule)) <------Here my module is unresolved(AS says unresolved reference)
            }
        }
    }

I am using Koin version 2.1.6

1

There are 1 answers

0
Jenea Vranceanu On BEST ANSWER

myModule is defined inside of a class modules. You cannot access it without an instance of this class.

What most people do is define modules right below the Application class declaration as a global variable. You can make it private so it won't be accessible out of the file.

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        // start Koin!
        startKoin {
            // declare used Android context
            androidContext(this@MyApplication)
            // declare modules
            modules(listOf(myModule))
        }
    }
}

// Optionally make it private
val myModule = module {
    MainActivityViewModel()
    single { MyRepo() }
}

Now you can remove class modules. But if you really need this class and created it with a purpose you can create an instance of this class and call its myModule variable:

modules(listOf(modules().myModule))