Problem found. Unfortunately it is deep in the emulator (qemu) startup code. The following code registers the emulator instance with the adb server.
qemu-setup.c:
sprintf(tmp,"0012host:emulator:%d",base_port+1);
socket_send(s, tmp, 18+4);
This registers the emulator with the adb server but it makes two assumptions:
- that the user port is one less than the adb port for the emulator. Comments in the code also suggest that the user port was once assumed to be an even number. The server takes the adb port number supplied and subtracts one to get the user port. It then tries to connect to the user port to confirm that it really is an emulator.
- only 4 characters of the adb port number is sent to the adb server (the +4 in socket_send). Thus if the port number is greater than 9999 or less than 1000 then the wrong port number is sent.
I haven't searched the Android bug database for this yet but will search/report there later today.
However that does not make things work for a-e-p because we will need to support older emulators.
I tested a fix that uses the Jenkins port allocator to allocate 7000 for user port, 7001 for emulator adb port and then 8000 for the adb server. With that maven happily worked using -Dandroid.device=emulator.
[INFO] Found 1 devices connected with the Android Debug Bridge
[INFO] android.device parameter set to emulator
[INFO] checking device=emulator-6000_hudson_en-GB_160_WVGA_android-7_unknown_sdk, port=8000, name=hudson_en-GB_160_WVGA_android-7, isEmulator=true
[INFO] Emulator emulator-6000_hudson_en-GB_160_WVGA_android-7_unknown_sdk found.
Obviously to provide a proper fix we need to do some magic with the port allocator plugin. Assuming that it is acceptable that we require a newer port allocator plugin for a-e-p then I'll look to extend that to help us. Failing that we can likely make the existing allocator work with a bit of messing around.
I suspect this may be due to the android-emulator-plugin starting a new ADB instance for each running job. This means that the android-maven-plugin may not find the ADB instance that contains the emulator.
I have had a niggling suspicion that this may be the case for a while because I have seen matrix based testing fail at times and I suspect this happens when two emulators/ADB are running on the same slave at once.
It may be that android-maven-plugin never finds a second ADB instance running on a non-standard port and hence if there is any other (non-Jenkins related) ADB hanging around things will fail.
You may find that adding
to the jobs maven goals and options configuration will fix it (this specifies the exact emulator device to use). However I suspect that may fail if my hypothesis about my matrix build failures is correct.