📝

Adroid CLIでゼロからエミュレータで実行するまで

に公開

AIに関係なく純粋なAndroid CLIの操作手順のメモ。一応、JDKのインストールから始めている。

SDKのダウンロードはほぼほぼ自動で行われる。
ライセンスへの同意手段だけなさそうなので cmdline-tools のみインストールしている。

JDKを入れる

これがないと始まらない。

JDKのディストリビューションとしてはTemurinがデファクトみたいなところがある。

https://adoptium.net/ja/temurin/releases?version=17&os=any&arch=any

Android開発としてはバージョン17が使われる。25に対応したっぽいけどひとまずは。

以下、Debian/Ubuntuの手順。MacはHomebrewで。Windowsは知らん。

sudo apt install -y wget apt-transport-https gpg
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/adoptium.gpg > /dev/null
echo "deb https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | sudo tee /etc/apt/sources.list.d/adoptium.list
sudo apt update
sudo apt install temurin-17-jdk

Linux Mintの場合は、 VERSION_CODENAME ではなく UBUNTU_CODENAME になる。

Android CLIを入れる

グローバルにインストールするのは更新時に面倒がありそうなのでローカルにインストール。

https://developer.android.com/tools/agents/android-cli/archive?hl=ja

Linux:

curl -fsSL https://dl.google.com/android/cli/latest/linux_x86_64/install.sh | bash

Mac:

curl -fsSL https://dl.google.com/android/cli/latest/darwin_arm64/install.sh | bash

~/.local/bin にインストールされる。

念の為更新。以降は手癖で更新。

android update

Android SDKを入れる

自分でANDROID_HOMEを指定しない場合、android infoで表示された場所がSDKのインストール先になる。

$ android info
sdk: /home/jhoshina/Android/Sdk
version: 0.7.15331015
launcher_version: 0.7.15331015

必要なものをインストール。パッケージ名は android sdk list --all でわかる。

が、必要になったらダウンロードされる感じなので、 cmdline-tools/latest のみ入れておけばよい。

android sdk install platform-tools cmdline-tools/latest

あとで必要になるのでライセンスへの同意を行っておく。

$HOME/Adroid/Sdk/cmdline-tools/latest/bin/sdkmanager --licenses

プロジェクトの作成

createコマンドで作成。ヘルプを確認する。

$ android create --help
Usage: android create [-h] [--verbose] [--list] [--minSdk=api]
                      --name=applicationName [-o=dest-path] [template-name]
Create a new Android project
      [template-name]      The template name
  -h, --help               Show this help message and exit.
      --minSdk=api         The 'minSdk' supported by the application (default
                             is defined in the template)
      --name=applicationName
                           The name of the application (e.g. 'My Application')
  -o, --output=dest-path   The destination project directory path (default is
                             '.')
      --verbose            Enables verbose output
      --list               List all available templates
$ android create --list
Template name                 Template description    Tags
empty-activity (default)      Empty Activity          compose,activity,agp-9
$ android create --name sandbox-app
INFO: Processing template 'empty-activity'
ERROR: Cannot create template: Directory (or file) '.' is not empty
ERROR: Failed to create project 'Empty Activity' due to previous error(s)

空ディレクトリで行うか、 -o でディレクトリを指定する。

$ android create --name sandbox-app -o=android
INFO: Processing template 'empty-activity'
INFO: Installing Android SDK package 'platforms/android-36' to '/home/vscode/Android/Sdk'
https://dl.google.com/android/repository/platform-36_r02.zip...
INFO: Successfully created project 'Empty Activity' at 'android'

エミュレータの作成

android emulator create コマンドで作成する。

デフォルトで medium_phone が選択される。現状細かい指定はできない。

$ android emulator create --help
Usage: android emulator create [-h] [--list-profiles] [--profile=<profile>]
Creates a virtual device
  -h, --help                Show this help message and exit.
      --list-profiles       lists the device profiles that can be used to
                              create a device
      --profile=<profile>   Create a device with a specified profile. Creates
                              the appropriate device for the selected profile
                              (i.e. watch, phone, XR, etc)
$ android emulator create --list-profiles
large_desktop
medium_desktop
medium_phone
medium_tablet
small_desktop
small_phone
$ android emulator create
https://dl.google.com/android/repository/sys-img/google_apis_playstore/x86_64-36_r07.zip...
Successfully created device 'medium_phone' with profile 'medium_phone'.
$ android emulator list
medium_phone

エミュレータの起動・終了

android emulator start <device name> で起動する。
終了は android emulator stop でデバイス名は不要。

$ android emulator start medium_phone
Emulator process 843819 started, log file location: '/home/jhoshina/.android/medium_phone/emulator.log'
Waiting for virtual device 'medium_phone' to fully start (295 seconds left)
Virtual device successfully started as 'emulator-5554'

$ android emulator stop              
Waiting for virtual device 'emulator-5554' to fully terminate (55 seconds left)

エミュレータでの実行

$ android run -h
Usage: android run [-h] [--debug] [--activity=PARAM] [--device=PARAM]
                   [--type=PARAM] [--apks=PARAM[,PARAM...]]...
Deploy an Android Application
      --activity=PARAM   The activity name
      --apks=PARAM[,PARAM...]
                         The paths to the APKs
      --debug            Run in debug mode
      --device=PARAM     The device serial number
  -h, --help             Show this help message and exit.
      --type=PARAM       The component type (ACTIVITY, SERVICE, etc.)

apkの指定が必要。プロジェクトのビルドコマンドはないので gradlew を使って普通にビルドする。
gradle本体とbuild-toolsはここで自動的にインストールされる。

./gradlew assembleDebug

apkを指定して実行。エミュレータは事前に起動しておく必要がある。

android run --apks=build/app/outputs/apk/debug/app-debug.apk

Discussion