LoginSignup
18
16

More than 5 years have passed since last update.

CI用にヘッドレスなAndroidエミュレータを複数台立ち上げるシェルスクリプト

Last updated at Posted at 2015-06-16

やりたいこと

  • AndroidのUIテストをやりたい。
    • 複数バージョンまとめてテストしたい。
    • CI環境で動かせるようにヘッドレスなエミュレータで実行したい。
    • シェルスクリプトでまとめてかけたら良いな。

いや、まてよ・・・・

  • テストを流し込むのは起動待ちした後にしないといけないけどどうやるんだ?
    • adb wait-for-deviceってたしかうまくいかないよな
    • そもそも、複数台立ち上げたときにエミュレータの起動待ちってどうやんの??

スクリプト本体

android sdkは事前に導入しておいてください。

start-multi-emulator.sh

#!/bin/sh

# android-19とandroid-22のエミュレータを作成
echo n | android create avd --force --name emulator-android-21 --target android-21 -c 50M -s WXGA800 --abi armeabi-v7a
echo n | android create avd --force --name emulator-android-19 --target android-19 -c 50M -s WXGA800 --abi armeabi-v7a

# エミュレータを実行
emulator -avd emulator-android-21 -no-skin -no-audio -no-window &
emulator -avd emulator-android-19 -no-skin -no-audio -no-window &

# 起動監視スクリプトを実行
./wait-for-boot.sh
wait-for-boot.sh
#!/bin/sh

spinstr='|/-\'
spin_index=0

spin_until () {
    while ! $@
    do
        spin_index=$(expr $(expr $spin_index + 1) % 4)
        printf "\r${spinstr:spin_index:1} "
        sleep 0.5
    done
    printf "\r"
}

adb_shell_getprop () {
    adb -s $1 shell getprop $2 | tr -d [:space:] # delete the whitespace
}

device_actually_ready () {
    [ "$(adb_shell_getprop $1 init.svc.bootanim)" = "stopped" ]
}

# エミュレータ一覧取得
adb start-server
DEVICES=$(adb devices | sed "1d;s/[[:space:]].*$//")

# エミュレータ毎に起動監視
for device in $DEVICES
do
    spin_until adb -s $device shell true 2> /dev/null
    # 起動確認するまでまつ
    spin_until device_actually_ready $device
    echo "$device is started"
    sleep 5
    # 起動後にロック解除
    adb -s $device shell input keyevent 82
done

実行方法

$ ./start-multi-emulator.sh

余談

  • CircleCIのVM内にcircle-androidってシェルスクリプトがあってそれが1台分のエミュレータ起動待ち処理を実装してたのでそれを流用しました
  • 複数台対応しないのかと問い合わせたところ自分でシェルスクリプト作れよ!って言われたので作りました。
18
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
16