4

Background

On a large app, that is very popular, I've noticed that when we close a foreground service, on a very specific group of devices (OnePlus devices: 6, 6T,A6000 and a few others) and Android versions (Android 9) , we got NPE:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
       at android.os.Parcel.createException + 1956(Parcel.java:1956)
       at android.os.Parcel.readException + 1918(Parcel.java:1918)
       at android.os.Parcel.readException + 1868(Parcel.java:1868)
       at android.app.IActivityManager$Stub$Proxy.setServiceForeground + 5111(IActivityManager.java:5111)
       at android.app.Service.stopForeground + 724(Service.java:724)
       at android.app.Service.stopForeground + 710(Service.java:710)
       ...

And the code that calls it is:

    stopForeground(true) //crash here
    stopSelf()

The problem

Since I don't have any of those devices, let alone Android 9 for them, I can't find the reason for this.

Since the app is so popular, and I see this issue exists only on those devices and Android versions, I suspect that indeed something is wrong only for them.

What I've found

Only thing I could do is to search for this issue over the Internet, but I couldn't find it.

The only similar thing is here, but it talks about Android O, and we didn't get even a single crash event there.

I know that sadly various manufacturers have applied a bad behavior that kills apps even if they are in the foreground (written about here), but I didn't think that this could cause a crash...

The questions

  1. Is there anything I can do to prevent it? The other place mentioned to use a wakeLock. Would that help?

  2. Maybe would using try-catch avoid this crash? But then what could happen? Would stopSelf alone make the service stop as usual? Or maybe I need to use try-catch on it as well?

  3. What is this exception anyway? I thought it would tell me about some invalid state, but instead I see NPE...

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    "What is this exception anyway?" -- a firmware bug, most likely. "Maybe would using try-catch avoid this crash?" -- wrapping the `stopForeground()` call in a `try`/`catch` might let you successfully call `stopSelf()`. I'm not certain if you absolutely need to call `stopForeground()` if you are calling `stopSelf()`, though it's a good idea. In this case, `stopSelf()` is the more important thing, so if the `stopForeground()` crashes inexplicably, `¯\_(ツ)_/¯` and move on. "Would stopService alone make the service stop as usual?" -- it should, barring another firmware bug. – CommonsWare Aug 18 '19 at 11:50
  • Sorry I meant "stopSelf" and not "stopService " . Updated the question. So you say I could use try-catch for each of them, and you think it might be ok? Do you think it might be the annoying new behavior of manufacturers, of killing apps in times they shouldn't ? – android developer Aug 18 '19 at 12:04
  • 1
    "So you say I could use try-catch for each of them, and you think it might be ok?" -- it can't hurt. "Do you think it might be the annoying new behavior of manufacturers, of killing apps in times they shouldn't ?" -- I think it might be the annoying old behavior of manufacturers, of tweaking the framework without adequate testing. That being said... where do the two lines of code that contain your crash reside? Clearly they are inside the service, but under what circumstances do they get called? – CommonsWare Aug 18 '19 at 12:13
  • @CommonsWare I see. As for when it gets called, it's on various events (on the UI thread), and on 2 cases on `onStartCommand` : if for some reason the intent that I get is null (and then I return Service.START_NOT_STICKY) , and when a notification action ("close" or something like that) was handled from there (to close the service manually, by the user). What do you mean by "old behavior" ? Since when have you started seeing similar issues? On which case? – android developer Aug 18 '19 at 12:19
  • 1
    "Since when have you started seeing similar issues?" -- when have I seen device manufacturers change things and introduce incompatibilities? I recall Motorola screwing up the old `Contacts` provider around Android 1.5. If by "similar issues" you mean throwing an exception on `stopForeground()`, I haven't seen that before. But manufacturers make incompatible changes all the time, though thankfully it seems to have slowed down somewhat in recent years. – CommonsWare Aug 18 '19 at 12:34
  • @CommonsWare Oh contacts API. One of the most annoying things to handle on Android... Anyway, I've added try-catch for each. I hope it will help. Thing is that I might never know if it does help, and whether it causes a different issue. – android developer Aug 18 '19 at 12:43
  • 1
    "Thing is that I might never know if it does help, and whether it causes a different issue" -- if you have crash logging integrated in the app, be sure to log if either of those `catch` blocks gets executed. Then, you will at least know if you are getting exceptions there. – CommonsWare Aug 18 '19 at 12:47
  • Well if they are not, I will see a new crash there. And if they are, well, that's the hoping part... We use Crashlytics. Anyway, thank you again for your wise advice. Why did you stop writing an answer and write mostly in comments? You got so "rich" here of points, that you don't care anymore? :) – android developer Aug 18 '19 at 12:50

0 Answers0