Laurent Le Moux
Laurent Le Moux

Reputation: 241

Unable to run an Android application in a Docker container with Gitlab-CI

I've been struggling with a problem for a couple of days now and I hope somebody will be able to help me...

I try to use gitlab ci for my basic android application project.

Based on the following tutorial Setting up GitLab CI for Android projects, I wrote a .gitlab-ci.yml script :

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "25"
  ANDROID_BUILD_TOOLS: "25.0.1"
  ANDROID_SDK_TOOLS: "25.2.3"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1 libqt5widgets5
  - wget --quiet --output-document=tools.zip https://dl.google.com/android/repository/tools_r${ANDROID_SDK_TOOLS}-linux.zip
  - unzip tools.zip -d /sdk
  - rm tools.zip
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter android-${ANDROID_COMPILE_SDK}
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter platform-tools
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter build-tools-${ANDROID_BUILD_TOOLS}
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter extra-android-m2repository
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter extra-google-google_play_services
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter extra-google-m2repository
  - export ANDROID_HOME=/sdk
  - export PATH=$PATH:$ANDROID_HOME/platform-tools/
  - chmod +x ./gradlew

stages:
  - test

functionalTests:
  stage: test
  script:
    - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter sys-img-x86-google_apis-${ANDROID_COMPILE_SDK}
    - echo no | /sdk/tools/android create avd -n test -t android-${ANDROID_COMPILE_SDK} --abi google_apis/x86
    - /sdk/tools/emulator64-x86 -avd test -no-accel -no-window -no-audio &
    - adb wait-for-device
    - ./gradlew assembleDebug
    - ./gradlew assembleDebugAndroidTest
    - adb devices
    - adb shell service list
    - adb shell input keyevent 82 # Unlock device to allow install...
    - adb install -r app/build/outputs/apk/app-debug.apk
    - adb install -r app/build/outputs/apk/app-debug-androidTest.apk
    - adb shell am instrument -w -r -e debug false -e class com.example.myfirstapp.ManageItemListTest com.example.myfirstapp.test/android.support.test.runner.AndroidJUnitRunner
  artifacts:
    paths:
    - app/build/outputs/

My first difficulty - as somebody mentioned in the tutorial's comments - is that I have no way to manage and enable hardware acceleration in a docker container.

For now, I added the '-no-accel' option when lauching the AVD to get the script further executed.

But the main problem is that 'adb shell input keyevent 82' as well as the following installation commands fail.

The emulated device is running but is apparently missing lots of services as shown below :

Found 17 services:
0   media.camera: [android.hardware.ICameraService]
1   media.resource_manager: [android.media.IResourceManagerService]
2   media.player: [android.media.IMediaPlayerService]
3   media.sound_trigger_hw: [android.hardware.ISoundTriggerHwService]
4   media.radio: [android.hardware.IRadioService]
5   media.audio_policy: [android.media.IAudioPolicyService]
6   media.drm: [android.media.IMediaDrmService]
7   media.extractor: [android.media.IMediaExtractorService]
8   media.audio_flinger: [android.media.IAudioFlinger]
9   drm.drmManager: [drm.IDrmManagerService]
10  media.codec: [android.media.IMediaCodecService]
11  gpu: [android.ui.IGpuService]
12  SurfaceFlinger: [android.ui.ISurfaceComposer]
13  android.service.gatekeeper.IGateKeeperService: []
14  android.security.keystore: [android.security.IKeystoreService]
15  android.hardware.fingerprint.IFingerprintDaemon: []
16  batteryproperties: [android.os.IBatteryPropertiesRegistrar]

Only 17 services are started when 91 do actually run for an AVD locally launched on my desktop with Android Studio.

The package manager (package: [android.content.pm.IPackageManager]) is among the missing services...

Can anybody tell me what happens and what to do ?

Kind regards,

Laurent

Upvotes: 4

Views: 1194

Answers (2)

JumpingJack
JumpingJack

Reputation: 19

If you edit the gitlab runner .toml file and set "privileged:" to true, then you should be able to use hardware acceleration with the emulator.

This way you no longer have to use unsupported ARM images and instead can use intel images.

Upvotes: 0

Laurent Le Moux
Laurent Le Moux

Reputation: 241

I finally found a solution using an ARM system image. But one must also wait for all required services to start. Hereafter is the new script for anyone interested:

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "25"
  ANDROID_BUILD_TOOLS: "25.0.1"
  ANDROID_SDK_TOOLS: "25.2.3"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1 libqt5widgets5
  - wget --quiet --output-document=tools.zip https://dl.google.com/android/repository/tools_r${ANDROID_SDK_TOOLS}-linux.zip
  - unzip tools.zip -d /sdk
  - rm tools.zip
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter android-${ANDROID_COMPILE_SDK}
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter platform-tools
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter build-tools-${ANDROID_BUILD_TOOLS}
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter extra-android-m2repository
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter extra-google-google_play_services
  - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter extra-google-m2repository
  - export ANDROID_HOME=/sdk
  - export PATH=$PATH:$ANDROID_HOME/platform-tools/
  - chmod +x ./gradlew

stages:
  - test

functionalTests:
  stage: test
  script:
    - echo y | /sdk/tools/android --silent update sdk --no-ui --all --filter sys-img-armeabi-v7a-google_apis-${ANDROID_COMPILE_SDK}
    - echo no | /sdk/tools/android create avd -n test -t android-${ANDROID_COMPILE_SDK} --abi google_apis/armeabi-v7a
    - /sdk/tools/emulator64-arm -avd test -no-audio -gpu off -no-boot-anim -no-window &
    - adb wait-for-device
    - OUT=`adb shell service list | grep IPackageManager 2>&1 &` # Now wait for other services startup
    - until [[ $OUT  =~ .*IPackageManager.* ]]; do
    - sleep 20
    - OUT=`adb shell service list | grep IPackageManager 2>&1 &`
    - done
    - ./gradlew assembleDebug
    - ./gradlew assembleDebugAndroidTest
    - adb shell input keyevent 82 # Unlock device to allow install...
    - adb install -r app/build/outputs/apk/app-debug.apk
    - adb install -r app/build/outputs/apk/app-debug-androidTest.apk
    - adb shell am instrument -w -r -e debug false -e class com.example.myfirstapp.ManageItemListTest com.example.myfirstapp.test/android.support.test.runner.AndroidJUnitRunner
  artifacts:
    paths:
    - app/build/outputs/

For this basic example, the whole execution took more than 25 minutes to complete (with my test failing but that's another story... ;-)). It would be great to have hardware acceleration...

Kind regards,

Laurent

Upvotes: 2

Related Questions