How to close an Android app using UiAutomator?

Building on the solution from @user597159 I got the following to close all applications on a Pixel 2 for Firebase Test Lab (i.e. the "walleye" device type):

private void killAllApps() throws Exception {
    boolean keepSwiping = true;
    int maxSwipeAttempts = 10;
    uiDevice.pressRecentApps();

    for (int swipeAttempt=0; swipeAttempt<maxSwipeAttempts && keepSwiping; swipeAttempt++) {
        int height = uiDevice.getDisplayHeight();
        int width = uiDevice.getDisplayWidth();
        uiDevice.swipe(width / 2, height / 2, width, height / 2, 50);

        UiObject clearAll1 = uiDevice.findObject(new UiSelector().text("Clear all"));
        UiObject clearAll2 = uiDevice.findObject(new UiSelector().textStartsWith("Clear all"));
        UiObject clearAll3 = uiDevice.findObject(new UiSelector().textContains("Clear all"));
        UiObject clear = clearAll1.exists() ? clearAll1 :
                (clearAll2.exists() ? clearAll2 : clearAll3);

        if (clear.exists()) {
            Logger.debug(TAG, "Attempting to close app by killAllApps and found clear=all button on swipeAttempt=" + swipeAttempt);
            clear.click();
            keepSwiping = false;
        } else {
            Logger.debug(TAG, "Attempting to close app by killAllApps but have to keep swiping swipeAttempt=" + swipeAttempt);
            keepSwiping = true;
        }
    }
}

Note on the Pixel 2, it is spelled "Clear all" not "CLEAR ALL".

I could not get some of the other solutions to work. I got UiObjectNotFoundException for the following:

app = uiDevice.findObject(new UiSelector().textContains("SDK Test App"));

And also for:

app = uiDevice.findObject(new UiSelector().className(com.locuslabs.android.sdk.SdkTestApplication.class));

In other words app.exists() returned false for these approaches that attempted to swipe up on the app to close on Pixel 2.


Better way (not device, OS version, UI or orientation specific):

Runtime.getRuntime().exec(new String[] {"am", "force-stop", "pkg.name.of.your.app"});

Tested and working on a Nexus 5X with android 6.0


This is how I kill all android apps at once with uiautomator:

public static void killApps()
{
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    try
    {
        device.pressHome();
        device.pressRecentApps();

        // Clear all isn't always visible unless you scroll all apps down
        int height = device.getDisplayHeight();
        int width = device.getDisplayWidth();
        device.swipe(width/2,height/2, width/2, height, 50);

        UiObject clear = device.findObject(new UiSelector()
            .resourceId("com.android.systemui:id/button")
            .text("CLEAR ALL")
        );
        if (clear.exists())
        {
            clear.click();
        }
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
    catch (UiObjectNotFoundException e)
    {
        e.printStackTrace();
    }
}

The best option would be to use getUiDevice.pressRecentApps, this will load up the recent apps for you, then take a screenshot using the uiautomator viewerso you have a view of the xml of the screen that has been loaded. You can then use this xml to select the object you wish to swipe using

UiObject app = new UIObject(new UiSelector().resourceId("The id of the app");
app.swipeLeft(100); 

or right

This should be able to close your app. The xml will depend on what style of android you are using and the device.