# The OxygenFlow Android image (oxygenflow#19): a JDK, the Android SDK and
# NDK, Gradle, cargo-ndk, the crates and Gradle dependencies of the
# OxygenHub app, so its build and tests run in a job with no network.
# Built by ./build.sh, which fetches and checks the downloads first.
FROM rust:1.97.1-slim-bookworm

RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates openjdk-17-jdk-headless unzip make git pkg-config \
 && rm -rf /var/lib/apt/lists/*

# The guest's PATH is fixed and has no /usr/local/cargo/bin, and a rustup
# proxy would need RUSTUP_HOME: link the toolchain's own binaries instead.
RUN rustup target add aarch64-linux-android x86_64-linux-android \
 && cargo install --locked --version 4.1.2 --root /opt/cargo-ndk cargo-ndk \
 && rm -rf /usr/local/cargo/registry /usr/local/cargo/git \
 && tc=$(ls -d /usr/local/rustup/toolchains/*/ | head -1) \
 && for b in cargo rustc rustdoc; do ln -sf "$tc/bin/$b" /usr/local/bin/$b; done \
 && ln -sf /opt/cargo-ndk/bin/cargo-ndk /usr/local/bin/cargo-ndk

# The SDK: Google's command-line tools (checked by build.sh), then the
# platform, build tools and NDK the app names, licences accepted here.
ENV ANDROID_HOME=/opt/android-sdk JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
COPY dl/cmdline-tools.zip /tmp/cmdline-tools.zip
RUN mkdir -p "$ANDROID_HOME/cmdline-tools" \
 && unzip -q /tmp/cmdline-tools.zip -d /tmp/ct \
 && mv /tmp/ct/cmdline-tools "$ANDROID_HOME/cmdline-tools/latest" \
 && rm -rf /tmp/ct /tmp/cmdline-tools.zip \
 && yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null \
 && "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --install \
      "platforms;android-37.0" "build-tools;36.0.0" "ndk;30.0.16248370" >/dev/null

# Gradle itself (checked by build.sh): jobs run `gradle`, not the wrapper,
# which would download its distribution.
ARG GRADLE_VERSION
COPY dl/gradle.zip /tmp/gradle.zip
RUN unzip -q /tmp/gradle.zip -d /opt && rm /tmp/gradle.zip \
 && ln -sf /opt/gradle-${GRADLE_VERSION}/bin/gradle /usr/local/bin/gradle

# The app's crates, vendored: cargo reads /.cargo/config.toml for any
# directory under /, so rust-core builds offline.
COPY vendor /opt/vendor
COPY cargo-config.toml /.cargo/config.toml

# Gradle's dependencies: build the app once here, with the network, and
# keep what Gradle fetched as a read-only cache (GRADLE_RO_DEP_CACHE) that
# a job's own, writable GRADLE_USER_HOME reads first. A failing test does
# not matter here; only the downloads do.
COPY app /tmp/app
RUN cd /tmp/app \
 && printf 'sdk.dir=%s\n' "$ANDROID_HOME" > local.properties \
 && (GRADLE_USER_HOME=/opt/gradle-home gradle --no-daemon -q --continue \
      :app:assembleDebug :app:createDebugUnitTestCoverageReport :app:lintDebug :app:validateDebugScreenshotTest || true) \
 && test -d /opt/gradle-home/caches/modules-2 \
 && cd / && rm -rf /tmp/app /opt/gradle-home/daemon /opt/gradle-home/native /opt/gradle-home/wrapper \
      /opt/gradle-home/caches/[0-9]* /opt/gradle-home/caches/transforms-* /opt/gradle-home/caches/build-cache-* \
      /opt/gradle-home/caches/journal-* /opt/gradle-home/caches/jars-* \
 && rm -f /opt/gradle-home/caches/modules-2/*.lock /opt/gradle-home/caches/modules-2/gc.properties \
 && chmod -R a+rX /opt/gradle-home "$ANDROID_HOME" /opt/vendor
