4

I'm trying to run for androidTests in CI process. I use VSTS build definition for this. Before testing I must start Android Emulator. I can use Shell script task for starting the emulator, but as I cannot start the process in background, the build flow cannot proceed to next step. I've tried all possibilities listed below:

nohup $ANDROID_HOME/tools/emulator @emulatorForUITests
nohup $ANDROID_HOME/tools/emulator @emulatorForUITests &
$ANDROID_HOME/tools/emulator @emulatorForUITests &

but the emulator is still running in foreground. Am I doing something wrong or is it VSTS security feature? How can I start the emulator in background?

UPDATE (answering questions in comments):

  • Starting emulator in foreground blocks build process therefore it must be non-blocking (background) task. The emulator starts in foreground (in UI) anyway. If I start it using the same script, but directly from Terminal instead of VSTS, the emulator starts, tests are run, everything is ok
  • I use my own agent (on macOS)

UPDATE2: build log from VSTS:

(...)
2017-07-14T10:11:31.8195010Z emulator: WARNING: userdata partition is resized from 550 M to 800 M
2017-07-14T10:11:31.8202870Z 
2017-07-14T10:11:31.8217150Z Hax is enabled
2017-07-14T10:11:31.8230410Z Hax ram_size 0x40000000
2017-07-14T10:11:31.8246000Z HAX is working and emulator runs in fast virt mode.
2017-07-14T10:11:34.0594360Z coreaudio: Could not initialize record - Unknown Audiodevice
2017-07-14T10:11:34.0627730Z coreaudio: Could not initialize record - Unknown Audiodevice
2017-07-14T10:11:34.0649440Z audio: Failed to create voice `goldfish_audio_in'
2017-07-14T10:11:34.0671380Z qemu-system-i386: warning: opening audio input failed
2017-07-14T10:11:34.0875870Z coreaudio: Could not initialize record - Unknown Audiodevice
2017-07-14T10:11:34.0888750Z coreaudio: Could not initialize record - Unknown Audiodevice
2017-07-14T10:11:34.0902400Z audio: Failed to create voice `adc'
2017-07-14T10:11:34.5066290Z emulator: emulator window was out of view and was recentered
2017-07-14T10:11:34.5078580Z 

As emulator is not a background process VSTS waits for emulator process to finish.

anakkin
  • 731
  • 1
  • 6
  • 21

2 Answers2

0

You can start the emulator in the background if you run the command with & and send the output to /dev/null. We did some trial and error and it only worked if we routed the output away, I'm guessing the bash task doesn't know it should complete otherwise.

Here's an example of the line we run to start the emulator:

$ANDROID_HOME/tools/emulator -avd emulator &>/dev/null &
Ann-Sofi
  • 176
  • 1
  • 9
  • ```$ANDROID_HOME/tools/emulator -avd emulator &>/dev/null &``` does the same as ```$ANDROID_HOME/tools/emulator @emulator &>/dev/null &```. It does not work with VSTS builds – anakkin Jul 01 '18 at 17:36
  • @anakkin maybe there's a difference in what agent we're running the jobs on? Our VSTS agent is running macOS and for us that's the version that ended up working (together with a script that waits for the emulator to start). – Ann-Sofi Jul 02 '18 at 08:23
  • Good point, maybe MS has updated something in the agents. I'll check this out and come back with the results soon. – anakkin Jul 02 '18 at 08:28
  • Still having this issue in Dec 2018. I wouldn't want to use exe/ windows for the android UI tests – Ovi Trif Dec 14 '18 at 13:26
0

I did it this way and it works

  1. Use this tool https://filehippo.com/download_advanced_bat_to_exe_converter/ or any other to convert a batch file to exe , put the below cmd and convert to exe which comes out as say "startavd.exe"

start cmd /C "%ANDROID_HOME%/emulator/emulator @vstsavd -wipe-data-no-boot-anim"

  1. Create a windows service by running below command in Admin cmd

sc create startavd binPath="pathto\startavd.exe"

  1. Use this tool https://www.coretechnologies.com/products/ServiceSecurityEditor/ to give start access to service accounts for startavd service

  2. Now in Vsts build task add a script task that executes below command

sc start startavd

That's it..

Even though the script fails the avd would start in the machine, then run your tests and at the end kill the avd by

  1. add a script task that executes below command

Taskkill /IM qemu-system-x86_64.exe /F

Kaleem
  • 41
  • 5