Search code examples
androiddockerandroid-emulatorautomated-testsgitlab-ci

Docker GitLab Error running Android Emulator: Cannot decide host bitness. 32 bits assumed


I am trying to automate Android Builds and Tests. The instrumentation test is not working because I get the following error while starting the android emulator in the Docker Runner:

Running with gitlab-ci-multi-runner 9.4.2 (6d06f2e) ...
Using Docker executor with image kmindi/android-ci:latest ...
...
$ chmod +x ./gradlew
$ echo no | avdmanager -v create avd --force --name test --abi google_apis/x86_64 --package 'system-images;android-25;google_apis;x86_64'
Do you wish to create a custom hardware profile? [no] $ export SHELL=/bin/bash && echo "no" | emulator -avd test -noaudio -no-window -gpu off -verbose -qemu
sh: 1: file: not found
sh: 1: file: not found
WARNING: Cannot decide host bitness because $SHELL is not properly defined; 32 bits assumed.
ERROR: 32-bit Linux Android emulator binaries are DEPRECATED, to use them
    you will have to do at least one of the following:

    - Use the '-force-32bit' option when invoking 'emulator'.
    - Set ANDROID_EMULATOR_FORCE_32BIT to 'true' in your environment.

    Either one will allow you to use the 32-bit binaries, but please be
    aware that these will disappear in a future Android SDK release.
    Consider moving to a 64-bit Linux system before that happens.

ERROR: Job failed: exit code 1

I created the following Dockerfile (https://hub.docker.com/r/kmindi/android-ci/)

FROM openjdk:8-jdk

ENV ANDROID_BUILD_TOOLS "26.0.1"
ENV ANDROID_SDK_TOOLS "25.2.5"
ENV ANDROID_HOME "/android-sdk"
ENV PATH=$PATH:${ANDROID_HOME}/tools:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/platform-tools

# Prepare dependencies
RUN mkdir $ANDROID_HOME \
&& apt-get update --yes \
&& apt-get install --yes wget tar unzip lib32stdc++6 lib32z1 libqt5widgets5 expect \
&& apt-get clean \
&& rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install sdk tools
RUN wget -O android-sdk.zip https://dl.google.com/android/repository/tools_r${ANDROID_SDK_TOOLS}-linux.zip \
&& unzip -q android-sdk.zip -d $ANDROID_HOME \
&& rm android-sdk.zip

# Workaround for 
# Warning: File /root/.android/repositories.cfg could not be loaded.
RUN mkdir /root/.android \
&& touch /root/.android/repositories.cfg

# Workaround for host bitness error with android emulator
# https://stackoverflow.com/a/37604675/455578
RUN mv /bin/sh /bin/sh.backup \
  && cp /bin/bash /bin/sh

# Add tools from travis
ADD https://raw.githubusercontent.com/travis-ci/travis-cookbooks/ca800a93071a603745a724531c425a41493e70ff/community-cookbooks/android-sdk/files/default/android-wait-for-emulator /usr/local/bin/android-wait-for-emulator
RUN chmod +x /usr/local/bin/android-wait-for-emulator

# Add own tools
COPY assure_emulator_awake.sh /usr/local/bin/assure_emulator_awake.sh
RUN chmod +x /usr/local/bin/assure_emulator_awake.sh


# Update platform and build tools
RUN echo "y" | sdkmanager "tools" "platform-tools" "build-tools;${ANDROID_BUILD_TOOLS}"

# Update SDKs
RUN echo "y" | sdkmanager "platforms;android-26" "platforms;android-25"

# Update emulators
RUN echo "y" | sdkmanager "system-images;android-25;google_apis;x86_64"

# Update extra
RUN echo "y" | sdkmanager "extras;android;m2repository" "extras;google;m2repository" "extras;google;google_play_services"

# Constraint Layout
RUN echo "y" | sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2"
RUN echo "y" | sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2"

In GitLab I use the following .gitlab-ci.yml:

image: kmindi/android-ci:latest

variables:
  ANDROID_COMPILE_SDK: "25"

before_script:
  - chmod +x ./gradlew

stages:
  - build
  - test

build:
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts:
    name: "App_{$CI_BUILD_ID}"
    expire_in: 1 week
    paths:
    - "app/build/outputs/apk/**/*.apk"
  except:
    - tags

test:unit:
  stage: test
  script:
    - ./gradlew test jacoco
  artifacts:
    name: "tests-unit-${CI_BUILD_NAME}_${CI_COMMIT_REF_NAME}_${CI_BUILD_REF}"
    expire_in: 1 week
    paths:
      - "**/build/reports/**"
    
test:instrumentation:25:
  stage: test
  script:
    - echo no | avdmanager -v create avd --force --name test --abi google_apis/x86_64 --package 'system-images;android-25;google_apis;x86_64'
    - export SHELL=/bin/bash && echo "no" | emulator -avd test -noaudio -no-window -gpu off -verbose -qemu &
    - adb wait-for-device
    - android-wait-for-emulator
    - export TERM=${TERM:-dumb}
    - assure_emulator_awake.sh "./gradlew cAT"
    - ./gardlew createDebugCoverageReport
  artifacts:
    name: "tests-instrumentation-${ANDROID_COMPILE_SDK}-${CI_BUILD_NAME}"
    expire_in: 1 week
    paths:
      - "**/build/reports/**"

The workaround proposed in https://stackoverflow.com/a/37604675/455578 (chaning of bash/sh) is not working (applied in the Dockerfile). Also setting the SHELL variable does not help. If I use emulator64-x86 instead of emulator I get the error command not found.

What am I missing?

Do I have to install other packages via apt?

Maybe it originates in the used openjdk-8 / debian stretch docker image?

Thanks :)


Solution

  • With SDK Revision 25.3.0 (March 2017) emulator was released separately from the SDK Tools (https://developer.android.com/studio/releases/emulator.html#25-3).

    Practically it means that now there is a new ${ANDROID_HOME}/emulator folder which you have to add to PATH. (NOTE: it has to be added before tools folder, because for whatever reason old tools/emulator binary is still there). Example:

    ENV PATH ${ANDROID_HOME}/emulator:${ANDROID_HOME}/tools:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/platform-tools:${PATH}