Logo
Android Native Development Guide

Cross-Compiling OpenCV for Android

A comprehensive guide to building OpenCV 4.x from source using the Android NDK and CMake, configuring ABI architectures, and integrating it flawlessly into a modern Kotlin & C++ Android project.

Step 1: Source & Toolchains

Prepare OpenCV source and the Android Native Development Kit

OpenCV 4.x Source

The core computer vision source code. Do not use the pre-built Android SDK if you need custom optimizations or specific flags.

Extraction Command:
unzip opencv-4.x.zip && cd opencv-4.x

Android NDK & Ninja

Provides the C/C++ compiler for Android (Clang) and the android.toolchain.cmake script. Ninja is recommended for speed.

Installation Tip:
Install directly via Android Studio SDK Manager -> SDK Tools -> NDK & CMake.

Step 2: Execute Cross-Compilation

Point CMake to the Android toolchain and specify the target ABI.

build_android.sh
#!/bin/bash
# Change this to your actual NDK path
export NDK_DIR="/Users/username/Library/Android/sdk/ndk/25.1.8937393"

cd opencv-4.x
mkdir build_android && cd build_android

# Configure CMake with NDK Toolchain
cmake -G "Ninja" \
    -DCMAKE_TOOLCHAIN_FILE=$NDK_DIR/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI="arm64-v8a" \ # Target 64-bit ARM
    -DANDROID_PLATFORM=android-24 \ # Minimum API Level
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_ANDROID_PROJECTS=OFF \ # Skip building sample apks
    -DBUILD_ANDROID_EXAMPLES=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DCMAKE_INSTALL_PREFIX=../opencv_android_sdk \
    ..

# Build and Install
ninja -j$(nproc)
ninja install
After successful compilation, you will find the headers and .a/.so libraries in the `opencv_android_sdk` folder.

Step 3: CMakeLists.txt Integration

Link the newly built OpenCV SDK into your app's native code.

app/src/main/cpp/CMakeLists.txt
# 1. Set the path to your compiled OpenCV SDK
set(OpenCV_DIR "\${CMAKE_CURRENT_SOURCE_DIR}/opencv_android_sdk/sdk/native/jni")
find_package(OpenCV REQUIRED)

# 2. Include your own JNI cpp files
add_library(native-lib SHARED native-lib.cpp)

# 3. Link OpenCV libraries and standard Android logging
target_link_libraries(
native-lib
\${OpenCV_LIBS}
log
)

Step 4: JNI & Kotlin Real-World Usage

Process an Android Bitmap in C++ using OpenCV and return the result.

1. Native C++ (JNI)

Convert an Android Bitmap into cv::Mat, process it (e.g., Grayscale or Blur), and map it back.

#include <jni.h>
#include <android/bitmap.h>
#include <opencv2/opencv.hpp>

extern "C" JNIEXPORT void JNICALL
Java_com_example_app_MainActivity_processImage(
JNIEnv* env, jobject /* this */, jobject bitmap) 

// 1. Lock Android Bitmap pixels
void* pixels;
AndroidBitmapInfo info;
AndroidBitmap_getInfo(env, bitmap, &info);
AndroidBitmap_lockPixels(env, bitmap, &pixels);

// 2. Wrap pixels in cv::Mat (Zero-copy)
cv::Mat mat(info.height, info.width, CV_8UC4, pixels);

// 3. OpenCV processing: Convert to Grayscale
cv::Mat gray;
cv::cvtColor(mat, gray, cv::COLOR_RGBA2GRAY);
cv::cvtColor(gray, mat, cv::COLOR_GRAY2RGBA);

// 4. Unlock and apply
AndroidBitmap_unlockPixels(env, bitmap);
}
2. Kotlin Layer

Load the native library and call the external function passing a mutable Bitmap.

class MainActivity : ComponentActivity() {

  // Declare the native method
  private external fun processImage(bitmap: Bitmap)

  override fun onCreate(savedInstanceState: Bundle?) {
    // Example usage in your UI
val myBitmap = BitmapFactory.decodeResource(...) 
    // Must be hardware mutable for in-place edit
val mutableBitmap = myBitmap.copy(...)

    // Let OpenCV process it!
processImage(mutableBitmap)
    // Now mutableBitmap is Grayscale
  }

  companion object {
    init {
      // Load the library built by CMake
System.loadLibrary("native-lib")
    }
  }
}

Explore More#mobile-dev

Integrating FFmpeg 7+ with Kotlin via JNI

Article

A complete, step-by-step visual guide to integrating pre-compiled FFmpeg 7 dynamic libraries (.so) into a modern Android project using CMake, JNI, and Jetpack Compose.

Android FFmpeg + C++: Video Cropping and Filter Processing

Article

A detailed demonstration of how to use FFmpeg and C++ in an Android project to implement video cropping, filter processing, and progress callbacks.

Integrating OpenCV 4.x in Android Studio

Article

A detailed step-by-step guide on how to integrate OpenCV 4.x into an Android Studio project, configure CMake and JNI, and implement image processing features.

Using OpenCV for Image Sharpening in Android

Article

A detailed step-by-step guide on how to integrate OpenCV into an Android project and implement image sharpening functionality.

Android OpenCV + FFmpeg 7: Making Videos Clearer from Blurry

Article

A detailed demonstration of how to use OpenCV and FFmpeg 7 in an Android project for video processing, achieving clarity from blurriness.

FFmpeg Android + OpenSSL Compilation Guide

Article

A detailed step-by-step guide on how to compile FFmpeg from source and integrate OpenSSL on the Android platform for HTTPS streaming support.

FFmpeg Xcode + Swift Compilation Guide

Article

A detailed step-by-step guide on how to compile FFmpeg from source using Xcode and Swift on macOS, and integrate it into an iOS project.

Android Image Similarity Detection and Grouping

Article

A detailed demonstration of how to use OpenCV in an Android project to implement image similarity detection and grouping features.