My question is somewhat similar to Using Koin in multi-module Android Project
I am trying to setup a MVVM project with Koin. I have created 4 modules. M1(with all UI View-models), M2(All use cases), M3(Pure logic not corrupted by Android classes) and M4(For access to servers and local databases). I want my M1 to have access to only M2. M2 to have access to both M3 and and M4. M4 has access to M3. M3 does not have access to any other module.
M1
|
M2
| |
M4 - M3
I have the application class in the M4. I am starting my Koin in the framework. Since M4 has to M3 , I am loading M3 modules in the application class. In M1 activity I am loading M1 and M2 modules.
Now some of my classes in M2 need M3 classes.
When I am running the application, I see my M4 application class being loaded which loads Koin modules for both M4 and M3 and later when the first activity in M1 is called, it loads modules for M1 and M2 Application class :
override fun onCreate() { super.onCreate()
startKoin { androidContext(this@UnifiedApplication) modules(M4Modules) loadM3Modules() } }
M1 activity class
loadM2Modules()
loadKoinModules(M1)
But when the any of the M2 objects is being used i get the following exception
Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for M3 class object
What am I doing wrong? How best to use Koin in multi module Android applications? MVVM with separation of layers.