【问题标题】:Travis CI Android Tests: no connected devicesTravis CI Android 测试:没有连接的设备
【发布时间】:2015-07-07 09:00:47
【问题描述】:

我正在尝试为 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 &

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

【问题讨论】:

  • 让我们暂时忘记设备仿真。让我们在真机上试一试。您真的可以确认操作系统可以识别真实设备吗?您可以通过在提升的终端/cmd 提示符中执行 adb devices 来做到这一点。如果您看到一些带有数字的长字符串,我们可以解决一个潜在问题。
  • 这是由 Travis 主持的。我无法在那里连接设备或执行 shell。我所能做的就是将正确的命令放在 .tavis.yml 文件中,并相信它会启动模拟器。不幸的是,我什至没有收到日志消息。

标签: android continuous-integration travis-ci


【解决方案1】:

不幸的是,我不允许发表评论,因为我只想完成 DominicJodoin 的回答。正如 DominicJodoin 所述,正确的缩进和更长的ADB_INSTALL_TIMEOUT 是必要的。

在我看来,您的模拟器正在运行,但尚未准备好安装 apk。使用- adb wait-for-device,您将等到设备连接。根据Documentation,这意味着:

请注意,此命令不会导致 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

【讨论】:

  • 感谢您的回答!我尝试使用android-wait-for-emulator 命令,但在:app:preDexDebug 中出现错误:pastebin.com/VbjwVKxz 这似乎与这里的问题相同:stackoverflow.com/questions/22849720/…,所以我猜这是内存问题。你知道我如何扩展用于运行它的内存吗?
  • 您好,我不是这方面的专家,也不知道如何在 travis 上扩展内存。在我的构建中,我还使用此选项 -PdisablePreDex" 运行 gradle 构建。如here 所述,这可以提高您的构建服务器性能。据我所知,predexing 运行了很多需要内存的进程。如果您运行增量构建,您将从 predexing 中受益,而 travis 构建服务器并非如此。还要按照给定链接中的说明更改您的 build.gradle。
  • 我是否在我的.travis.yml 中禁用预索引?如果是,在哪里?
  • 是的,您使用此命令 - ./gradlew connectedAndroidTest 运行 gradle 构建。只需在那里添加选项,这样你就有了- ./gradlew connectedAndroidTest -PdisablePreDex
【解决方案2】:

我认为您的问题是 sys-img-armeabi-v7a-android-22 图像在 Travis 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

【讨论】:

  • 感谢您的回答!令人困惑的是,这对您有用,但即使更改为android-21,我也会遇到同样的错误。我的 .travis.yml 现在看起来像这样:pastebin.com/nfA1pj6r 任何其他可能有问题的想法,或者我还能尝试什么?
  • @Terry @DominicJodoin 感谢您的意见。我有同样的错误,sys-img-armeabi-v7a-android-21 我也有错误...
  • 好的,我想我找到了你的问题。 before_script:script: 指令的缩进是错误的。确保它们前面没有前导空格。在您当前的 .travis.yml 文件中,这些指令看起来像是在 android: 指令下。复制/粘贴这个已修复的.travis.yml 文件here,让我知道它是怎么回事。
  • 现在好像可以运行模拟器了,但是安装APK失败,出现ShellCommandUnresponsiveException。知道这意味着什么/如何解决吗?日志:pastebin.com/vJDt005V
  • 尝试将ADB_INSTALL_TIMEOUT 环境变量设置为更高的超时值。见stackoverflow.com/a/28949723/213272
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    我在 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
    

    【讨论】:

    • 这个问题是如果脚本通过了构建被认为是成功的,即使connectedCheck可能会失败
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 2015-01-04
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多