Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save russell-shizhen/77e41709d35b1b3685aad325cf240339 to your computer and use it in GitHub Desktop.
Save russell-shizhen/77e41709d35b1b3685aad325cf240339 to your computer and use it in GitHub Desktop.
restart android emulator in command line on MacOS
#!/bin/bash -ex
# NOTE: ANDROID_HOME must be configured as an environment variable.
# Kill all the existing adb servers and start over a new adb server.
function restart_adb_server() {
echo "********************* Restarting adb ... *********************"
adb kill-server
sleep 2
adb start-server
sleep 2
echo "********************* Restarting adb done *********************"
}
function ensure_device_available() {
# Must turn off exit on error temporarily
set +e
adb devices | grep -q offline
if [ $? -eq 0 ]; then
restart_adb_server
adb devices | grep -q offline
if [ $? -eq 0 ]; then
echo "Device remains 'offline'. Please investigate!"
exit 1
fi
fi
set -e
}
function kill_all_emulators() {
# kill all the emulators
adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done
}
# restart emulator Pixel_2_API_26 with no GUI ("-no-window") and quick boot ("-no-snapshot").
# Need to use AVD manager to create an emulator with name Pixel_2_API_26 first of all.
function restart_emulator() {
restart_adb_server
kill_all_emulators
$ANDROID_HOME/emulator/emulator -avd Pixel_2_API_26 -no-window -no-snapshot &
sleep 30
ensure_device_available
}
restart_emulator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment