Appium fails to launch app on Android emulator with API 30+

Hello there!
Could you please help with unstable application launch on Android emulator with API 30, 31, 33? Maybe I’m doing something wrong with configuration, but there was no such issue with older APIs.

We have multiple Android emulators with different screen density and size.
Idea is to launch test cases on each emulator. Cycle is next: we start emulator & app with Appium, do the test and then kill emulator with adb. Then we do it for next emulator and so on.

We use next options:
UiAutomator2Options options = new UiAutomator2Options();
options.setNoReset(false);
options.setFullReset(true);
options.setNewCommandTimeout(Duration.ofMillis(0));
options.autoGrantPermissions();
options.setAvdReadyTimeout(Duration.ofMillis(300000));
options.setAvdLaunchTimeout(Duration.ofMillis(300000));
options.setUiautomator2ServerInstallTimeout(Duration.ofMillis(60000));
options.setAndroidInstallTimeout(Duration.ofMillis(100000));
options.setAppWaitForLaunch(false);
options.setAvd("–required emulator name–");
options.setAvdArgs("-no-snapshot -accel on");
options.setCapability(“unicodeKeyboard”, true);
options.setCapability(“resetKeyboard”, true);
options.setCapability(“androidDeviceReadyTimeout”, 120000);

Appium service is launched from terminal manually, so we simply use:
new AndroidDriver(applicationUrl, options);

After test is completed we use adb commands to get emulator name and kill it:
Runtime.getRuntime().exec(“adb devices”);
//parsing results
Runtime.getRuntime().exec(String.format(“adb -s %s emu kill”, emulatorName));

Problem #1 (Probably fixed).
Periodically io.appium.settings application is not launched printing no any meaningful error in logs. It just tells that appium application is not started during timeout.

Here on forum I found suggestion to uninstall a few appium apps from emulator and run the code again.
So we added try-catch logic for new driver creation.
If session creation fails in catch block we run next commands to uninstall appium apps:
Runtime.getRuntime().exec(“adb uninstall io.appium.uiautomator2.server”).waitFor();
Runtime.getRuntime().exec(“adb uninstall io.appium.uiautomator2.server.test”).waitFor();
Runtime.getRuntime().exec(“adb uninstall io.appium.settings”).waitFor();

Then we run the code again: new AndroidDriver(applicationUrl, options);

Problem #2.
With the fix above there is no problem any more with appium applications.
But! When session creation fails and we apply the fix on 2nd try sometimes target application will be launched visually (we see it running on emulator screen and can interact with it manually), but driver creation will fail with session not created exception again. Then application will be closed.

Additional Info.
Host OS: macOS with Apple M1, so arm64-v8a emulator images were used.
Target device: Android emulator, API 30, 31, 33.
How it was created (in case if something is missed): echo “no” | ./avdmanager create avd -n emulator_name --abi google_apis/arm64-v8a --package “system-images;android-33;google_apis;arm64-v8a”
Appium-Java version: 8.3.0
Appium version: 2.0.0-beta.57
UIAutomator2 version: 2.12.6

Also if we change reset options to opposite (options.setNoReset(true); options.setFullReset(false); ) and last execution failed with the problem above then this emulator will never launch app again. Running with reset options will be required to recover executions.

Could you provide any suggestion to get stability in application launch?

Could you provide a log for problem #2? Would prefer a git gist but just post here if you can’t do that.

Thank you for reply. Sorry, due to force majeure I was unable to reply earlier.

I played with some open source applications and realized that less heavy application is more stable it works. I used Google Authenticator app to reproduce issue and get the appium logs.

As I said previously I tried to run app in a loop on different emulators. So there are 2 log files:

In both logs I’m seeing Dropbox errors. In second log this one is bad:

[debug] [AppiumDriver@dc16] Encountered internal error running command: Error: Unable to download the app: Cannot download the file from Dropbox - google_auth.apk - Simplify your life Request failed with status code 500

So one suggestion would be to download the app and run locally.

Other errors I’m seeing are that the emulators have some trouble restarting, and when they do restart they may be in a bad state.

So second suggestion would be to try running the loop without restarting emulator, or try running on real device. It just seems like the emulators are a bit flaky, which has been my experience as well.