31

I am trying to set up Travis for Android. Running the build seems to work so far, but when it comes to the tests, it complains about "No connected devices!"

:app:connectedAndroidTestDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:connectedAndroidTestDebug'.
> com.android.builder.testing.api.DeviceException: java.lang.RuntimeException: 
    No connected devices!

Here is my .travis.yml, and from what I understand, I am creating and starting an emulator for the tests, just the way as the documentation says.

language: android
android:
  components:
    # Uncomment the lines below if you want to
    # use the latest revision of Android SDK Tools
    # - platform-tools
    # - tools

    # The BuildTools version used by your project
    - build-tools-22.0.1

    # The SDK version used to compile your project
    - android-22

    # Additional components
    - extra-google-google_play_services
    - extra-google-m2repository
    - extra-android-m2repository
    # - addon-google_apis-google-19
    # - add-on
    # - extra

    # Specify at least one system image,
    # if you need to run emulator(s) during your tests
    - sys-img-armeabi-v7a-android-22
    # - sys-img-x86-android-17

  licenses:
    - 'android-sdk-license-.+'

  # Emulator Management: Create, Start and Wait
  before_script:
    - echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a
    - emulator -avd test -no-skin -no-audio -no-window &
    - android-wait-for-emulator
    - adb shell input keyevent 82 &

Can you tell me what I'm doing wrong and how to fix it?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Terry
  • 14,529
  • 13
  • 63
  • 88
  • Let's forget device emulation for the moment. Let's try on a real device. Can you actually confirm that a real device is recognised by the OS? You can do that by executing `adb devices` in an elevated terminal/cmd prompt. If you see some long string with digits, we can move one potential problem out of the way. – Shailen Jul 07 '15 at 14:27
  • 1
    This is hosted by Travis. I cannot connect a device or execute a shell there. All I can do is put the right command in the .tavis.yml file and trust that it starts the emulator. Unfortunately I don't even get log messages for that. – Terry Jul 08 '15 at 07:21

4 Answers4

13

Unfortunately i am not allowed to comment, as i just want to complete DominicJodoin's answer. Correct indentation and a longer ADB_INSTALL_TIMEOUT is necessary as DominicJodoin already stated.

In my opinion your Emulator is running but not ready to install an apk. With - adb wait-for-device you wait until the device connected. According to the Documentation this means:

Note that this command does not cause adb to wait until the entire system is fully booted. For that reason, you should not prepend it to other commands that require a fully booted system.

Try replacing this line with - android-wait-for-emulator in your travis.yml instead.

Travis.yml

language: android
jdk: oraclejdk7
cache:
  directories:
   - node_modules
sudo: false

android:
  components:
   # Uncomment the lines below if you want to
    # use the latest revision of Android SDK Tools
    # - platform-tools
    # - tools

    # The BuildTools version used by your project
    - build-tools-22.0.1

    # The SDK version used to compile your project
    - android-22

    # Additional components
    - extra-google-google_play_services
    - extra-google-m2repository
    - extra-android-m2repository
    # - addon-google_apis-google-19
    # - add-on
    # - extra

    # Specify at least one system image,
    # if you need to run emulator(s) during your tests
    - sys-img-armeabi-v7a-android-21
    # - sys-img-x86-android-17

  licenses:
   - 'android-sdk-license-.+'

env:
  global:
   # install timeout in minutes (2 minutes by default)
    - ADB_INSTALL_TIMEOUT=8

# Emulator Management: Create, Start and Wait
before_script:
  - echo no | android create avd --force -n test -t android-21 --abi armeabi-v7a
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &

script:
  - android list target
  - ./gradlew connectedAndroidTest
LeoColman
  • 6,950
  • 7
  • 34
  • 63
J-Bossi
  • 542
  • 6
  • 12
  • Thanks for your answer! I tried using the `android-wait-for-emulator` command, but I got an error in `:app:preDexDebug`: http://pastebin.com/VbjwVKxz It seems to be the same issue like here: http://stackoverflow.com/questions/22849720/android-gradle-build-script-returns-error-137-in-predexdebug , so I guess it's a memory issue. Do you know how I can extend the memory that is used for running it? – Terry Jul 20 '15 at 09:40
  • 2
    Hello, i'm not an expert on this topic and i don't know how to extend memory on travis. On my builds i also run the gradle build with this option `-PdisablePreDex"`. As stated [here](http://tools.android.com/tech-docs/new-build-system/tips#TOC-Improving-Build-Server-performance.) this improves your build server performance. As far as i know predexing runs a lot of processes which need memory. You benefit from predexing if you run incremental builds, which is not the case on a travis build-server. Also change your build.gradle as stated in the given link. – J-Bossi Jul 20 '15 at 10:19
  • Do I disable predexing in my `.travis.yml`? If yes, where? – Terry Jul 20 '15 at 15:19
  • 1
    Yes, you run the gradle build with this command `- ./gradlew connectedAndroidTest` . just add the Option there so you have `- ./gradlew connectedAndroidTest -PdisablePreDex` – J-Bossi Jul 20 '15 at 15:58
9

I think your problem is the sys-img-armeabi-v7a-android-22 image is not available yet on Travis CI.

Indeed if you run the following command on Travis CI: android list target, the output for android-22 shows no Tag/ABIs : no ABIs.

I would suggest you try running your tests on the sys-img-armeabi-v7a-android-21 in the meantime.

You can have a look at a sample Android project with unit tests I forked and ran successfully with your components but with sys-img-armeabi-v7a-android-21 image on Travis CI:

Hope this helps!

Edit: android-22 image should be available shortly on Travis CI. See the following pull request.

Dominic Jodoin
  • 2,538
  • 18
  • 21
  • Thanks for your answer! It's confusing that this works for you, but even after changing to `android-21` I get the same error. My .travis.yml now looks like this: http://pastebin.com/nfA1pj6r Any other ideas what could be wrong, or what else I could try? – Terry Jul 10 '15 at 08:14
  • @Terry @DominicJodoin Thanks for your input. I have the same error and with `sys-img-armeabi-v7a-android-21` i've got also the error... – David Jul 10 '15 at 09:44
  • Ok, I think I found your problem. The indentation of the `before_script:` and `script:` directives are wrong. Ensure there is no leading whitespace in front of them. In your current `.travis.yml` file, those directives appear like they are under the `android:` directive. Copy/paste this fixed `.travis.yml` file [here](http://pastebin.com/raw.php?i=Rz8U9v2D) and let me know how it goes. – Dominic Jodoin Jul 10 '15 at 12:47
  • It seems to run the emulator now, but fails to install the APK, with a ShellCommandUnresponsiveException. Any idea what that means / how to fix it? Log: http://pastebin.com/vJDt005V – Terry Jul 10 '15 at 14:17
  • Try setting the `ADB_INSTALL_TIMEOUT ` environment variable to a higher timeout value. See http://stackoverflow.com/a/28949723/213272 – Dominic Jodoin Jul 10 '15 at 14:36
  • I set it to 8 minutes, but it didn't change anything. Any other idea? – Terry Jul 13 '15 at 14:36
  • Can you post your `.travis.yml` file again? Else did/can you try with the sample project I posted above? – Dominic Jodoin Jul 13 '15 at 20:48
  • I just tried it with your example project. I get the same exception: http://pastebin.com/qrz0uV19 Does it mean that travis needs to be set up differently, somehow? – Terry Jul 16 '15 at 11:10
  • Hmm can you try forking my project publicly and show me the result on Travis CI? – Dominic Jodoin Jul 19 '15 at 02:30
1

I wanted to use a more recent emulator. Unfortunately I wasn't able to make it work on android-26 or 27, but I was able to make it work on android-25. The ABI names were changed. Here's what works for me:

language: android

jdk:
  - oraclejdk8

env:
  global:
    - ANDROID_BUILD_TOOLS_VERSION=26.0.2
    - ANDROID_ABI=arm64-v8a
    - ANDROID_TAG=google_apis
    - ANDROID_API_LEVEL=26
    - EMULATOR_API_LEVEL=25
    - ADB_INSTALL_TIMEOUT=8 # minutes (2 minutes by default)

android:
  components:
    # Uncomment the lines below if you want to
    # use the latest revision of Android SDK Tools
    - tools
    - platform-tools
    - tools

    # The BuildTools version used by your project
    - build-tools-$ANDROID_BUILD_TOOLS_VERSION

    # The SDK version used to compile your project
    - android-$ANDROID_API_LEVEL
    - android-$EMULATOR_API_LEVEL

    # Support library
    # Latest artifacts in local repository
    - extra-android-m2repository

    # Specify at least one system image,
    # if you need to run emulator(s) during your tests
    - sys-img-$ANDROID_ABI-$ANDROID_TAG-$EMULATOR_API_LEVEL

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

# Emulator Management: Create, Start and Wait
before_script:
  - android list targets
  - echo no | android create avd --force -n test -t "android-"$EMULATOR_API_LEVEL --abi $ANDROID_ABI --tag $ANDROID_TAG
  - emulator -list-avds
  - emulator -avd test -no-window &
  - android-wait-for-emulator
  - adb devices
  - adb shell input keyevent 82 &
Gavriel
  • 18,880
  • 12
  • 68
  • 105
0

I found the key ADB_TIMEOUT_INSTALL bit in J-Bossi answer, and it starts the emulator in before_script like Travis-CI is currently recommending, but I had issues with the VM running out of memory. So instead of running the emulator while the build is running, I changed my config to run the build, then start the emulator, then run the tests.

sudo: false

language: android

env:
  global:
    # switch glibc to a memory conserving mode
    - MALLOC_ARENA_MAX=2
    # wait up to 10 minutes for adb to connect to emulator
    - ADB_INSTALL_TIMEOUT=10

android:
  components:
    - platform-tools
    - extra-android-m2repository
    - build-tools-22.0.1
    - android-22
    - sys-img-armeabi-v7a-android-22

script:
  - ./gradlew assemble lint

after_script:
  # Emulator Management: Create, Start and Wait
  - echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
  # now run the tests
  - ./gradlew connectedCheck
  • the problem with this is that if script passes the build is considered successful, eventhough the connectedCheck might fail – Gavriel Jan 20 '18 at 22:22