Skip to content

Instantly share code, notes, and snippets.

@trevjonez
Created October 28, 2020 03:47
Show Gist options
  • Save trevjonez/de6faa033979772222d79f06f69b805e to your computer and use it in GitHub Desktop.
Save trevjonez/de6faa033979772222d79f06f69b805e to your computer and use it in GitHub Desktop.
Android emulator helper functions
mkdir "$HOME/avd_logs"
function wait_for() {
name=$1
check_command=$2
i=0
while ! eval "$check_command"; do
(( i++ == 0 )) && printf %s "-- Waiting for $name ..." || printf '.'
sleep 1
if [ $i -gt $((5 * 60)) ] #5 minutes max spin time
then
(( i )) && printf '\n'
echo "$name took too long"
exit 1
fi
done
(( i )) && printf '\n'
}
function check_adb_count() {
expected_count=$1
if [ $expected_count -eq $(adb devices | grep -c emulator) ]
then
return 0
else
return 1
fi
}
function startEmulators() {
avd=$1
count=$2
i=0
while [ $i -lt $count ]
do
next=$((i + 1))
nohup emulator -avd "$1" -read-only -cores 2 -gpu auto -noaudio -no-window -no-boot-anim &> "$HOME/avd_logs/emu_$next.txt" &
wait_for "emulator $next to show up in adb" "check_adb_count $next"
i=$next
done
}
function stopEmulators() {
adb devices | \
grep emulator | \
cut -f1 | \
while read -r emulator
do
echo "Issuing kill command for $emulator"
adb -s "$emulator" emu kill
sleep 1
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment