Skip to content

Instantly share code, notes, and snippets.

@sobujbd
Last active October 6, 2018 14:37
Show Gist options
  • Save sobujbd/976c16f1f46b4e3e6a1499193400a7dd to your computer and use it in GitHub Desktop.
Save sobujbd/976c16f1f46b4e3e6a1499193400a7dd to your computer and use it in GitHub Desktop.
Run Android Emulator (AVD) from Bash Script
#!/bin/bash
function selectAVD()
{
echo "Available AVDs:"
$emuDir/emulator -list-avds
echo "Which AVD would you like to use?"
read myAVD
ARRAY=($($emuDir/emulator -list-avds))
isIn=false
for i in $ARRAY; do
if [[ $i == $myAVD ]]; then
isIn=true
fi
done
if [[ isIn ]]; then
echo $myAVD > "$HOME/.myAVD/myAVD.tmp"
else
echo "Not an available emulator."
exit
fi
}
function selectDir()
{
echo "(Default is typically [your Android directory]/Sdk/emulator"
read -e -p "Directory: " emuDir
emuDir=$(eval echo $emuDir)
if [[ ! -f $emuDir/emulator ]]; then
echo "This is not a valid emulator directory."
exit
else
echo "Directory selected."
echo $emuDir > "$HOME/.myAVD/emuDir.tmp"
fi
}
if [[ ! -d $HOME/.myAVD ]]; then
mkdir $HOME/.myAVD
fi
if [[ -f "$HOME/.myAVD/emuDir.tmp" ]]; then
emuDir=$(<$HOME/.myAVD/emuDir.tmp)
else
echo "No AVD Directory selected. Please input your AVD directory."
selectDir
fi
if [[ -f "$HOME/.myAVD/myAVD.tmp" ]]; then
myAVD=$(<$HOME/.myAVD/myAVD.tmp)
else
if [[ $(eval echo $emuDir/emulator -list-avds) == '' ]]; then
echo "No available AVDs. Please create one in your Android Studio."
else
echo "No current AVD selected."
selectAVD
fi
fi
if [[ $1 == 'list' ]]; then
$emuDir/emulator -list-avds
echo 'Current emulator:' $myAVD
elif [[ $1 == 'setAVD' ]]; then
selectAVD
elif [[ $1 == 'setDir' ]]; then
selectDir
elif [[ $1 == '' ]]; then
$emuDir/emulator -avd $myAVD
elif [[ $1 == 'help' ]]; then
echo "Available commands:"
echo "list: lists available AVDs and displays current default AVD"
echo "setAVD: sets new Default AVD"
echo "setDir: sets new emulator directory"
echo "inputting no command will open current default AVD."
else
echo 'Unknown input'
fi
@sobujbd
Copy link
Author

sobujbd commented Oct 6, 2018

Thanks goes to Colin Brennan @SysVisionz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment