Stopping an Android app from console

AndroidTestingAdb

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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>

Solution 3 - Android

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

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

adb shell am kill com.your.package

Solution 4 - Android

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.

Solution 5 - Android

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.

Solution 6 - Android

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

Solution 7 - Android

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.

Solution 8 - Android

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!

Solution 9 - Android

If all you are looking for is killing a package

pkill package_name 

should work

Solution 10 - Android

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

Solution 11 - Android

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...

Solution 12 - Android

pkill NAMEofAPP

Non rooted marshmallow, termux & terminal emulator.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhpiqueView Question on Stackoverflow
Solution 1 - AndroidEnrico RosView Answer on Stackoverflow
Solution 2 - AndroidChristopher OrrView Answer on Stackoverflow
Solution 3 - Androiddell116View Answer on Stackoverflow
Solution 4 - AndroidSomeone SomewhereView Answer on Stackoverflow
Solution 5 - AndroidThomasWView Answer on Stackoverflow
Solution 6 - AndroidPatrick ValsecchiView Answer on Stackoverflow
Solution 7 - AndroidZimbaView Answer on Stackoverflow
Solution 8 - Androidsinan bilkayView Answer on Stackoverflow
Solution 9 - AndroidsteveView Answer on Stackoverflow
Solution 10 - AndroidTarekBView Answer on Stackoverflow
Solution 11 - Androidmatto1990View Answer on Stackoverflow
Solution 12 - AndroidWilliam A. Krithinithis KriptoView Answer on Stackoverflow