Home » Android » How to run Android UI tests properly on CircleCI 2.0?

How to run Android UI tests properly on CircleCI 2.0?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Trying to upgrade my Android project to CircleCI 2.0. Everything is fine, but having trouble of running Android UI tests with emulator.

  • Log says com.android.builder.testing.api.DeviceException: No connected devices!
  • I’ve actually downloaded an emulator image and set it up (coz CircleCI’s default Android Docker image doesn’t come with emulator): sdkmanager "system-images;android-22;default;armeabi-v7a" && echo "no" | avdmanager create avd -n test -k "system-images;android-22;default;armeabi-v7a" && (emulator64-arm -avd test -noaudio -no-boot-anim -no-window -accel on) &
  • adb devices returns no device 🙁
How to&Answers:

Example 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

Note:

  • Some certain architecture image is not provided by Google, e.g. API level 26 doesn’t have ARM EABI v7a System Image, that’s why I choose system-images;android-22;default;armeabi-v7a above. To see which images are available, run command sdkmanager --list --verbose | grep system-images.

  • You need to set an environment variable LD_LIBRARY_PATH with lib64 and qt path, otherwise you’ll probably encounter ERROR: Could not load OpenGLES emulation library [lib64OpenglRender] or error while loading shared libraries: libQt5Widgets.so.5: cannot open shared object file: No such file or directory
    Exited with code 127
    . This is due to a bug from Android SDK.

  • To run a command in the background on CircleCI, it’s not like the usual way just append & to the end of command, that will be killed by the hangup (HUP) signal eventually. The correct way is to say background: true.

Answer:

Following is an example config.yml with which i managed to get my Android espresso tests working some time back using circle ci. May be useful for someone.

Location of the config.yml: create .circleci directory at root of the project and keep config.yml inside that.

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