Search code examples
androidkotlinkoin

Koin Exception when using an Android Service with isolatedprocess=true


I am trying to add an service with the isolatedProcess flag to my application see https://developer.android.com/guide/topics/manifest/service-element for details. But as soon as I set the flag to true the service crashes with the following koin exception:

Process: thumbnailCreationService, PID: 4215 java.lang.RuntimeException: Unable to create application XYZApplication: org.koin.core.error.InstanceCreationException: Could not create instance for [Singleton:'XYZ']
                                                                                                        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6760)
                                                                                                        at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2129)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7868)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                                                                                                    Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [Singleton:'XYZ']

koinmodule:

val appModule = module {
  singleOf(::XYZ)
}

Android application class:

  protected open fun setUpKoin() {
        startKoin {
            androidContext(this@XYZApplication)
            modules(
                listOf(
                    appModule
               ))
}

I don´t know why this happens, because I only trigger the initalization of the koin modules in the application class and not the service.

Update: it looks like the init of koin fails because my Application context is not available in the service. But i don´t know how I can fix this.


Solution

  • The problem was that some functions like the MasterKey are not available in an isolatedprocess service and because of that KOIN crashes when it trys to init the classes that use such functions. To fix this I have added some checks (android.os.Process.isIsolated()) in my application to modify the koin initalization.