Vincenzo
Vincenzo

Reputation: 6418

emulator package installation problem in a Docker image

I'm trying to build a Docker image to use in a GitLab CI pipeline to be used in free runner to build and test a Flutter app using Maestro.

When I build the image in the GitLab pipeline the job completes fine but when the image is used in the test pipeline it builds the .apk fine but /usr/local/android-sdk/platform-tools/adb wait-for-device fails with the message

$ /usr/local/android-sdk/platform-tools/adb wait-for-device
/usr/bin/bash: line 159: /usr/local/android-sdk/platform-tools/adb: No such file or directory

Checking the prints for the command RUN adb version || echo "ADB not found" in the image build pipeline I see

Step 43/47 : RUN adb version || echo "ADB not found"
 ---> Running in 28d7af8c8174
Android Debug Bridge version 1.0.41
Version 35.0.2-12147458
Installed as /usr/local/android-sdk/platform-tools/adb
Running on Linux 5.15.154+ (x86_64)
 ---> 934415d4f98f

When I build the image locally though it fails with the message

#29 [25/32] RUN yes "y" | sdkmanager --verbose "emulator"
Info: Parsing legacy package: /usr/local/android-sdk/cmdline-tools/latest                 
#29 0.597 Info: Parsing /usr/local/android-sdk/platform-tools/package.xml
#29 0.600 Info: Parsing /usr/local/android-sdk/platforms/android-34/package.xml
#29 0.603 Info: Parsing /usr/local/android-sdk/system-images/android-34/google_apis/x86_64/package.xml
Warning: Errors during XML parse:       ] 5% Fetch remote repository...         ..        
Warning: Additionally, the fallback loader failed to parse the XML.y...                   
Warning: Failed to find package 'emulator'10% Computing updates...              .         
#29 ERROR: process "/bin/sh -c yes \"y\" | sdkmanager --verbose \"emulator\"" did not complete successfully: exit code: 1
------
 > [25/32] RUN yes "y" | sdkmanager --verbose "emulator":
Warning: Failed to find package 'emulator'10% Computing updates...              
------
Dockerfile:131
--------------------
 129 |     RUN  yes "y" | sdkmanager "system-images;android-$ANDROID_VERSION;google_apis;$ANDROID_ARCHITECTURE"
 130 |     
 131 | >>> RUN yes "y" | sdkmanager --verbose "emulator"
 132 |     RUN  echo "no" | avdmanager create avd -n "$EMULATOR_NAME" -k "system-images;android-$ANDROID_VERSION;google_apis;$ANDROID_ARCHITECTURE"
 133 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c yes \"y\" | sdkmanager --verbose \"emulator\"" did not complete successfully: exit code: 1

In the Dockerfile I broke down the Android SDK installation in multiple steps to find out where it was the problem.

FROM ubuntu:22.04

ENV DEBIAN_FRONTEND="noninteractive"
ENV JAVA_VERSION="17"

ENV ANDROID_TOOLS_URL="https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip"
ENV ANDROID_ARCHITECTURE="x86_64"
ENV ANDROID_SDK_ROOT="/usr/local/android-sdk"
ENV ANDROID_BUILD_TOOLS_VERSION="30.0.3"
ENV EMULATOR_NAME="test_emulator" \
    EMULATOR_TIMEOUT=300 \
    HW_ACCEL_OVERRIDE="-accel off"  



ENV ANDROID_VERSION="34"
ENV GRADLE_VERSION="8.2"
ENV GRADLE_USER_HOME="/opt/gradle"
ENV GRADLE_URL="https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip"

ENV FLUTTER_VERSION="3.24.4"
ENV FLUTTER_CHANNEL="stable"
ENV FLUTTER_ROOT="/opt/flutter"
ENV FLUTTER_URL="https://storage.googleapis.com/flutter_infra_release/releases/$FLUTTER_CHANNEL/linux/flutter_linux_$FLUTTER_VERSION-$FLUTTER_CHANNEL.tar.xz"

ENV MAESTRO_VERSION="1.39.0"


# Install the necessary dependencies.
RUN apt-get update \
  && apt-get install --yes --no-install-recommends \
    openjdk-$JAVA_VERSION-jdk \
    curl \
    unzip \
    sed \
    git \
    bash \
    xz-utils \
    libglvnd0 \
    ssh \
    xauth \
    x11-xserver-utils \
    libpulse0 \
    libxcomposite1 \
    libgl1-mesa-glx \
    libxkbfile1 \
#  added for emulator
    iputils-ping \
    libc6 \
    libstdc++6 \
    libgcc1 \
    libpulse0 \
    libsdl1.2debian \
  && rm -rf /var/lib/{apt,dpkg,cache,log}

# # Install Maestro.
RUN mkdir -p /opt/maestro \
  && curl -L -o /tmp/maestro.zip "https://github.com/mobile-dev-inc/maestro/releases/download/cli-${MAESTRO_VERSION}/maestro.zip" \
  && unzip -q /tmp/maestro.zip -d /opt \
  && rm /tmp/maestro.zip
