Foreground service getting killed from Oreo

后端 未结 5 782
日久生厌
日久生厌 2020-12-07 22:18

I have written a foreground service which is working properly for all OS version lower than Oreo. From Oreo application process is getting killed after 5 minutes of closing

5条回答
  •  不思量自难忘°
    2020-12-07 22:30

    I've analyzed the issue on OnePlus, as I'm in the same situation as you. As I see currently, there is no solution. OnePlus clearly follows a bad practice.

    As they haven't released the source code which kills processes in this manner, I downloaded a OnePlus ROM (I choose OnePlus 5T 5.1.5), extract it, find the .class which does this (OnePlusHighPowerDetector.class in services.vdex), decompile it, and tried to find out what's going on.

    You can find a version of this class here (it is not by me, and maybe it is not the same version that I've used): https://github.com/joshuous/oneplus_blobs_decompiled/blob/master/com/android/server/am/OnePlusHighPowerDetector.java

    Unfortunately, the most important functions aren't decompiled successfully. But we can analyze the bytecode anyway. Here's what I've found:

    • OnePlus kills background processes (it seems that it doesn't matter whether it has a foreground service or not) without mercy. Almost all of them. It doesn't really matter, how little CPU resources are used. My app uses less than 1%, and it gets killed.
    • If the app is pinned in the recent app list (determined by a list of com_oneplus_systemui_recent_task_lockd_list), it doesn't get killed. So, the user can save the app by pinning it. But it is inconvenient for them.
    • Oneplus provides a list of processes, which they don't kill. This list is in oneplus-framework-res.apk/res/values/array.xml, with the key string-array name="backgroundprocess_detection_app_whitelist". This list mostly contains map and fitness applications1.
    • Maybe there are other factors which can save a process being killed, I haven't analyzed the issue further. If you have the time, check out OnePlusHighPowerDetector.java.

    Here are the steps needed to decompile a .vdex file:

    • use Vdex Extractor to create a .dex from .vdex (maybe you'll need to use the --ignore-crc-error option)
    • use JADX to decompile a .dex (this is currently the best decompiler in my opinion), or use dex2jar to create a .jar from .dex, and use any decompiler to decompile the .jar

    1Rant: this shows how bad the current situation is. OnePlus, is this really the solution? You've chosen 10-15 applications, which can run in the background, and you don't care about all the other ones? How can one create a new fitness/map/music player application, which works safely on your device?

提交回复
热议问题