Are Android Animations Annoying You?

If this title doesn’t make sense. No worries. Come here when you start writing functional automation test and need to run it against Android Emulator.

The Problem (Skip this section, if all you need is solution)

When you want to run your automation tests against android emulator, you would like to disable the animations. The animations are enabled by default on the emulators. They give a good user experience, but not the speed. As the animations take time during transitions. Do you want your automation tool to have good user experience? I hope not at all. What you want is — get the tests run asap and just check-in the code to the repository. If this was not enough, we also ran some visual comparison tests which would become flaky if the animations were enabled.

Finding

The key to the solution is to do a clean reboot after you have changed the settings. sudo reboot is what you have to avoid and embrace$ANDROID_HOME/platform-tools/adb shell “su 0 am start -a android.intent.action.REBOOT”.

Solution

Thanks to Mikhail Advani for acknowledging my finding and transforming it into a decent code (as well as reviewing and suggesting changes to this blog). A simple shell script that have worked for us 99.99% of the times:

#!/usr/bin/env bash
set -e

EMULATOR_PROCESS_ID_FILE_PATH=~/.Nexus_25.pid

function open_device()
{
$ANDROID_HOME/emulator/emulator -avd Nexus_25 -no-boot-anim -no-audio -no-window > /dev/null &

echo $! > $EMULATOR_PROCESS_ID_FILE_PATH #pid of previous command execution
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 10; done' #Wait until home screen gets displayed
}


function modify_settings()
{
$ANDROID_HOME/platform-tools/adb shell "settings put global window_animation_scale 0.0
"
$ANDROID_HOME/platform-tools/adb shell "settings put global transition_animation_scale 0.0"
$ANDROID_HOME/platform-tools/adb shell "settings put global animator_duration_scale 0.0"

#change whatever settings you wish to.
}

function restart_device()
{
$ANDROID_HOME/platform-tools/adb shell "su 0 am start -a android.intent.action.REBOOT"
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 10; done' #Wait until home screen gets displayed
}

function switch_off_device()
{
kill -9 $(cat $EMULATOR_PROCESS_ID_FILE_PATH)
#Why this? Because we don't trust adb emu kill
rm $EMULATOR_PROCESS_ID_FILE_PATH
}


open_device
modify_settings
restart_device
switch_off_device

https://sagarmaurya.dev | BuyMeACoffee: https://www.buymeacoffee.com/sagarising