I wanted to get espresso tests running under Jenkins, these instructions are for Jenkins running on Ubuntu (17.04) and you have already got Jenkins running and building Android projects. After some messing around I got it working. This is what you will need to do.
- Install the package “qemu-kvm”.
>sudo apt-get install qemu-kvm
- Add the Jenkins user to the kvm group.
>sudo usermod -a -G kvm jenkins
- Build the android-emulator plugin for Jenkins, the current version in the Jenkins plugins repository is too old to work. I found a fork on GitHub which has many of the issues fixed. The GitHub page is https://github.com/wbrunette/android-emulator-plugin.
Clone and build, using the “upgrade” branch.
>git clone https://github.com/wbrunette/android-emulator-plugin.git
>git checkout -b origin/upgrade
- Install the android-emulator plugin into Jenkins. The plugin can be found in target/android-emulator.hpi
- Install the “JUnit Plugin”.
- Restart Jenkins.
- Configure the project you want to run tests under.
- Scroll down to “Build Environment” and check “Run an Android emulator during build”.
- Fill in the options to match what I have below, depending on your app and tests, you may want to change the orientation to landscape and the android version/target abi. These ones work well with the current android version 26 images.
OPTION VALUE ALTERNATIVE VALUES Android OS version android-26 AVD Device NEXUS_7_W_SKIN NEXUS_7 Device locale en_US Any valid locale SD card size 64M Any size you need. Target ABI google_apis/x86 Emulator name suffix test Hardware Add extra hardware properties. hw.initialOrientation portrait landscape hw.lcd.height 1920 hw.lcd.width 1200 skin.name 1920×1200 Very important, otherwise the emulator will be too small. hw.keyboard yes hw.ramSize 1536 Post Build Delete emulator after build
- Add an “Execute Shell” build step, as the first build step. Enter the following command to stop the soft keyboard from appearing in editors and pushing items off the display.
$ANDROID_HOME/platform-tools/adb -s $ANDROID_AVD_DEVICE wait-for-device shell settings put secure show_ime_with_hard_keyboard 0
- Add the “connectedAndroidTest” to the “Invoke Gradle script” “Task” section.
- Add the “Publish JUnit test result report” post build step. Point “Test report XMLs” to the location of the build report. This will be
Where [appname] is the path of your app under build – usually “app” in a single target project.
- “Save” the configuration, and run a build.
The build will either complete without any failed tests, or it will fail. I have had it fail a few times without any real reason but it seems fairly stable. When looking at the project status page you will get a graph of the “Test Result Trend” and be able to look at the test results. A logcat.txt is also saved as an artifact.