Skip to content

Instantly share code, notes, and snippets.

@jin
Last active April 11, 2018 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jin/6babcbe600d8ee24c732494118f4bb27 to your computer and use it in GitHub Desktop.
Save jin/6babcbe600d8ee24c732494118f4bb27 to your computer and use it in GitHub Desktop.
Setup for Bazel's Android Instrumentation Tests on CI (Google Compute Engine)

Bazel Android Team

Setup Instructions for Android Instrumentation Tests with Bazel

Test project: https://github.com/googlesamples/android-testing

Tested with Bazel version 0.12.0rc2

Tested on fresh GCE Ubuntu 14.04 and 16.04 images with nested KVM support

Obtaining the project

$ git clone https://github.com/googlesamples/android-testing

Installing dependencies

$ sudo apt-get install -y maven xvfb qemu-system-x86 unzip cpu-checker

Installing Bazel

$ wget [https://releases.bazel.build/0.12.0/rc2/bazel-0.12.0rc2-installer-linux-x86_64.sh](https://releases.bazel.build/0.12.0/rc2/bazel-0.12.0rc2-installer-linux-x86_64.sh)
 
$ chmod u+x bazel-0.12.0rc2-installer-linux-x86_64.sh
 
$ ./bazel-0.12.0rc2-installer-linux-x86_64.sh --user
 
$ echo "source $HOME/.bazel/bin/bazel-complete.bash" >> ~/.bashrc
 
$ echo "export PATH=\"$HOME/bin:$PATH\"" >> ~/.bashrc
 
$ source ~/.bashrc
 
$ bazel version # verify Bazel version

Installing Android SDK and tools

$ wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
 
$ unzip -d sdk sdk-tools-linux-3859397.zip
 
$ cd sdk
 
$ yes | tools/bin/sdkmanager --licenses # accept license
 
$ tools/bin/sdkmanager --update
 
$ tools/bin/sdkmanager "emulator" "system-images;android-23;default;x86" "platform-tools" "build-tools;27.0.3"  "platforms;android-27" # test project only uses system images on api 23.

Setting up KVM

$ kvm-ok

$ sudo groupadd kvm
 
$ sudo usermod -a -G kvm $(whoami)
 
$ sudo chown root:kvm /dev/kvm
 
$ echo 'KERNEL=="kvm", NAME="%k", GROUP="kvm", MODE="0660"' | sudo tee --append /etc/udev/rules.d/65-kvm.rules
 
$ sudo reboot

Test run

$ cd ~/android-testing

# remove path attribute from android_sdk_repository in WORKSPACE

$ export ANDROID_HOME=$HOME/sdk

$ bazel test //... --spawn_strategy=local
INFO: Analysed 46 targets (0 packages loaded).
INFO: Found 37 targets and 9 test targets...
[1 / 10] 9 actions running
    Testing //ui/espresso/CustomMatcherSample:CustomMatcherSampleInstrumentationTest; 71s local
INFO: Elapsed time: 103.926s, Critical Path: 103.61s
INFO: Build completed successfully, 10 total actions
//ui/espresso/BasicSample:BasicSampleInstrumentationTest                 PASSED in 84.7s
//ui/espresso/CustomMatcherSample:CustomMatcherSampleInstrumentationTest PASSED in 95.0s
//ui/espresso/DataAdapterSample:DataAdapterSampleInstrumentationTest     PASSED in 91.4s
//ui/espresso/IdlingResourceSample:IdlingResourceSampleInstrumentationTest PASSED in 83.2s
//ui/espresso/IntentsAdvancedSample:IntentsAdvancedSampleInstrumentationTest PASSED in 79.0s
//ui/espresso/IntentsBasicSample:IntentsBasicSampleInstrumentationTest   PASSED in 83.3s
//ui/espresso/MultiWindowSample:MultiWindowSampleInstrumentationTest     PASSED in 90.0s
//ui/espresso/RecyclerViewSample:RecyclerViewSampleInstrumentationTest   PASSED in 84.8s
//ui/uiautomator/BasicSample:BasicSampleInstrumentationTest              PASSED in 103.6s</td>

Known issues

  1. If emulator dies or unified launcher throws an error, Bazel doesn’t print the emulator log to the stdout. The emulator and unified launcher logs can be found in /tmp.

  2. Headless testing requires --spawn_strategy=local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment