Hi all, trying to use koin on the native side, and...
# koin
x
Hi all, trying to use koin on the native side, and i’m running into this
Copy code
Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)
with very little output from the console as to why. Its pointing to this
Copy code
App`kfun:org.koin.core.module.Module#<get-mappings>(){}kotlin.collections.HashMap<kotlin.String,org.koin.core.instance.InstanceFactory<*>>:
Has anyone encountered this before?
Okay after sometime, i think i found the issue, and this is a weird one - so bear with me 🐻. I have two modules
:login
and
:app
.
:app
has this file
AppModules.kt
Copy code
val appModules: List<Module> = listOf(
  .. 
  loginViewModelsModule,
  appViewModelsModule,
)
where,
loginViewModelsModule
is defined in
:login
commonMain
as an
Copy code
expect val loginViewModelsModule: Module
and
appViewModelsModule
is defined in
:app
commonMain
as
Copy code
expect val appViewModelsModule: Module
In order to debug the above runtime crash, i commented out
appViewModelsModule
like
Copy code
val appModules: List<Module> = listOf(
  .. 
  loginViewModelsModule,
  // appViewModelsModule,
)
which worked, so I thought its something to do with how i defined this particular module. So i made the module empty in
iosMain
Copy code
actual val appViewModelsModule: Module = module { }
but even then it was still crashing with the same ambiguous error. This was pretty much a road-block - so then I decided to define the module on the same file in
AppModules.kt
then it dawned on me
Copy code
expect val appViewModelsModule: Module 

val appModules: List<Module> = listOf(
  .. 
  loginViewModelsModule,
  appViewModelsModule, // Compilation error, variable must be initialised
)
Looks like kotlin compiler is okay with
expect
top level members from external modules being not initialised, but somehow not okay with the
expect
top level members from within the same module. Am i going crazy, or doesn’t this sound like a kotlin-compiler bug 🤔?
apparently, the solution was just to use a delegate in the iosMain
Copy code
actual val appViewModelsModules: Module get() =
  module {
    single { MainViewModel() }
    single { RootViewModel() }
  }
and that worked just fine