Try   HackMD

Run Android by command line without Android Studio

Prepare the environment

apt update -y
DEBIAN_FRONTEND=noninteractive apt install -y wget unzip openjdk-16-jdk

Download command-line tools

Goto https://developer.android.com/studio#downloads, select command-line tools

Note the number(ANDROID_SDK_TOOLS) will change if a new latest published.

export ANDROID_SDK_TOOLS=7583922
export ANDROID_SDK_ROOT=$PWD/sdk

wget --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
unzip android-sdk.zip
mkdir cmdline-tools/latest
mv cmdline-tools/{lib,bin} cmdline-tools/latest
export PATH=$ANDROID_SDK_TOOLS/cmdline-tools/latest/bin:$PATH
yes | sdkmanager --licenses

Build the apk

./gradlew assembleDebug

and the output is located at app/build/outputs/apk/debug/{your_app_name}_0.0.1_debug.apk

Choose suitable SDK

Choose wanted SDK, you can select the same API value as your phone system https://source.android.com/setup/start/build-numbers?hl=en

For example, I have Android-10 on my phone, so I choose (API level 29) SDK, the subversion are shown in sdkmanager --list

sdkmanager --list
sdkmanager --install "build-tools;29.0.3"
sdkmanager --install "platforms;android-29"
sdkmanager --install "system-images;android-29;google_apis;x86_64"

Run emulator

Note, emulator is ran on qemu, so it's not a recommand(?) to start the emulator in docker/podman.

# install qemu (needed?)
apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils

# create and start the image
avdmanager create avd -n test -k  "system-images;android-29;google_apis;x86_64"
sdk/emulator/emulator @test

I test on WSL, it works, but slower than Android studio, and always crash(Segmentation fault).

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

TODO

  • I cannot install haxm (a hardware accerlation tools) on win11
  • I disable "virtual machine platform" and reboot, otherwise the qemu cannot start.