React-native-track-player: Android 8: IllegalStateException

Created on 2 Jul 2018  路  27Comments  路  Source: react-native-kit/react-native-track-player

I am using the latest dev branch with a few fixes on top from @nasmuris' fork: https://github.com/nasmuris/react-native-track-player/commits/dev

I am seeing the following crashes from users on Android 8 / 8.1:

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleUnbindService (ActivityThread.java:3487)
  at android.app.ActivityThread.-wrap27 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1705)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:172)
  at android.app.ActivityThread.main (ActivityThread.java:6637)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

Caused by: java.lang.IllegalStateException: 
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1505)
  at android.app.ContextImpl.startService (ContextImpl.java:1461)
  at android.content.ContextWrapper.startService (ContextWrapper.java:644)
  at guichaguri.trackplayer.logic.Events.dispatchEvent (Events.java:41)
  at guichaguri.trackplayer.logic.MediaManager.onServiceUnbounded (MediaManager.java:271)
  at guichaguri.trackplayer.logic.services.PlayerService.onUnbind (PlayerService.java:43)
  at android.app.ActivityThread.handleUnbindService (ActivityThread.java:3473)

Most helpful comment

It's the new service policy implemented in Android 8. In my recode, I'm merging the events service and the music service together, so that won't be a problem anymore.

All 27 comments

I have been able to replicate the crash, so I have a more detailed crash log:

07-02 06:00:42.989   908   944 W ActivityManager: Stopping service due to app idle: u0a282 -4m30s370ms com.jonathanpuckey.radiogarden/guichaguri.trackplayer.logic.services.PlayerService
07-02 06:00:42.992  9147  9147 D ReactNativeTrackPlayer: onUnbind
07-02 06:00:42.995   908  1612 W ActivityManager: Background start not allowed: service Intent { cmp=com.jonathanpuckey.radiogarden/guichaguri.trackplayer.logic.services.PlayerTask (has extras) } to com.jonathanpuckey.radiogarden/guichaguri.trackplayer.logic.services.PlayerTask from pid=9147 uid=10282 pkg=com.jonathanpuckey.radiogarden
07-02 06:00:42.996  9147  9147 D AndroidRuntime: Shutting down VM
07-02 06:00:42.998  9147  9147 E AndroidRuntime: FATAL EXCEPTION: main
07-02 06:00:42.998  9147  9147 E AndroidRuntime: Process: com.jonathanpuckey.radiogarden, PID: 9147
07-02 06:00:42.998  9147  9147 E AndroidRuntime: java.lang.RuntimeException: Unable to unbind to service guichaguri.trackplayer.logic.services.PlayerService@e709191 with Intent { act=trackplayer.connect cmp=com.jonathanpuckey.radiogarden/guichaguri.trackplayer.logic.services.PlayerService }: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.jonathanpuckey.radiogarden/guichaguri.trackplayer.logic.services.PlayerTask (has extras) }: app is in background uid UidRecord{618e9f5 u0a282 SVC  bg:+1m0s7ms idle change:idle procs:1 seq(0,0,0)}
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ActivityThread.handleUnbindService(ActivityThread.java:3408)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ActivityThread.-wrap27(Unknown Source:0)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:106)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:164)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6494)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
07-02 06:00:42.998  9147  9147 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.jonathanpuckey.radiogarden/guichaguri.trackplayer.logic.services.PlayerTask (has extras) }: app is in background uid UidRecord{618e9f5 u0a282 SVC  bg:+1m0s7ms idle change:idle procs:1 seq(0,0,0)}
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ContextImpl.startService(ContextImpl.java:1477)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.content.ContextWrapper.startService(ContextWrapper.java:650)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at guichaguri.trackplayer.logic.Events.dispatchEvent(Events.java:41)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at guichaguri.trackplayer.logic.MediaManager.onServiceUnbounded(MediaManager.java:271)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at guichaguri.trackplayer.logic.services.PlayerService.onUnbind(PlayerService.java:43)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    at android.app.ActivityThread.handleUnbindService(ActivityThread.java:3394)
07-02 06:00:42.998  9147  9147 E AndroidRuntime:    ... 8 more
07-02 06:00:43.013  1446  1900 V Avrcp   : onPlaybackStateChanged: state PlaybackState {state=3, position=51000, buffered position=0, speed=1.0, updated=764660, 

It's the new service policy implemented in Android 8. In my recode, I'm merging the events service and the music service together, so that won't be a problem anymore.

Awesome - looking forward to it!

Nice, any estimate on your next release? Really looking forward to it as well!

@puckey I am facing same issue. Do you have a solution for this?

I think guichaguri was clear about what the solution would need to be - lets have some patience and not add to this thread unless we have something of substance to add.

Is there anything we can do to our current codebases to prevent this error while we wait for a fix? I just pushed an app centered around this module to production and my users on Android 8.0 are freaking out.

I tried pulling in the android redesign branch but anytime I call any method on TrackPlayer it hard crashes the app

Okay I found the cause, more about that here.

According to this:

By default, these restrictions only apply to apps that target Android 8.0 (API level 26) or higher. However, users can enable most of these restrictions for any app from the Settings screen, even if the app targets an API level lower than 26.

All you need to do to fix this error while we wait for a PR is change your targetSdkVersion to 25. I will be testing this today and will update this comment with my results.

I am also trying to find a patch while we wait for a re write since I am on a similar boat with my Android 8.0 users. As I understand by changing the targetSdkVersion to 25 would you then have to enable the restrictions manually through settings?

@Alanz2223 that is what it seems yes, projects targeting 25 or lower are exempt from those settings unless the user themself toggles the setting.

I have a beta test running where I changed the targetSdkVersion for react-native-track-player and the targetSdkVersion of my own project to 25 and it seems to be going smoothly. Android did give me a warning when I drafted the release but it seems to be safe to ignore.

@robbiemilejczak hey I just wanted to know whether everything has gone smoothly by downgrading? Also word of caution this workaround will only be good for the next couple of months

Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018.

@Alanz2223 so sorry, yes the change to target 25 did fix the crashes for my 8.0 users. Hopefully we will see a PR before Nov 1, I appreciate that info!

It's the new service policy implemented in Android 8. In my recode, I'm merging the events service and the music service together, so that won't be a problem anymore.

Sounds good, but this was over 2,5 months ago. Any ETA on this? We're developing a new app and just hit this issue. @robbiemilejczak - You mentioned that you got a warning when you targeted 25 on a beta release. Was that for an app update or a new app release?

@magnus-eriksson that was in regards to a new release. I'm not holding my breath for any updates to this, for now I'll be relying on targeting 7.0 and updating changes via code-push but I think I will be transitioning to react-native-video to handle audio streaming

+1

We've just updated the library to support RN 57 and Android SDK 27. Is this still an issue in the latest version we've just published?
v.1.0.0:
https://github.com/react-native-kit/react-native-track-player/releases/tag/v1.0.0

@dcvz

i faced a similar issue after updating to v1.0.0 (only on android 8+):

 W/ActivityManager: Stopping service due to app idle: u0a678 -1m10s266ms com.******/com.guichaguri.trackplayer.service.MusicService
D/MediaSessionService: Destroying com.*****/TrackPlayer (userId=0)

the app doesn't crash, but the playback stops and then using any of the remote controls will cause a crash:

7459-7459/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.*****, PID: 7459
    java.lang.RuntimeException: Unable to start service com.guichaguri.trackplayer.service.MusicService@ea18111 with Intent { act=android.intent.action.MEDIA_BUTTON flg=0x10000010 cmp=com.*****/com.guichaguri.trackplayer.service.MusicService bnds=[210,859][378,1027] (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'com.guichaguri.trackplayer.service.metadata.MetadataManager com.guichaguri.trackplayer.service.MusicManager.getMetadata()' on a null object reference
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3696)
        at android.app.ActivityThread.-wrap21(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1801)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.guichaguri.trackplayer.service.metadata.MetadataManager com.guichaguri.trackplayer.service.MusicManager.getMetadata()' on a null object reference
        at com.guichaguri.trackplayer.service.MusicService.onStartCommand(MusicService.java:54)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3679)
        at android.app.ActivityThread.-wrap21(Unknown Source:0)聽
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1801)聽
        at android.os.Handler.dispatchMessage(Handler.java:105)聽
        at android.os.Looper.loop(Looper.java:164)聽
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)聽
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)聽
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)聽

