Creating and managing Android Virtual Devices using the terminal
Sunday, 17 September 2023 / last updated: Sunday, 26 May 2024
As a mobile developer, I create AVDs quite often. I’ve come to hate this experience because:
- having to start Android Studio (or IntelliJ IDEA) every time I want to do anything with an AVD is annyoing
- no easy way to share emulator config with other developers and testers
In this article I’ll show how I manage AVDs from the terminal. You don’t even need to have Android Studio / IntellIJ installed – Android command-line tools are enough.
Creating AVD
To view installed system images:
sdkmanager --list_installed | grep "system-images"
To create an AVD:
avdmanager create avd \
--sdcard '8192M' \ # or '8G'
--package 'system-images;android-33;google_apis;arm64-v8a' \
--name 'Pixel_7_API_33' \
--device 'pixel_7'
💡 To get the list of available device profiles that can be passed to the
--device
argument, runavdmanager list devices
. Drop in a-c
flag to get nicer output.
By default, it will create a new AVD in
$HOME/.android/avd
. Let’s see:
$ ls -h ~/.android/avd
Pixel_7_API_33.avd Pixel_7_API_33.ini
Running AVD
To run the newly created AVD:
emulator @Pixel_7_API_33
I don’t like the above command – it logs all output to the terminal tab in which you run it, rendering it unusable. I prefer the below:
(emulator @Pixel_7_API_33 1> /dev/null 2>&1 &) > /dev/null 2>&1
💡 Learn more about advanced options of the
emulator
command.
Customizing AVD
The AVD that we’ve created works, but there are some problems with it. Let’s fix them.
The first problem is that you can’t use your computer’s
keyboard to input text in the emulator. The fix is quite simple.
Change hw.keyboard = no
to
hw.keyboard = yes
in
~/.android/avd/Pixel_7_API_33.avd/config.ini
.
The second problem is that the default values of RAM and VM
heap size are too low. To increase them, edit
hw.ramSize
and vm.heapSize
in the same
config.ini
file. Unfortunately, the
avdmanager create avd
command doesn’t accept options
to change these values when creating the AVD.
I find the following values reasonable:
hw.ramSize = 4096M
vm.heapSize = 1024M
Changing GPS coordinates
There’s a AVD.conf
file in
~/.android/avd/Pixel_7_API_33.avd
which contains the
emulator’s GPS coordinates. By default, they point to Google’s HQ
in Mountain View, California.
[perAvd]
loc\altitude=5
loc\heading=0
loc\latitude=37.422
loc\longitude=-122.084
loc\velocity=0
You can edit the coordinates with any text editor and the changes will be picked up by the AVD the next time it starts up. To see the changes in real time instead, use this emulator console command:
adb emu geo fix 18.213 50.769
Somewhat counterintuitively, in the command above longitude comes before latitude.
Disabling saving quick-boot state on exit
I don’t like the quick-boot feature. It’s unreliable, has weird bugs, and saving the snapshot always takes too long when closing the emulator.
To disable quick-boot, add this line to
AVD.conf
:
set\saveSnapshotOnExit=1
Here 1 means “don’t save quick-boot state”, and 0 means “save quick-boot state”. Kind of like Unix exit codes, where 0 means success and non-zero means failure.
Removing snapshots
Snapshots live in the snapshots
directory in your
AVD’s directory. Removing them is simple:
rm -rf ~/.android/avd/Pixel_7_API_33.avd/snapshots
Learn more
2024-08-18 – frp (Fast Reverse Proxy)
instead of ngrok
2024-08-16 – Cirrus CI is the best CI system
out there
2024-08-14 – Going to Berlin for
Droidcon/Fluttercon
2024-06-25 – I was awarded Google Open Source Peer
Bonus
2024-06-04 – My journey to Google I/O
’24
2024-05-11 – GitHub Actions
beg for a supply chain attack
2024-03-19 – Writing a custom Dart
VM service extension (part 1)
2024-02-08 – On using smartphone for things
that make sense
2023-11-30 – Semantics in Flutter - under
the hood
2023-11-25 – Flutter Engine notes
2023-09-17 – Creating
and managing Android Virtual Devices using the terminal
2023-05-27 – Suckless
Android SDK setup
2023-05-26 – Let’s start over
2023-05-21 –
Short thought on “The Zen of
Unix”
2023-05-15 – Notes about “flutter
assemble”
2019-01-07 – Google Code-in
2018