Koin在使用带有isolatedprocess=true的Android Service时出现异常。

huangapple go评论61阅读模式
英文:

Koin Exception when using an Android Service with isolatedprocess=true

问题

我正在尝试为我的应用程序添加一个带有isolatedProcess标志的服务,详情请参见https://developer.android.com/guide/topics/manifest/service-element。但是,一旦我将标志设置为true,服务就会崩溃,并出现以下Koin异常:

进程:thumbnailCreationService,PID:4215 java.lang.RuntimeException:无法创建应用程序XYZApplication的实例:org.koin.core.error.InstanceCreationException:无法为[Singleton:'XYZ']创建实例
      在android.app.ActivityThread.handleBindApplication(ActivityThread.java:6760)处
      在android.app.ActivityThread.-$$Nest$mhandleBindApplication(未知来源:0)
      在android.app.ActivityThread$H.handleMessage(ActivityThread.java:2129)处
      在android.os.Handler.dispatchMessage(Handler.java:106)处
      在android.os.Looper.loopOnce(Looper.java:201)处
      在android.os.Looper.loop(Looper.java:288)处
      在android.app.ActivityThread.main(ActivityThread.java:7868)处
      在java.lang.reflect.Method.invoke(Native Method)处
      在com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)处
      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)处
      由org.koin.core.error.InstanceCreationException引起:无法为[Singleton:'XYZ']创建实例

Koin模块:

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

Android应用程序类:

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

我不知道为什么会发生这种情况,因为我只在应用程序类中触发了Koin模块的初始化,而没有在服务中触发。

更新: 看起来Koin的初始化失败,因为我的应用程序上下文在服务中不可用。但我不知道如何修复这个问题。

英文:

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.

答案1

得分: 0

The problem was that some functions like the MasterKey are not available in an isolated process service and because of that KOIN crashes when it tries to initialize 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 initialization.

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 16:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定