lxc init ubuntu:24.04 -c security.nesting=true android-emulator
lxc config device add android-emulator port5555 proxy listen=tcp:10.0.0.1:5555 connect=tcp:127.0.0.1:5555
lxc config device add android-emulator kvm unix-char source=/dev/kvm
lxc config device add android-emulator vhost-net unix-char source=/dev/vhost-net
lxc config device add android-emulator vhost-vsock unix-char source=/dev/vhost-vsock
lxc start android-emulator
sudo apt update
sudo apt install unzip openjdk-21-jdk-headless cpu-checker qemu-kvm --yes
sudo usermod -aG kvm ubuntu
Log out and log in
Check if KVM is usable
$ sudo kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
From: https://developer.android.com/studio#command-line-tools-only
$ wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
$ unzip commandlinetools-linux-11076708_latest.zip
$ rm commandlinetools-linux-11076708_latest.zip
$ cd cmdline-tools
$ mkdir -p ~/android/sdk
$ ./bin/sdkmanager --sdk_root=/home/ubuntu/android_sdk --list --include_obsolete
Note
The android-XX number in the above listing is the SDK level and not Android version number. Check https://apilevels.com/ for the correspondence between SDK level and Android version.
Tip
Android 9 x86 arch emulator image and all Android 11 arch images support running ARM binaries with builtin emulation. See https://developer.android.com/studio/releases/emulator#support_for_arm_binaries_on_android_9_and_11_system_images and https://android-developers.googleblog.com/2020/03/run-arm-apps-on-android-emulator.html
There are three native bridge technologies for running ARM binaries on x86: libhoudini from intel, libndk_translation from google and QEMU translation [source]
$ ./bin/sdkmanager --sdk_root=/home/ubuntu/android/sdk "platform-tools" "platforms;android-30" "system-images;android-30;google_apis;x86"
To list all device models
$ ./cmdline-tools/bin/avdmanager list device
$ ./cmdline-tools/bin/avdmanager create avd --package "system-images;android-30;google_apis;x86" --name avd30 --device 38
Update AVD config to include the full path
Edit key image.sysdir.1
in config.ini for the AVD file at located at .android/avd/avd30.avd/config.ini
Original
image.sysdir.1=ubuntu/android/sdk/system-images/android-30/google_apis/x86/
Updated
image.sysdir.1=/home/ubuntu/android/sdk/system-images/android-30/google_apis/x86/
$ ./android/sdk/emulator/emulator -avd avd30 -no-audio -no-boot-anim -no-window
Starting adb and check if running okay
$ ./android/sdk/platform-tools/adb shell
To shutdown emulator from adb shell
$ ./android_sdk/platform-tools/adb shell reboot -p
Use OpenGapps for Google Play services: https://opengapps.org/ (not needed for google_apis images)
C:\> adb connect 10.0.0.1:5555
C:\> adb shell
C:\> scrcpy.exe --no-audio --no-audio-playback
Or use QtScrcpy
In case of black screen edit config/config.ini UseDesktopOpenGL=1
Bluetooh emulation is supported on recent emulator versions and Android SDK level >= 31. The Bumble Android library can be used to interact with the Android Bluetooth stack.
- https://google.github.io/bumble/transports/android_emulator.html
- https://google.github.io/bumble/platforms/android.html
On supported emulator versions, bluetooth emulation is supported out of the box without needing attitional command line parameters to be passed to the emulator. A corresponding line can be seen in the emulator logs.
INFO | Activated packet streamer for bluetooth emulation
The emulator starts a netsimd process automatically. The netsimd process exposes a grpc service on a random port which we need later on. The port number can be found in the file $XDG_RUNTIME_DIRECTORY/netsimd.ini
. However on headless Ubuntu installes the XDG_RUNTIME_DIRECTORY
environment variable may not be defined in the first place. For such cases follow the steps below to create it.
uid=$(id -u)
sudo mkdir -p /run/user/$uid
sudo chown -R $uid:$uid /run/user/$uid
sudo chmod 0700 /run/user/$uid
The environment variable can either be hardcoded in .bashrc or exported for the current session or specified to the emulator on the command line.
export XDG_RUNTIME_DIR=/run/user/$uid
OR
$ XDG_RUNTIME_DIR=/run/user/1000 ./android/sdk/emulator/emulator -avd avd31_small -no-audio -no-boot-anim -no-window -writable-system
Bumble also reads in the grpc port from the ini file, hence pass the environment variable to the Python process too.
-
https://discuss.linuxcontainers.org/t/running-vm-s-inside-lxd-containers/14884/4
-
https://www.reddit.com/r/androiddev/comments/13kdma9/android_emulator_on_remote_machine/
-
https://developer.android.com/studio/run/emulator-commandline
-
https://gist.github.com/nhtua/2d294f276dc1e110a7ac14d69c37904f ***
-
https://docs.mitmproxy.org/stable/howto-install-system-trusted-ca-android/