# Run Android by command line without Android Studio ## Prepare the environment ``` bash! 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. ``` bash 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 ``` bash ./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` ``` bash 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. ``` bash # 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). ![](https://i.imgur.com/MDua8DR.png) ## TODO * I cannot install haxm (a hardware accerlation tools) on win11 * I disable "virtual machine platform" and reboot, otherwise the qemu cannot start.