简体   繁体   English

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

[英]Travis CI Android Tests: no connected devices

I am trying to set up Travis for Android. 我正在尝试为Android设置Travis。 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. 这是我的.travis.yml,根据我的理解,我正在为测试创建和启动模拟器,就像文档所说的那样。

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? 你能告诉我我做错了什么以及如何解决它?

Unfortunately i am not allowed to comment, as i just want to complete DominicJodoin's answer. 不幸的是我不能发表评论,因为我只想完成DominicJodoin的回答。 Correct indentation and a longer ADB_INSTALL_TIMEOUT is necessary as DominicJodoin already stated. 正如DominicJodoin所说,正确的缩进和更长的ADB_INSTALL_TIMEOUT是必要的。

In my opinion your Emulator is running but not ready to install an apk. 在我看来你的模拟器正在运行但尚未准备好安装apk。 With - adb wait-for-device you wait until the device connected. 使用- adb wait-for-device您需要等到设备连接完毕。 According to the Documentation this means: 根据文档,这意味着:

Note that this command does not cause adb to wait until the entire system is fully booted. 请注意,此命令不会导致adb等待整个系统完全引导。 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中的- android-wait-for-emulator替换此行。

Travis.yml 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

I think your problem is the sys-img-armeabi-v7a-android-22 image is not available yet on Travis CI. 我认为您的问题是Travis CI上尚未提供sys-img-armeabi-v7a-android-22图像。

Indeed if you run the following command on Travis CI: android list target , the output for android-22 shows no Tag/ABIs : no ABIs. 实际上,如果你在Travis CI上运行以下命令: android list targetandroid-22的输出显示没有Tag/ABIs : no ABIs.

I would suggest you try running your tests on the sys-img-armeabi-v7a-android-21 in the meantime. 我建议你尝试在sys-img-armeabi-v7a-android-21上同时运行测试。

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

Hope this helps! 希望这可以帮助!

Edit: android-22 image should be available shortly on Travis CI. 编辑: android-22图片应该很快就可以在Travis CI上使用。 See the following pull request . 请参阅以下拉取请求

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. 不幸的是我无法在android-26或27上运行它,但我能够在android-25上运行它。 The ABI names were changed. ABI名称已更改。 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 &

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. 我在J-Bossi的答案中找到了关键的ADB_TIMEOUT_INSTALL位,并且像Travis-CI当前推荐的那样在before_script启动了模拟器,但是我遇到了VM内存不足的问题。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM