11#! /usr/bin/env bash
22
3- # This file comes from https://github.com/appium/ruby_lib_core
4-
53# This script was copy-pasted from https://docs.microsoft.com/en-us/azure/devops/pipelines/languages/android?view=azure-devops#test-on-the-android-emulator
64# with some changes
75
@@ -13,16 +11,48 @@ echo "y" | ${ANDROID_HOME}/tools/bin/sdkmanager --install "$emulator"
1311${ANDROID_HOME} /tools/bin/avdmanager list
1412
1513# Create emulator
16- echo " no" | ${ ANDROID_HOME} /tools/bin/avdmanager create avd -d " Nexus 6" -n testemulator -k " ${ emulator} " --force
14+ echo " no" | $ANDROID_HOME /tools/bin/avdmanager create avd -d " Nexus 6" -n $ANDROID_AVD -k " $emulator " -c 1500M --force
1715
1816echo ${ANDROID_HOME} /emulator/emulator -list-avds
1917
2018echo " Starting emulator"
2119
2220# Start emulator in background
23- nohup ${ANDROID_HOME} /emulator/emulator -avd testemulator -no-boot-anim -no-snapshot > /dev/null 2>&1 &
24- ${ANDROID_HOME} /platform-tools/adb wait-for-device shell ' while [[ -z $(getprop sys.boot_completed | tr -d ' \r ' ) ]]; do sleep 1; done; input keyevent 82'
25-
26- ${ANDROID_HOME} /platform-tools/adb devices
27-
28- echo " Emulator started"
21+ nohup $ANDROID_HOME /emulator/emulator -avd $ANDROID_AVD -no-snapshot > /dev/null 2>&1 &
22+
23+ adb wait-for-device get-serialno
24+ secondsStarted=` date +%s`
25+ TIMEOUT=360
26+ while [[ $(( `date +% s` - $secondsStarted )) -lt $TIMEOUT ]]; do
27+ # Fail fast if Emulator process crashed
28+ pgrep -nf avd || exit 1
29+
30+ pstat=$( adb shell ps)
31+ if ! [[ $pstat =~ " root " ]]; then
32+ # In recent APIs running `ps` without `-A` only returns
33+ # processes belonging to the current user (in this case `shell`)
34+ pstat=$( adb shell ps -A)
35+ fi
36+
37+ if [[ $pstat =~ " com.android.systemui" ]]; then
38+ echo " System UI process is running. Checking services availability"
39+ if adb shell " ime list && pm get-install-location && echo PASS" | grep -q " PASS" ; then
40+ break
41+ fi
42+ fi
43+
44+ sleep 5
45+ secondsElapsed=$(( `date +% s` - $secondsStarted ))
46+ secondsLeft=$(( $TIMEOUT - $secondsElapsed ))
47+ echo " Waiting until emulator finishes services startup; ${secondsElapsed} s elapsed; ${secondsLeft} s left"
48+ done
49+
50+ bootDuration=$(( `date +% s` - $secondsStarted ))
51+ if [[ $bootDuration -ge $TIMEOUT ]]; then
52+ echo " Emulator has failed to fully start within ${TIMEOUT} s"
53+ exit 1
54+ fi
55+ echo " Emulator booting took ${bootDuration} s"
56+ adb shell input keyevent 82
57+
58+ adb devices -l
0 commit comments