Android Travis 测试失败,出现 ShellCommandUnresponsiveException

Android tests fail on Travis with ShellCommandUnresponsiveException

我们在 Travis 上的 Android 项目的第一次甚至第二次执行拉取请求时看到很多构建失败。但是,如果我们重新启动完全相同的构建足够多次,它最终会通过。

以下是失败时的错误:

:onebusaway-android:connectedAndroidTest
09:48:14 E/Device: Error during shell execution: null
Unable to install /home/travis/build/OneBusAway/onebusaway-android/onebusaway-android/build/outputs/apk/onebusaway-android-debug.apk
com.android.ddmlib.InstallException
at com.android.ddmlib.Device.installPackages(Device.java:927)
at com.android.builder.testing.ConnectedDevice.installPackages(ConnectedDevice.java:105)
at com.android.builder.internal.testing.SimpleTestCallable.call(SimpleTestCallable.java:125)
at com.android.builder.internal.testing.SimpleTestCallable.call(SimpleTestCallable.java:48)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.android.ddmlib.ShellCommandUnresponsiveException
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:513)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:390)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:359)
at com.android.ddmlib.Device.executeShellCommand(Device.java:566)
at com.android.ddmlib.Device.createMultiInstallSession(Device.java:987)
at com.android.ddmlib.Device.installPackages(Device.java:884)
... 9 more
com.android.builder.testing.ConnectedDevice > runTests[test(AVD) - 5.0.1] FAILED 
com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException
    at com.android.builder.testing.ConnectedDevice.installPackages(ConnectedDevice.java:108)
null
com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException
at com.android.builder.testing.ConnectedDevice.installPackages(ConnectedDevice.java:108)
at com.android.builder.internal.testing.SimpleTestCallable.call(SimpleTestCallable.java:125)
at com.android.builder.internal.testing.SimpleTestCallable.call(SimpleTestCallable.java:48)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.android.ddmlib.InstallException
at com.android.ddmlib.Device.installPackages(Device.java:927)
at     com.android.builder.testing.ConnectedDevice.installPackages(ConnectedDevice.java:105)
... 8 more
Caused by: com.android.ddmlib.ShellCommandUnresponsiveException
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:513)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:390)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:359)
at com.android.ddmlib.Device.executeShellCommand(Device.java:566)
at com.android.ddmlib.Device.createMultiInstallSession(Device.java:987)
at com.android.ddmlib.Device.installPackages(Device.java:884)
... 9 more
:onebusaway-android:connectedAndroidTest FAILED
FAILURE: Build failed with an exception.

我们正在 运行 使用 gradlew connectedTest 在 Travis 上的模拟器上进行测试。

这是我们的 .travis.yml:

# Test format changes to this .travis.yml file before submitting a PR with:
# http://lint.travis-ci.org/OneBusAway/onebusaway-android

language: android
jdk: oraclejdk7
# Turn off caching to avoid any caching problems
cache: false
# Use the Travis Container-Based Infrastructure (see #203)
sudo: false
env:
  global:
    - ANDROID_API_LEVEL=21
    - ANDROID_BUILD_TOOLS_VERSION=21.1.2
    - ANDROID_ABI=google_apis/armeabi-v7a

android:
  components:
    - platform-tools
    - tools
    - build-tools-$ANDROID_BUILD_TOOLS_VERSION
    - android-$ANDROID_API_LEVEL
    # For Google Maps API v1
    - addon-google_apis-google-$ANDROID_API_LEVEL
    # Google Play Services
    - extra-google-google_play_services
    # Support library
    - extra-android-support
    # Latest artifacts in local repository
    - extra-google-m2repository
    - extra-android-m2repository
    # Specify at least one system image
    - sys-img-armeabi-v7a-addon-google_apis-google-$ANDROID_API_LEVEL

before_script:
  # Create and start emulator
  - echo no | android create avd --force -n test -t "Google Inc.:Google APIs:"$ANDROID_API_LEVEL --abi $ANDROID_ABI
  - emulator -avd test -no-skin -no-audio -no-window &

script:
  - ./wait_for_emulator
  - ./gradlew connectedCheck -PdisablePreDex

# Integration with Gitter (https://gitter.im/OneBusAway/onebusaway-android)
notifications:
  webhooks:
    urls:
      - https://webhooks.gitter.im/e/493b93a98ed03a010c4c
    on_success: change  # options: [always|never|change] default: always
    on_failure: always  # options: [always|never|change] default: always
    on_start: false     # default: false

可以将Travis上的环境变量ADB_INSTALL_TIMEOUT设置为8分钟之类的值来避免这个问题。

例如,在您的 .travis.yml:

language: android
jdk: oraclejdk7
# Turn off caching to avoid any caching problems
cache: false
# Use the Travis Container-Based Infrastructure
sudo: false
env:
  global:
    - ANDROID_API_LEVEL=21
    - ANDROID_BUILD_TOOLS_VERSION=21.1.2
    - ANDROID_ABI=armeabi-v7a
    - ADB_INSTALL_TIMEOUT=8 # minutes (2 minutes by default)

android:
  components:
    - platform-tools
    - tools
    - build-tools-$ANDROID_BUILD_TOOLS_VERSION
    - android-$ANDROID_API_LEVEL

ShellCommandUnresponsiveExceptionAOSP issue 69735: Ddmlib is too agressive with timeouts in Device.java有关,这会导致ddmlib如果没有接收到输入会很快超时。上面的环境变量在 Travis VM 上延长了这个超时时间。

另外请确保您使用的是最新的 API 级别的 SDK 和构建工具(至少是以上版本),因为早期版本不允许设置此环境变量。

请注意,如果您的测试偶尔通过 Travis,这可能只会解决您的问题 - 如果您的构建总是失败,您可能还有其他问题。

编辑 2016 年 3 月

请注意,如果您仍然看到失败,尤其是在 API 级别 23 模拟器上,则可能是另一个模拟器超时问题导致了您的问题。

要解决此问题,您需要将 Gradle 插件更新到至少 2.0.0-beta3 - 例如:

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0-beta5'
}

详情见: