Android Emulator
Written by masteryeti on .This article demonstrates how to run an Android emulator using a minimal portable install of the necessary Android tools and packages.
Setup
Create a new directory (i.e. "./android-emulator") and get into the directory. This will be the portable working directory for the emulator, including the system images and android tools.
mkdir android-emulator && cd android-emulator/
Then we download and extract the commandline tools zip-file from the official Android website:
download_url="https://developer.android.com/studio"
# extract zipfile url and filename from the $download_url page
zipfile_url="$(curl "$download_url" | grep -o -E 'https://.*commandlinetools[^"'"'"' ]*linux[^"'"'"' ]*\.zip([?#][^"'"'"' ]*|)')"
zipfile_file="${zipfile_url##*/}"
zipfile_file="${zipfile_file%%\?*}"
zipfile_file="${zipfile_file%%#*}"
if [ -n "$zipfile_url" ] || [ -n "$zipfile_file" ]
then
echo "error: Could not extract commandlinetools-linux zip-file (from $download_url). Try to download manually."
exit 1
fi
# download commandline tools (not studio)
wget "$zipfile_url"
# unzip the commandlinetools zip
unzip "$zipfile_file" -d tools || { echo "error: Could not extract $zipfile_file. Try to extract it manually."; exit 1; }
Now we must install some necessary SDK packages, we can also select our desired android version here (check available packages with: tools/cmdline-tools/bin/sdkmanager --sdk_root=. --list
).
# ensure these directories exist, necessary for emulator
mkdir emulator platform-tools platforms system-images
# install emulator, adb, and the latest android system image (fully featured including playstore etc.)
tools/cmdline-tools/bin/sdkmanager --sdk_root=. emulator platform-tools 'system-images;android-30;google_apis_playstore;x86_64'
Now we are ready to create a new emulator image (give it a custom/unique name). Note that the default location for this emulator is in ~/.android/avd/
and it is best to keep it like that (otherwise several export paths and additional command options must be set).
tools/cmdline-tools/bin/avdmanager create avd -n [name-of-emulator] -k "system-images;android-30;google_apis_playstore;x86_64" -c 1G
Run the emulator using (note that webcam0 does not refer to video0, check available webcams using emulator/emulator -webcam-list
):
emulator/emulator -avd [name-of-emulator] -camera-back webcam0 -memory 4096 -netfast -no-snapshot
Manipulate events for automation using the adb:
platform-tools/adb shell input text "type this text..."
platform-tools/adb shell input keyevent 0 # (see http://developer.android.com/reference/android/view/KeyEvent.html)
platform-tools/adb shell tap x y
platform-tools/adb shell swipe x1 y1 x2 y2 t_ms
platform-tools/adb shell swipe x1 y1 x2 y2 t_ms # (for a long press)
In order to send a custom video/image such as a picture file or live screen grab to the back camera of the emulator, you can setup a virtual webcam.