268

Is it possible to stop an Android app from the console? Something like:

adb stop com.my.app.package

It would speed up our testing process so much. Right now we uninstall/install the app each time to make sure the manual test cases start with a clean state.

hpique
  • 119,096
  • 131
  • 338
  • 476

13 Answers13

760

The clean way of stopping the app is:

adb shell am force-stop com.my.app.id

This way you don't have to figure out the process ID.

Lothar
  • 12,537
  • 6
  • 72
  • 121
Enrico Ros
  • 7,627
  • 2
  • 13
  • 6
  • 39
    This should be the accepted answer for this problem. The question doesn't include a rooted device. – Dimitris Nov 01 '13 at 00:02
  • 13
    @Dimitris I wrote my answer above before the `force-stop` command existed. I updated my answer to point to this one. – Christopher Orr Nov 25 '13 at 13:49
  • 2
    Also I updated my answer with a good way of really providing a "clean slate" for running tests. – Christopher Orr Nov 25 '13 at 23:22
  • Important point that after this your BroadcastReceiver will not work (till next app open) https://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html – CAMOBAP Apr 21 '18 at 14:31
  • 1
    Tested on `Android 9.0 API Level 28`, the app still is displayed in the **Recent Apps** even though the `PID` is found regarding the app. Do you know how to stop the app **completely** which means even from the background apps? – talha06 Jan 20 '19 at 00:53
172

Edit: Long after I wrote this post and it was accepted as the answer, the am force-stop command was implemented by the Android team, as mentioned in this answer.

Alternatively: Rather than just stopping the app, since you mention wanting a "clean slate" for each test run, you can use adb shell pm clear com.my.app.package, which will stop the app process and clear out all the stored data for that app.


If you're on Linux:
adb shell ps | grep com.myapp | awk '{print $2}' | xargs adb shell kill

That will only work for devices/emulators where you have root immediately upon running a shell. That can probably be refined slightly to call su beforehand.

Otherwise, you can do (manually, or I suppose scripted):
pc $ adb -d shell
android $ su
android # ps
android # kill <process id from ps output>

stkent
  • 19,772
  • 14
  • 85
  • 111
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 6
    Couldn't get it to work (permission denied both for kill and su), but +1 for showing me I can do lots more than I thought with adb. – hpique Jun 25 '10 at 11:06
  • 9
    Ah, sounds like you don't have a rooted device. It definitely works on the emulator at least! :) – Christopher Orr Jun 25 '10 at 11:55
  • 1
    Maybe you have to remount the partition read-write and run adb as root: adb remount; adb root # that's just a stupid guess thought – Rorist Jun 25 '10 at 12:25
  • `adb shell ps | grep com.myapp | awk '{print $2}' | xargs adb shell su -0 kill` works for me – Roman Truba Mar 17 '21 at 21:14
  • Clear command is perfect, it does exactly what op wants, equivalent to swiping up in task manager screen. – Chemist Mar 22 '23 at 12:41
45

First, put the app into the background (press the device's home button)

Then....in a terminal....

adb shell am kill com.your.package
dell116
  • 5,835
  • 9
  • 52
  • 70
19

you can use the following from the device console: pm disable com.my.app.package which will kill it. Then use pm enable com.my.app.package so that you can launch it again.

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
  • 3
    On a non-rooted phone this seems to do nothing. – Robert Muil May 25 '12 at 15:09
  • I've checke it on a non-rooted phone. At first there are some permission denied messages and then it killed my DalvicVM app/service. Just give it a sec. – LikeYou May 09 '13 at 16:25
  • 1
    on a rooted device this works, and it has the added benefit of being able to keep the app/service down. This may be desirable for testing purposes. – miki Jul 21 '13 at 20:13
  • 2
    Note: You need to `adb shell`, then run `su` first. – kakyo Aug 15 '13 at 20:37
  • Warning, this will make the launcher icon shortcuts for that package disappear, even after re-enabling. – Elad Nava Nov 27 '14 at 13:23
  • This can be used on Android 2.3 or below to kill the app from the task. Since there is no way to kill an app. – ImMathan May 30 '17 at 13:19
  • Yes, this only works with root. Instead of `am force-stop ...` this is the only way that prevents the app to automatically restart it (e. g. in a kiosk mode) – Vertex Aug 23 '17 at 08:40
10

If you have access to the application package, then you can install with the -r option and it will kill the process if it is currently running as a side effect. Like this:

adb -d install -r MyApp.apk ; adb -d shell am start -a android.intent.action.MAIN -n com.MyCompany.MyApp/.MyActivity

The -r option preserves the data currently associated with the app. However, if you want a clean slate like you mention you might not want to use that option.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
7

If you target a non-rooted device and/or have services in you APK that you don't want to stop as well, the other solutions won't work.

To solve this problem, I've resorted to a broadcast message receiver I've added to my activity in order to stop it.

public class TestActivity extends Activity {
    private static final String STOP_COMMAND = "com.example.TestActivity.STOP";

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TestActivity.this.finish();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //other stuff...

        registerReceiver(broadcastReceiver, new IntentFilter(STOP_COMMAND));
    }
}

That way, you can issue this adb command to stop your activity:

adb shell am broadcast -a com.example.TestActivity.STOP
5

The "stop" command is implemented as force-stop; stops background app from running. If it's in foreground, it'll stop also: eg.

adb shell am force-stop com.android.providers.telephony

Clearing of packages also deletes their data eg.

adb shell pm clear com.android.providers.telephony

will delete all your sms

Be careful which one you choose.

Zimba
  • 2,854
  • 18
  • 26
4

adb shell killall -9 com.your.package.name

according to MAC "mandatory access control" you probably have the permission to kill process which is not started by root

have fun!

2

If all you are looking for is killing a package

pkill package_name 

should work

Irshad
  • 3,071
  • 5
  • 30
  • 51
steve
  • 39
  • 1
  • Note: this only works on newer Android versions. pkill is not available on my 4.4 emulator but is available on the Android 6 emulator. – Simon Warta Nov 27 '17 at 16:54
1

I tried all answers here on Linux nothing worked for debugging on unrooted device API Level 23, so i found an Alternative for debugging From Developer Options -> Apps section -> check Do Not keep activities that way when ever you put the app in background it gets killed

P.S remember to uncheck it after you finished debugging

TarekB
  • 757
  • 14
  • 20
0

In eclipse go to the DDMS perspective and in the devices tab click the process you want to kill under the device you want to kill it on. You then just need to press the stop button and it should kill the process.

I'm not sure how you'd do this from the command line tool but there must be a way. Maybe you do it through the adb shell...

matto1990
  • 3,766
  • 2
  • 27
  • 32
  • I've found a [code snippet on GitHub](https://gist.github.com/805605) on how to do this from the command line without running Eclipse and [extended it slightly](https://gist.github.com/3175409). – sschuberth Jul 25 '12 at 11:39
0

Here is what I used

adb shell pidof com.payjoy.status | xargs adb shell kill
Sooraj S
  • 291
  • 5
  • 13
-2
pkill NAMEofAPP

Non rooted marshmallow, termux & terminal emulator.

wscourge
  • 10,657
  • 14
  • 59
  • 80