6

After deploying a new APK for a system app, is there a way to restart the app process without rebooting the device?

Non-system apps are managed with the Activity Manager through the am command. There are options like start, restart, force-stop, as well as commands against services, like startservice or stopservice.

I have mainly read and tried around the Activity Manager, and I am missing something. Right now, I just rely on the time-consuming brutal reboot command. Posts on installing system apps also use reboot only, as far as I found.

Great and timely feedback mention the very commands I tried, but when they work, they are in "force" mode. I did not check the source code, but it usually means something akin to kill -s SIGKILL, and I wonder whether that is really necessary to go that far.


Some sample outputs (thanks everyone for comments!):

(note, I have indented the command output for readability)

Restarting with restart

$:/ # am start com.android.email
    Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=com.android.email }
$:/ # ps | grep email
    u0_a30    1436  117   705400 32128 ffffffff 4006b954 S com.android.email
$:/ # am restart com.android.email
    Restart the system...
    android.os.DeadObjectException
    at android.os.BinderProxy.transact(Native Method)
    at android.app.ActivityManagerProxy.restart(ActivityManagerNative.java:4647)
    at com.android.commands.am.Am.runRestart(Am.java:1410)
    at com.android.commands.am.Am.onRun(Am.java:308)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
    at com.android.commands.am.Am.main(Am.java:78)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
    at dalvik.system.NativeStart.main(Native Method)
$:/ # ps | grep email
$:/ # #=> Process was killed anyway
$:/ # am start com.android.email #=> Works again, but the previous error...

Restarting with force-stop

$:/ # am start com.android.email
$:/ # ps | grep email
    u0_a30    2680  1506  706404 31668 ffffffff 40059954 S com.android.email
$:/ # am force-stop com.android.email
$:/ # ps | grep email
$:/ # #=> Process killed
$:/ # am start com.android.email #=> Works again, but it was *forced* kill!

Some context and idiosyncrasies: My particular case is a custom system app (in /system/priv-app) that exposes a service, no activity. I am used to rely on am startservice and am stopservice to manage non-system processes. It does not work properly for system apps (read: "I don't know how to do it" :-) ). All this is on Android 4.4.2, API level 19.

Eric Platon
  • 187
  • 1
  • 1
  • 5
  • 1
    Not really an answer, but faster than a complete reboot it should be to do a soft reboot (i.e. kill the system server). Though I don't know how to do that from the command line (I've normally used the boot menu for that ;) my guess is you simply use the kill command on its process once you've identified that (ps | grep system_server should help you find its PID): kill $(ps | grep system_server | awk '{print $2}'). That said: if you can identify the PID of that app, it should work the same way (but might have side effects). – Izzy Mar 08 '16 at 07:01
  • @Izzy Isn't it something like busybox killall system_server. I used that command before in an app I made. – Thomas Vos Mar 08 '16 at 07:24
  • @SuperThomasLab As I wrote, I didn't try that myself – so you could be right. Still, consider busybox is not a given on all devices – while kill usually is (IMHO; with no busybox it often points to toolbox or the like), as are ps and grep. Admittedly, awk again might depend on busybox, so one might have to do the kill manually in two steps then. – Izzy Mar 08 '16 at 07:32
  • Doesn't the above just restart userland, similar to am restart? (Related.) In any case: Eric, could you clarify what the problem is using am start? It works fine on my device for system apps. – Matthew Read Mar 08 '16 at 07:37
  • Thank you all. @Matthew Read, the problem is to have a custom system app restart completely. am restart does not work for me, but I may be using it wrong. I cite it, although I do not believe that is right. @all, it would be great to restart without external tooling, if possible. I guess that should be possible properly on such a fully-fledged OS ! – Eric Platon Mar 08 '16 at 08:21
  • 1
    Yeah I didn't expected am restart to work, but am force-stop & am start should. What happens when you try it? – Matthew Read Mar 08 '16 at 08:22
  • 6
    @EricPlaton : I can give my assurance that a system app can be force-stopped completely and one of its component can be started from scratch again. My answer here can show you that (hint: com.android.vending is a system app). // Can you precisely tell us what in that app are you trying to restart? am start -S COMPONENT would force-stop the app and then start your app's component. am force-stop PACKAGE and then am start or am startservice can help you as well. – Firelord Mar 08 '16 at 11:45
  • Thank you all, again! I have updated the question to address most of your feedback. I see that there are solutions, but don't you feel weird that you have to force stop a process to restart? That's one of the motivation for the question, and I should clarify it. – Eric Platon Mar 09 '16 at 00:43
  • 1
    I'll have to take a peek around the source code and see whether the Android system itself does a kill when updating an app. – Matthew Read Mar 09 '16 at 00:55

0 Answers0