Want to restart Android application in a single Appium session without deleting/uninstalling it

Alternatively, you can use resetApp() method, which is going to clear user data and restart the app.

if we call "driver.closeApp(); it will close the app instance.
if we now call driver.runAppInBackground(1) how it will run the app in background and relaunch it when we already closed the application ?

@Pankaj7

with our app it is just look as start.

Please find below code snippet along with the error log.

DesiredCapabilities dr=new DesiredCapabilities();
dr.setCapability(“deviceName”, “Moto”);
dr.setCapability(“platformName”, “Android”);
dr.setCapability(“appPackage”, “com.google.android.youtube”);
dr.setCapability(“appActivity”, “com.google.android.youtube.HomeActivity”);
AppiumDriver driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”),dr);
WebElement title=driver.findElement(By.xpath("//android.widget.TextView[@text=‘Account (offline)’]"));
driver.closeApp();
try{driver.runAppInBackground(1);}catch (Exception e) {}
String str=title.getText();

Error Log : FAILED: TestClass
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 28 milliseconds
Build info: version: ‘3.4.0’, revision: ‘unknown’, time: ‘unknown’
System info: host: XXXXXXX’, ip: ‘XX.XX.XX.XXX’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_121’
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.google.android.youtube, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=XXXXXX, platform=LINUX, appActivity=com.google.android.youtube.HomeActivity, desired={appPackage=com.google.android.youtube, appActivity=com.google.android.youtube.HomeActivity, platformName=Android, deviceName=Moto}, platformVersion=6.0, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: XXXX-XXXX-XXXX-XXXX-XXXX
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

Please suggest, here the mentioned code is not working.

@Pankaj7 what line of code is problematic? i do not see in log…

also i see that code not quite in correct order. change to:

// start driver and close app
AppiumDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),dr);
driver.closeApp();

// start app
try{driver.runAppInBackground(1);}catch (Exception e) {}

// find element and get it text
WebElement title=driver.findElement(By.xpath("//android.widget.TextView[@text='Account (offline)']"));
String str=title.getText();

App is not starting using code

try{driver.runAppInBackground(1);}catch (Exception e) {}

@chetan_thummar replace with:

String packageName = ((AndroidDriver) driver).getCurrentPackage();
driver.terminateApp(packageName);
driver.activateApp(packageName);
3 Likes

Thank you @Aleksei It’s working perfectly with packageName

@Aleksei I am new in appium coding, and i have a scenario where i want to close the app and start again ( basically want to restart here), and i am trying to use your given code, in my test case, but getting compile time error - “The method runAppInBackground(Duration) in the type InteractsWithApps is not applicable for the arguments (int)”

And I don’t know where i have to use this package code( do i need to use it ??)

You should use duration of time now. See example
http://appium.io/docs/en/commands/device/app/background-app/

I think that should work, will try and revert you back …
Thanks for this helpful information

Thank you @Aleksei It’s working perfectly for me

@Aleksei I was using the below snippet and the driver is Android Driver

driver.closeApp();
String packageName = driver.getCurrentPackage();
driver.terminateApp(packageName);
driver.activateApp(packageName);

I got this error

[AndroidDriver] ‘com.android.launcher3’ is still running after 500ms timeout
An unknown server-side error occurred while processing the command. Original error: ‘com.android.launcher3’ is still running after 500ms timeout

Also used the below snippet and that also produced the error which is listed below

try{
driver.runAppInBackground(Duration.ofSeconds(10));
}catch (Exception e) {}

Error:

[AndroidDriver] Cannot activate ‘com.android.launcher3’. Original error: Error executing adbExec. Original error: ‘Command ‘C:\Users\xyz\AppData\Local\Android\Sdk\platform-tools\adb.exe -P 5037 -s emulator-5554 shell monkey -p com.android.launcher3 -c android.intent.category.LAUNCHER 1’ exited with code 252’; Stderr: ‘args: [-p, com.android.launcher3, -c, android.intent.category.LAUNCHER, 1] [AndroidDriver] arg: “-p” [AndroidDriver] arg: “com.android.launcher3” [AndroidDriver] arg: “-c” [AndroidDriver] arg: “android.intent.category.LAUNCHER” [AndroidDriver] arg: “1” [AndroidDriver] data=“com.android.launcher3” [AndroidDriver] data=“android.intent.category.LAUNCHER”’; Code: ‘252’

with latest appium 1.20+ use just:

driver.launchApp(); // actual behavior kill and start
1 Like

@Aleksei launchApp() is deprecated in Appium-Python-Client(v3.0.0). How can I achieve a restart in a single session in Appium-Python-Client(v3.0.0)?

@wreed But activate_app() does not launch the app if its already running. It will launch app if it does not find the given app running. I want to re launch the app at certain conditions irrespective its running or not.

Ok, so terminate_app and activate_app to restart the app does not work for you. Maybe no way then.

could you please tell me what is the name & version of the jar file you had used for this.

((AndroidDriver) driver).closeApp();

This solution by @Aleksei works for my issue of closing and reopening an application in a single appium session.