ENV PATH="/opt/maestro/bin:$PATH"

# Verify Maestro installation
RUN /opt/maestro/bin/maestro --version || echo "Maestro not found"


# Install Gradle.
RUN curl -L $GRADLE_URL -o gradle-$GRADLE_VERSION-bin.zip \
  && apt-get install -y unzip \
  && unzip gradle-$GRADLE_VERSION-bin.zip \
  && mv gradle-$GRADLE_VERSION $GRADLE_USER_HOME \
  && rm gradle-$GRADLE_VERSION-bin.zip

# Install Android SDK.
RUN apt-get update && apt-get install -y iputils-ping && apt-get clean
RUN mkdir -p /root/.android 
RUN mkdir -p /usr/local/android-sdk/cmdline-tools/latest
RUN mkdir -p /tmp
RUN  touch /root/.android/repositories.cfg 
RUN  curl -o android_tools.zip $ANDROID_TOOLS_URL 
RUN unzip -qq -d /tmp android_tools.zip&& \
    echo "Unzip successful" 
RUN ls /tmp
RUN mv /tmp/cmdline-tools/* /usr/local/android-sdk/cmdline-tools/latest
RUN ls -l $ANDROID_SDK_ROOT/cmdline-tools/latest
RUN  rm android_tools.zip /tmp
ENV PATH="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$PATH"
#RUN ping -c 4 google.com

RUN yes "y" | sdkmanager "build-tools;$ANDROID_BUILD_TOOLS_VERSION" > sdkmanager_output.log || cat sdkmanager_output.log
RUN sdkmanager --update
RUN  yes "y" | sdkmanager "platforms;android-$ANDROID_VERSION" 
RUN  yes "y" | sdkmanager "platform-tools"
RUN  yes "y" | sdkmanager "system-images;android-$ANDROID_VERSION;google_apis;$ANDROID_ARCHITECTURE"

RUN yes "y" | sdkmanager --verbose "emulator"
RUN  echo "no" | avdmanager create avd -n "$EMULATOR_NAME" -k "system-images;android-$ANDROID_VERSION;google_apis;$ANDROID_ARCHITECTURE"

# Update PATH
ENV PATH="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/emulator:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/platforms:$FLUTTER_ROOT/bin:$GRADLE_USER_HOME/bin:$PATH"

RUN ls -R $ANDROID_SDK_ROOT
RUN adb version || echo "ADB not found"
# RUN which emulator && which adb
RUN echo "path is: \n\n"
RUN echo "$PATH" | tr ':' $'\n'


# Install Flutter.
RUN curl -o flutter.tar.xz $FLUTTER_URL \
  && mkdir -p $FLUTTER_ROOT \
  && tar xf flutter.tar.xz -C /opt/ \
  && rm flutter.tar.xz \
  && git config --global --add safe.directory /opt/flutter \
  && flutter config --no-analytics \
  && flutter precache \
  && yes "y" | flutter doctor --android-licenses \
  && flutter doctor \
  && flutter upgrade --force\
  && flutter update-packages

# Verify installations
RUN flutter --version \
  && emulator -version \
  && adb version

Why sdkmanager doesn't find the "emulator" package to install? I see in other posts that for older version of the cmdline-tools the command sdkmanager --channel=3 emulator solved the problem, but I tried adding it and it fails

#21 [18/30] RUN sdkmanager --update
Warning: Errors during XML parse:       ] 5% Fetch remote repository...                   
#21 0.748 Warning: Additionally, the fallback loader failed to parse the XML.
[===                                    ] 10% Computing updates...              .         
#21 0.865 No updates available
#21 0.867 [=======================================] 100% Computing updates...             
#21 DONE 0.9s

#22 [19/30] RUN sdkmanager --channel=3 emulator
Warning: Errors during XML parse:                                                         
#22 0.517 Warning: Additionally, the fallback loader failed to parse the XML.
Warning: Failed to find package 'emulator'8% Computing updates...               .         
[===                                    ] 10% Computing updates...                        
#22 ERROR: process "/bin/sh -c sdkmanager --channel=3 emulator" did not complete successfully: exit code: 1
------
 > [19/30] RUN sdkmanager --channel=3 emulator:
[===                                    ] 10% Computing updates...                    
[===                                    ] 10% Computing updates...                    
------
Dockerfile:121
--------------------
 119 |     RUN sdkmanager --update
 120 |     # RUN sdkmanager --help
 121 | >>> RUN sdkmanager --channel=3 emulator
 122 |     # RUN  yes "y" | sdkmanager "build-tools;$ANDROID_BUILD_TOOLS_VERSION" 
 123 |     RUN  yes "y" | sdkmanager "platforms;android-$ANDROID_VERSION" 
--------------------
ERROR: failed to solve: process "/bin/sh -c sdkmanager --channel=3 emulator" did not complete successfully: exit code: 1

also RUN sdkmanager --help would make the build fail, after display the help.

Can you spot what I'm doing wrong?

Upvotes: 0

Views: 111

Answers (0)

Related Questions