#!/bin/sh set -ex CONTAINERRUNTIMES='docker' # currently not enabled podman nerdctl for CONTAINERRUNTIME in $CONTAINERRUNTIMES / do type $CONTAINERRUNTIME 2>/dev/null && break test "$CONTAINERRUNTIME" = / && { echo "install a container runtime (e.g ${CONTAINERRUNTIMES//\ /\/})" >&2 exit 2 } done HASHES='md5sum cksum sha1sum base64 uuencode' for HASH in $HASHES / do type $HASH 2>/dev/null && break test "$HASH" = / && { echo "install checksum (e.g ${HASHES//\ /\/})" >&2 exit 3 } done DockerfileContent(){ cat << 'DOCKERFILEEOF' FROM debian:latest ARG YESACCEPT=n RUN test "$YESACCEPT" = "y" || { printf "\033[31;1;4m%s\n%s\033[0m " "FAILED TO BUILD CONTAINER: You did not ACCEPT THE UPSTREAM LICENSE" " -> export YESACCEPT=y" >&2; exit 1; } RUN apt-get update -y && apt-get install -y make openjdk-17-jdk-headless unzip zip wget curl ENV ANDROID_SDK_ROOT="/opt/android" ENV BUILD_TOOLS_LATEST="$ANDROID_SDK_ROOT/cmdline-tools/latest" SHELL ["/bin/bash", "-c"] RUN BUILD_TOOLS="$(realpath -m "$BUILD_TOOLS_LATEST/..")";\ mkdir -p "$BUILD_TOOLS";\ cd "$BUILD_TOOLS";\ pwd;\ for LATESTTOOLS in \ "$(curl https://developer.android.com/studio#command-line-tools-only | grep -e 'https://dl.google.com/android/repository/commandlinetools-linux-.*_latest.zip' | cut -f2 -d'"')" \ 'https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip' ;\ do \ echo "testing to get '$LATESTTOOLS'";\ echo test "${LATESTTOOLS:0:22}" = "https://dl.google.com/" -a "${LATESTTOOLS:(-11)}" = "_latest.zip" ;\ test "${LATESTTOOLS:0:22}" = "https://dl.google.com/" -a "${LATESTTOOLS:(-11)}" = "_latest.zip" || { echo "unsure of URL $LATESTTOOLS correct, skipping it..." >&2; continue; }; \ wget -O cmdline-tools.zip "$LATESTTOOLS" ;\ unzip cmdline-tools.zip && break || { echo "error downloading working cmdline-tools.zip"; exit 1; };\ done;\ ls ;\ ls cmdline-tools;\ rm cmdline-tools.zip;\ mv -v cmdline-tools "$BUILD_TOOLS_LATEST" || true ENV PATH="$BUILD_TOOLS_LATEST/bin:$PATH" #TODO make this automatic ENV JAVA_HOME="/usr/lib/jvm/java-17-openjdk/" ENV LIBRARY_PATH="$LIBRARY_PATH:$BUILD_TOOLS_LATEST/lib" RUN echo you selected to accept the licenses/TOS #RUN echo "$YESACCEPT" | sdkmanager --install "build-tools;33.0.2" #RUN echo "$YESACCEPT" | sdkmanager --install "platforms;android-33" DOCKERFILEEOF } diff Dockerfile <(DockerfileContent) 2>/dev/null > /dev/null || { read -p 'reset/start Dockerfile[Y/n]' YES test "$YES" = "n" && { echo "aborting..." >&2; exit 1; } DockerfileContent > Dockerfile } IMAGE=android-app:"$($HASH Dockerfile | tr -cd '[a-zA-Z0-9]' | head -c 12)" $CONTAINERRUNTIME build --build-arg YESACCEPT="$YESACCEPT" --tag "$IMAGE" . SOCKET="$($CONTAINERRUNTIME context inspect | grep '"Host":' | cut -d'"' -f4)" test "${SOCKET:0:8}" = "unix:///" || { echo "Container runtime socket needs to be a unix socket and thus mountable" >&2 exit 4 } $CONTAINERRUNTIME run -e YESACCEPT="$YESACCEPT" -v "${SOCKET:7}":"/var/run/docker.sock" -it --rm "$IMAGE"