readium / kotlin-toolkit

A toolkit for ebooks, audiobooks and comics written in Kotlin
https://readium.org/kotlin-toolkit
BSD 3-Clause "New" or "Revised" License
186 stars 82 forks source link

Unable to create application org.readium.r2.testapp.Application: android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent #298

Closed RomanSoviak closed 1 year ago

RomanSoviak commented 1 year ago

I launched test-app on Samsung m31(Android 12) via Android Studio. Everything was good, but after I added prod lcp library readium:liblcp:2.0.0@aar for testing opening prod lcpl files, (this library we obtained from Edrlab a long time ago) the app crashed on start with this stacktrace:

2022-11-14 21:31:51.667 17485-17485/? E/USNET: USNET: appName: org.readium.r2reader
2022-11-14 21:31:52.266 17485-17485/org.readium.r2reader E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.readium.r2reader, PID: 17485
    java.lang.RuntimeException: Unable to create application org.readium.r2.testapp.Application: android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=org.readium.r2.testapp.MediaService cmp=org.readium.r2reader/org.readium.r2.testapp.MediaService }: app is in background uid UidRecord{f7258d5 u0a2183 SVC  idle change:idle|uncached procs:0 seq(0,0,0)}
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7593)
        at android.app.ActivityThread.access$1600(ActivityThread.java:315)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2286)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8751)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=org.readium.r2.testapp.MediaService cmp=org.readium.r2reader/org.readium.r2.testapp.MediaService }: app is in background uid UidRecord{f7258d5 u0a2183 SVC  idle change:idle|uncached procs:0 seq(0,0,0)}
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1941)
        at android.app.ContextImpl.startService(ContextImpl.java:1887)
        at android.content.ContextWrapper.startService(ContextWrapper.java:793)
        at org.readium.r2.testapp.Application.onCreate(Application.kt:85)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7588)
        at android.app.ActivityThread.access$1600(ActivityThread.java:315) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2286) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:226) 
        at android.os.Looper.loop(Looper.java:313) 
        at android.app.ActivityThread.main(ActivityThread.java:8751) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 

The crash happened in startService() method

Screenshot 2022-11-14 at 22 24 42
mickael-menu commented 1 year ago

This doesn't seem related to the LCP library.

You could try replacing the startService with startForegroundService, but see our related discussion on this PR.

@qnga Maybe we'll need to revisit the use of startService for the new MediaService.

RomanSoviak commented 1 year ago

@mickael-menu I will close this issue, cause the problem was that I shouldn't use prod lcp lib in the test app, cause this lib should be used only in our app with a certain bundle id, but it would be great if developers got the error message about that. I added lcp test lib and everything works fine, thanks

mickael-menu commented 1 year ago

That doesn't make any sense to me, but maybe there's a race condition:

  1. The liblcp crashed the app after checking that it was not the right app.
  2. Android puts the app in the background before killing it.
  3. startService() is reached before the app is killed.