skip to Main Content

I’m working on a Dockerfile where I’m trying to create a container environment to run Java code that requires both ADB and AAPT executables. While I’ve been able to successfully execute ADB commands, I’m encountering issues when attempting to run AAPT commands inside the Docker container.

Here’s a simplified version of my Dockerfile:

FROM maven:3.8.6-eclipse-temurin-19-alpine as build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

FROM eclipse-temurin:19-jdk-alpine
VOLUME /tmp
RUN apk update && 
    apk add -v --no-cache android-tools
COPY --from=build /home/app/target/*.jar /project.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /project.jar"]

I’ve included the android-tools package in the container via the apk command, which allows me to run ADB commands successfully. However, I’m unable to execute AAPT commands, which are also required by my Java code.

Am I looking at the wrong package for AAPT, if so What is the correct package to run apk on to get the AAPT executable in the container?

If anyone has experience with using AAPT within a Docker container or any insights into what might be causing this issue, I’d greatly appreciate your help. Thank you!

3

Answers


  1. Since AAPT2 (Android Asset Packaging Tool) is part of the Android SDK Build-Tools, which is a component of the Android SDK… adding that to your Dockerfile should help.
    I will be using android-sdk-cmdline-tools-offline.

    # Use a base image
    FROM maven:3.8.6-eclipse-temurin-19-alpine as build
    
    # Set environment variables
    ENV ANDROID_SDK_ROOT /usr/local/android-sdk
    ENV BUILD_TOOLS_VERSION 34.0.0
    ENV CMDLINE_TOOLS_VERSION 9644228
    
    # Install dependencies
    RUN apk update && 
        apk add --no-cache wget unzip
    
    # Download and extract Android SDK command-line tools
    RUN wget https://dl.google.com/android/repository/commandlinetools-linux-${CMDLINE_TOOLS_VERSION}_latest.zip -O sdk-tools.zip && 
        mkdir -p ${ANDROID_SDK_ROOT}/cmdline-tools && 
        unzip sdk-tools.zip -d ${ANDROID_SDK_ROOT}/cmdline-tools/ && 
        mv ${ANDROID_SDK_ROOT}/cmdline-tools/cmdline-tools ${ANDROID_SDK_ROOT}/cmdline-tools/latest && 
        rm sdk-tools.zip
    
    # Add the Android SDK tools to the PATH
    ENV PATH ${PATH}:${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin
    
    # Install the Android Build Tools
    RUN yes | sdkmanager "build-tools;${BUILD_TOOLS_VERSION}"
    
    # Copy your project files
    COPY src /home/app/src
    COPY pom.xml /home/app
    
    # Build your project
    RUN mvn -f /home/app/pom.xml clean package
    
    # Setup the runtime stage
    FROM eclipse-temurin:19-jdk-alpine
    
    # Set environment variables
    ENV ANDROID_SDK_ROOT /usr/local/android-sdk
    ENV BUILD_TOOLS_VERSION 34.0.0
    
    # Install Android Build-Tools
    COPY --from=build ${ANDROID_SDK_ROOT} ${ANDROID_SDK_ROOT}
    
    # Add the Android SDK tools to the PATH
    ENV PATH ${PATH}:${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin:${ANDROID_SDK_ROOT}/build-tools/${BUILD_TOOLS_VERSION}
    
    # Setup a volume
    VOLUME /tmp
    
    # Copy the built jar file
    COPY --from=build /home/app/target/*.jar /project.jar
    
    # Set the entry point for your container
    ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /project.jar"]
    

    I added the environment variables (ANDROID_SDK_ROOT, BUILD_TOOLS_VERSION and CMDLINE_TOOLS_VERSION) to make it easier to reference the Android SDK root path and the build tools version. And I installed wget and unzip to download and extract the Android SDK command-line tools.

    That way, I can download and extract the Android SDK command-line tools to the specified ANDROID_SDK_ROOT, adding Android SDK cmdline-tools bin directory to the PATH.
    I used the sdkmanager to install the Android Build-Tools of the specified version (adjust BUILD_TOOLS_VERSION and CMDLINE_TOOLS_VERSION as necessary).

    In the runtime stage, I copied the entire Android SDK installation from the build stage to ensure that aapt and other build tools are available in the runtime environment. And I added the build-tools directory to the PATH to make the aapt executable available in the PATH in your runtime container.

    Login or Signup to reply.
  2. to get the AAPT executable in the container is

    android-sdk-cmdline-tools

    . The

    android-tools

    package only includes the ADB executable.
    To fix your Dockerfile, you can change the apk add command to the following:

    apk add -v --no-cache android-sdk-cmdline-tools
    

    Here is the updated Dockerfile:

    FROM maven:3.8.6-eclipse-temurin-19-alpine as build
    COPY src /home/app/src
    COPY pom.xml /home/app
    RUN mvn -f /home/app/pom.xml clean package
    
    FROM eclipse-temurin:19-jdk-alpine
    VOLUME /tmp
    RUN apk update && 
        apk add -v --no-cache android-sdk-cmdline-tools
    COPY --from=build /home/app/target/*.jar /project.jar
    ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /project.jar"]
    
    Login or Signup to reply.
  3. To include AAPT in your Docker container, you’ll need to install the Android SDK or standalone Android build tools package, as AAPT is usually bundled with these.

    Here’s an updated Dockerfile that installs the Android build tools, which include AAPT, in your container:

    FROM maven:3.8.6-eclipse-temurin-19-alpine as build
    COPY src /home/app/src
    COPY pom.xml /home/app
    RUN mvn -f /home/app/pom.xml clean package
    
    FROM eclipse-temurin:19-jdk-alpine
    VOLUME /tmp
    RUN apk update && 
        apk add -v --no-cache android-tools && 
        wget https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip -O /tmp/android-tools.zip && 
        unzip -d /usr/local/android /tmp/android-tools.zip && 
        rm /tmp/android-tools.zip && 
        /usr/local/android/cmdline-tools/bin/sdkmanager --install "build-tools;30.0.3" && 
        ln -s /usr/local/android/cmdline-tools/tools/bin/aapt /usr/bin/aapt
    COPY --from=build /home/app/target/*.jar /project.jar
    ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /project.jar"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search