如何 运行 Android UI 在 CircleCI 2.0 上正确测试?

How to run Android UI tests properly on CircleCI 2.0?

正在尝试将我的 Android 项目升级到 CircleCI 2.0。一切都很好,但是用模拟器测试 运行 Android UI 有问题。

示例circle.yml

version: 2
jobs:
  build:
    docker:
      - image: circleci/android:api-26-alpha
    steps:
      - run:
          name: Setup emulator
          command: sdkmanager "system-images;android-22;default;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-22;default;armeabi-v7a"
      - run:
          name: Launch emulator
          command: export LD_LIBRARY_PATH=${ANDROID_HOME}/emulator/lib64:${ANDROID_HOME}/emulator/lib64/qt/lib && emulator64-arm -avd test -noaudio -no-boot-anim -no-window -accel on
          background: true
      - run:
          name: Run Tests
          command: ./gradlew :demo:connectedAndroidTest

:

  • Google 未提供某些特定架构图像,例如API 级别 26 没有 ARM EABI v7a 系统映像,这就是为什么我在上面选择 system-images;android-22;default;armeabi-v7a。要查看哪些图像可用,运行 命令 sdkmanager --list --verbose | grep system-images

  • 你需要设置一个环境变量LD_LIBRARY_PATH,路径为lib64qt,否则你可能会遇到ERROR: Could not load OpenGLES emulation library [lib64OpenglRender]error while loading shared libraries: libQt5Widgets.so.5: cannot open shared object file: No such file or directory Exited with code 127。这是由于 a bug from Android SDK.

  • To 运行 CircleCI后台的一个命令,不像通常的方式只是在命令末尾追加&,会被hangup杀死(HUP) 信号最终。正确的方法是 background: true.

以下是一个示例 config.yml,我使用 Android 一段时间前使用 circle ci 设法让我的浓缩咖啡测试工作。可能对某人有用。

config.yml 的位置:在项目的根目录下创建 .circleci 目录,并在其中保留 config.yml

version: 2
jobs:
  build:
    working_directory: ~/code
    docker:
      - image: circleci/android:api-28-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
         name: Chmod permissions
         command: sudo chmod +x ./gradlew
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
          name: Lists installed targets
          command: android list target
          environment:
          TERM: dumb
      - run:
          name: Show list of system-images
          command: sdkmanager --list --verbose | grep system-images
      - run:
          name: Setup Emulator
          command: sdkmanager "system-images;android-21;default;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-21;default;armeabi-v7a"
      - run:
          name: Launch Emulator
          command: |
                  cd ${ANDROID_HOME}/emulator;ls
                  export LD_LIBRARY_PATH=${ANDROID_HOME}/emulator/lib64:${ANDROID_HOME}/emulator/lib64/qt/lib
                  emulator -avd test -no-window -noaudio -no-boot-anim -no-window -accel on
          background: true
      - run:
          name: Wait emulator
          command: |
              circle-android wait-for-boot
              adb shell input keyevent 82
      - run:
          name: Run Espresso UI Tests
          command: ./gradlew :app:connectedDebugAndroidTest
      - store_artifacts:
          path: app/build/reports
          destination: reports
      - store_test_results:
          path: app/build/test-results