Travis CI Android测试:没有连接设备

时间:2015-07-07 09:00:48

标签: android continuous-integration travis-ci

我正在尝试为Android设置Travis。到目前为止,运行构建似乎工作,但是当涉及到测试时,它会抱怨"没有连接的设备!"

: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!

这是我的.travis.yml,根据我的理解,我正在为测试创建和启动模拟器,就像documentation所说的那样。

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 &

你能告诉我我做错了什么以及如何解决它?

4 个答案:

答案 0 :(得分:13)

不幸的是我不能发表评论,因为我只想完成DominicJodoin的回答。正如DominicJodoin已经说过的那样,正确的缩进和更长的ADB_INSTALL_TIMEOUT是必要的。

在我看来你的模拟器正在运行但尚未准备好安装apk。使用- adb wait-for-device,您需要等到设备连接完毕。根据{{​​3}},这意味着:

  

请注意,此命令不会导致adb等待整个系统完全引导。因此,您不应将其添加到需要完全引导系统的其他命令。

请尝试用travis.yml中的- android-wait-for-emulator替换此行。

<强> 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

修改:修复了上面YAML代码段中的缩进。

答案 1 :(得分:8)

我认为您的问题是Travis CI尚未提供sys-img-armeabi-v7a-android-22图片。

确实如果您在Travis CI上运行以下命令:android list targetandroid-22的输出显示没有Tag/ABIs : no ABIs.

我建议您在此期间尝试在sys-img-armeabi-v7a-android-21上运行测试。

您可以查看一个带有单元测试的Android项目示例,我已经分叉并使用您的组件成功运行,但在Travis CI上使用sys-img-armeabi-v7a-android-21图像:

希望这有帮助!

编辑: android-22图片很快就会在Travis CI上提供。请参阅以下pull request

答案 2 :(得分:1)

我想使用更新的模拟器。不幸的是我无法在android-26或27上运行它,但我能够在android-25上运行它。 ABI名称已更改。这对我有用:

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 &

答案 3 :(得分:0)

我在J-Bossi回答中找到了密钥ADB_TIMEOUT_INSTALL位,它在before_script中启动了模拟器,就像Travis-CI目前推荐的那样,但是我遇到了VM内存不足的问题。因此,不是在构建运行时运行模拟器,而是更改了我的配置以运行构建,然后启动模拟器,然后运行测试。

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