No java so (shared object) with docker build of custom modules

wwx
Tue, 28 Oct 2025 18:12:00 +0000
Android/Java

Attempting to add the stitcher module from contrib to opencv build for android within a docker container. Everything builds except the java#.so file. I’ve attempted to force it thru env variables (genai suggestions) to force it, but the approach feels like a hack. Including the docker file below. Any advice is appreciated from humans. Thanks in advance!

# Use a lean base image suitable for compilation
FROM ubuntu:24.04

# Define build arguments for version control
ARG OPENCV_VERSION="4.12.0"
ARG NDK_VERSION="r29"

# Define SHA256 checksums
ARG OPENCV_SHA256="FA3FAF7581F1FA943C9E670CF57DD6BA1C5B4178F363A188A2C8BFF1EB28B7E4"
ARG CONTRIB_SHA256="52BEF961072E464604F162010E71DF769AA42794C95CE064A183056C4D5868E3"
ARG NDK_SHA256="4ABBBCDC842F3D4879206E9695D52709603E52DD68D3C1FFF04B3B5E7A308ECF"

# Set non-interactive mode for installation
ENV DEBIAN_FRONTEND=noninteractive

# Install essential dependencies: build tools, CMake, and Java
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    wget \
    unzip \
    git \
    cmake \
    ninja-build \
    build-essential \
    python3 \
    python3-pip \
    openjdk-17-jdk \
    coreutils \
    && rm -rf /var/lib/apt/lists/*

# Set environment variables for the build
ENV OPENCV_HOME=/opencv
ENV NDK_HOME=/opt/android-ndk
ENV ANDROID_NATIVE_API_LEVEL=24
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ENV PATH=$JAVA_HOME/bin:$PATH

# Create directories
RUN mkdir -p $OPENCV_HOME/src $OPENCV_HOME/contrib $OPENCV_HOME/build

WORKDIR /tmp

# Download and extract OpenCV and Contrib sources
RUN wget -q https://github.com/opencv/opencv/archive/$OPENCV_VERSION.zip -O opencv.zip && \
    echo "$OPENCV_SHA256 opencv.zip" | sha256sum -c - && \
    unzip -q opencv.zip -d $OPENCV_HOME/src && \
    mv $OPENCV_HOME/src/opencv-$OPENCV_VERSION $OPENCV_HOME/src/opencv_main && \
    rm opencv.zip

RUN wget -q https://github.com/opencv/opencv_contrib/archive/$OPENCV_VERSION.zip -O contrib.zip && \
    echo "$CONTRIB_SHA256 contrib.zip" | sha256sum -c - && \
    unzip -q contrib.zip -d $OPENCV_HOME/contrib && \
    mv $OPENCV_HOME/contrib/opencv_contrib-$OPENCV_VERSION $OPENCV_HOME/contrib/opencv_contrib_main && \
    rm contrib.zip

# Download and extract Android NDK
RUN wget -q https://dl.google.com/android/repository/android-ndk-$NDK_VERSION-linux.zip -O ndk.zip && \
    echo "$NDK_SHA256 ndk.zip" | sha256sum -c - && \
    unzip -q ndk.zip -d /opt && \
    mv /opt/android-ndk-$NDK_VERSION $NDK_HOME && \
    rm ndk.zip

WORKDIR $OPENCV_HOME/build

# CMake command to configure a minimal Android build targeting arm64-v8a.
# BUILD_LIST explicitly includes 'stitching' and its essential dependencies.
RUN cmake \
    -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=$NDK_HOME/build/cmake/android.toolchain.cmake \
# --- PATH RELATED
    -DOPENCV_EXTRA_MODULES_PATH=$OPENCV_HOME/contrib/opencv_contrib_main/modules \
    -DBUILD_LIST="core,imgproc,imgcodecs,features2d,calib3d,flann,stitching,java" \
# ---ANDROID RELATED---
    -DANDROID_ABI="arm64-v8a" \
    -DANDROID_NDK=$NDK_HOME \
    -DANDROID_NATIVE_API_LEVEL=$ANDROID_NATIVE_API_LEVEL \
    -DANDROID_SUPPRESS_SDK_BUILD=ON \
    -DANDROID_SDK_HOME=OFF \
    -DANDROID_SDK_BUILD_TOOLS_VERSION=OFF \
    -DBUILD_JAVA_PROJECT=OFF \
    -DBUILD_ANDROID_EXAMPLES=OFF \
    -DINSTALL_ANDROID_EXAMPLES=OFF \
    -DBUILD_ANDROID_PROJECTS=OFF \
    -DBUILD_ANDROID_SERVICE=OFF \
# ---JAVA RELATED---
    -DBUILD_JAVA=ON \
    -DBUILD_FAT_JAVA_LIB=OFF \
    -DJAVA_HOME=$JAVA_HOME \
    # FORCE BUILD OF JAVA SO - HACK
    -DJava_FOUND=TRUE \
    -DJava_AWT_FOUND=TRUE \
    -DJava_INCLUDE_PATH=$JAVA_HOME/include \
    -DJava_AWT_INCLUDE_PATH=$JAVA_HOME/include \
    -DJava_JNI_INCLUDE_PATH=$JAVA_HOME/include \
    -DJava_JNI_INCLUDE_PATH2=$JAVA_HOME/include/linux \
    -DJava_JNI_INCLUDE_PATH_HOST=$JAVA_HOME/include/x64 \
    -DBUILD_opencv_java=ON \
# OTHER OPTIONS
    -DCPACK_GENERATOR=None \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_DATA_FILES=OFF \
    -DINSTALL_CREATE_DISTRIB=OFF \
    -DBUILD_SDK=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DWITH_TBB=OFF \
    -DWITH_CUDA=OFF \
    -DWITH_IPP=OFF \
    $OPENCV_HOME/src/opencv_main

# Compile and install the custom SDK
RUN ninja -j$(nproc)
RUN ninja install

# Set the entry point to keep the container running if needed, or just exit cleanly
CMD ["echo", "Custom OpenCV build complete. Output is in /opencv/build/install/sdk"]

The Errors are:

43.36 CMake Error: File /opencv/src/opencv_main/modules/java/android_sdk/build.gradle.in does not exist.
43.36 CMake Error at modules/java/android_sdk/CMakeLists.txt:179 (configure_file):
43.36   configure_file Problem configuring file
43.36
43.36
43.36 CMake Error: File /opencv/src/opencv_main/modules/java/android_sdk/build.gradle.in does not exist.
43.36 CMake Error at modules/java/android_sdk/CMakeLists.txt:206 (configure_file):
43.36   configure_file Problem configuring file
43.36
43.36
43.37 CMake Error at CMakeLists.txt:1099 (add_subdirectory):
43.37   add_subdirectory given source "doc" which is not an existing directory.
43.37
43.37
43.37 CMake Error at CMakeLists.txt:1102 (add_subdirectory):
43.37   add_subdirectory given source "data" which is not an existing directory.
43.37
43.37
43.37 CMake Error at CMakeLists.txt:1121 (include):
43.37   include could not find requested file:
43.37
43.37     cmake/OpenCVGenHeaders.cmake
43.37
43.37
43.37 CMake Error at CMakeLists.txt:1129 (include):
43.37   include could not find requested file:
43.37
43.37     cmake/OpenCVGenAndroidMK.cmake
43.37
43.37
43.37 CMake Error at CMakeLists.txt:1132 (include):
43.37   include could not find requested file:
43.37
43.37     cmake/OpenCVGenConfig.cmake
43.37
43.37
43.37 CMake Error at CMakeLists.txt:1140 (include):
43.37   include could not find requested file:
43.37
43.37     cmake/OpenCVGenABI.cmake
43.37

The cmake directory contents do exist. The android sdk does not. Only the NDK is present.

2 posts - 1 participant