Search code examples
androidandroid-uiautomator

How to close an Android app using UiAutomator?


How can I close an especific Android app using UiAutomator API? Like when you manually press the Recents button and swipe the app you want to close.


Solution

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