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

Travis CI Android Tests: no connected devices

我正在尝试为 Android 设置 Travis。 运行 构建到目前为止似乎工作正常,但在测试时,它抱怨 "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!

这是我的。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 &

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

我认为您的问题是 sys-img-armeabi-v7a-android-22 图片在 Travis CI 上尚不可用 CI。

确实,如果您 运行 在 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.

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

我认为您的模拟器 运行 但尚未准备好安装 apk。使用 - adb wait-for-device 等待设备连接。根据 Documentation 这意味着:

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.

请尝试在您的 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

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

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

我想使用更新的模拟器。不幸的是,我无法让它在 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 &