Android instrumentation test with Bitbucket pipelane and Docker

1k views Asked by At

I am new in Docker, and I need help with instrumentation tests. I create a Dockerfile (get it from here and make little modifiers)

FROM ubuntu:14.04

# Install java7
RUN apt-get update && \
  apt-get install -y software-properties-common && \
  add-apt-repository -y ppa:webupd8team/java && \
  (echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select     true | /usr/bin/debconf-set-selections) && \
  apt-get update && \
  apt-get install -y oracle-java7-installer && \
  apt-get clean && \
  rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV JAVA7_HOME /usr/lib/jvm/java-8-oracle

# Install java8
RUN apt-get update && \
  apt-get install -y software-properties-common && \
  add-apt-repository -y ppa:webupd8team/java && \
  (echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections) && \
  apt-get update && \
  apt-get install -y oracle-java8-installer && \
  apt-get clean && \
  rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV JAVA8_HOME /usr/lib/jvm/java-8-oracle

# Install Deps
RUN dpkg --add-architecture i386 && apt-get update && apt-get install -y --    force-yes expect git wget libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5 lib32z1     libpulse0 python curl libqt5widgets5 && apt-get clean && rm -fr     /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Copy install tools
COPY tools /opt/tools

RUN chmod +x /opt/tools/android-accept-licenses.sh
RUN chmod +x /opt/tools/android-wait-for-emulator.sh

ENV PATH ${PATH}:/opt/tools

# Install Android SDK
RUN cd /opt && wget --output-document=android-sdk.tgz --quiet         https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz && \
  tar xzf android-sdk.tgz && \
  rm -f android-sdk.tgz && \
  chown -R root.root android-sdk-linux && \
  /opt/tools/android-accept-licenses.sh "android-sdk-linux/tools/android     update     sdk --all --no-ui --filter platform-tools,tools" && \
  /opt/tools/android-accept-licenses.sh "android-sdk-linux/tools/android update    sdk --all --no-ui --filter platform-tools,tools,build-tools-25.0.2,android-14,android-23,android-25,extra-android-support,extra-android-m2repository,extra-  google-m2repository,extra-google-google_play_services,sys-img-armeabi-v7a-  google_apis-23"

# Setup environment
ENV ANDROID_HOME /opt/android-sdk-linux
ENV PATH ${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools

RUN which adb
RUN which android

# Create emulator
RUN echo "no" | android create avd \
            --force \
            --device "Nexus 5" \
            --name nexus5_23 \
            --target android-23 \
            --abi google_apis/armeabi-v7a \
            --sdcard 512M

# Cleaning
RUN apt-get clean

# Start up the emulator
RUN ["/bin/bash", "-c", "SHELL=/bin/bash emulator -avd nexus5_23 -no-window & /opt/tools/android-wait-for-emulator.sh"]

# GO to workspace
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace

When I build it, it works, and emulator running. My bitbucket pipeline is

image: xxx/android-23:latest
pipelines:
  default:
    - step:
        script: 
          - bash ./gradlew cAT 

But all my pipelines filed with error:

com.android.builder.testing.api.DeviceException: No connected devices!

Is it possible to run instrumentation tests automatically? May be I must use not Docker, but something else?

1

There are 1 answers

0
Chris Gunawardena On

RUN commands are run when building the container, you can think of this as running the emulator and shutting down the computer. When you restart the container, it's not running any more. You need a 'CMD' as the last line, which gets run when the container is started. See this link for more info

It's not easy to get GUI apps working in docker, good luck and please post back if you get it working!