I just noticed on android <8.0 when the app is closed from task manager:

/com.***** D/ReactNative: ReactInstanceManager.detachViewFromInstance()
/com.*****D/RNTrackPlayer: Releasing service resources...
/com.*****I/ExoPlayerImpl: Release 67aad2e [ExoPlayerLib/2.9.0] [generic_x86_64, Android SDK built for x86_64, unknown, 23] [goog.exo.core]

the notification bar doesn't get dismissed an on interaction the app will crash with the same error.

@Drazail I added check for null manager in the onStartCommand method.

if (intent != null && Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
   if (manager == null) return START_NOT_STICKY;
   MediaButtonReceiver.handleIntent(manager.getMetadata().getSession(), intent);
   return START_NOT_STICKY;
}

That worked for me

@jankrah12 Yes, use the latest dev branch, it has been fixed.

Does it make sense to maintain this issue opened, whilst the issue had been fixed?

ps: @Guichaguri Tks for fixing it, your work has contributed a lot to the community.

Yes, sorry I have not been able to go back into android mode yet to test this. Hope to do so in a week or two. Feel free to close if it is fixed!

I am on the dev branch and am seeing the same crash

if (manager == null) return START_NOT_STICKY; does not appear to be in the current dev

https://github.com/react-native-kit/react-native-track-player/blob/9834aabef64d7d5b517960dafdad8ada9dcaad33/android/src/main/java/com/guichaguri/trackplayer/service/MusicService.java#L53

        if (intent != null && Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
            MediaButtonReceiver.handleIntent(manager.getMetadata().getSession(), intent);
            return START_NOT_STICKY;
        }

@brenwell We've made some changes to the notification behavior and it should work better now.

I think I am getting this on 1.1.1.

--------- beginning of crash 2019-01-22 17:11:09.838 19043-19080/? E/AndroidRuntime: FATAL EXCEPTION: mqt_native_modules Process: fm.pinna.app, PID: 19043 java.lang.IllegalStateException: Not allowed to start service Intent { cmp=fm.pinna.app/com.guichaguri.trackplayer.service.MusicService }: app is in background uid UidRecord{d74bafd u0a316 SVC bg:+1m0s247ms idle procs:1 seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1506) at android.app.ContextImpl.startService(ContextImpl.java:1462) at android.content.ContextWrapper.startService(ContextWrapper.java:644) at android.content.ContextWrapper.startService(ContextWrapper.java:644) at com.guichaguri.trackplayer.module.MusicModule.waitForConnection(MusicModule.java:100) at com.guichaguri.trackplayer.module.MusicModule.getState(MusicModule.java:418) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29) at android.os.Looper.loop(Looper.java:164) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192) at java.lang.Thread.run(Thread.java:764) 201

This is happening after the app has been sitting in the background for a bit. Android 8.0.0

@curiousdustin seems like the same as #391

I agree, more comments over on that issue.

Was this page helpful?
0 / 5 - 0 ratings