Embarking on the journey of image processing for Android using OpenCV might seem daunting at first. But fear not! In this tutorial, we’ll lay down a user-friendly roadmap for setting up OpenCV 4.6.0 (and other versions) using Android Studio with the powerful assistance of the Native Development Kit (NDK).
Why Use OpenCV with Android NDK?
Imagine you want to build a fabulous bakery on your mobile device that can process images like a pro chef! The Android SDK provides the facilities to bake a cake, but it’s the NDK that allows you to bring in advanced techniques from a master chef (C++), elevating your app’s capabilities to a new level.
Pre-requisites
- OpenCV 4.6.0
- Android Studio 2021.2.1
- Android Build Tool 33.0
- Android NDK 25.0
- Kotlin 1.6.10
- Gradle 7.2.1
- Mac OS 12.4
How to Use This Repository
- Download and Install Android Studio.
- Install NDK and CMake.
- Clone this repository as an Android Studio project:
- In Android Studio, click on File – New – Project from Version Control – Git.
- Paste this repository’s GitHub URL, choose a project directory and click next.
- Install the OpenCV Android release:
- Download OpenCV 4.6.0 Android release or the latest available Android release on the OpenCV website.
- Unzip downloaded file and place the OpenCV-android-sdk directory at your desired path.
- Link your Android Studio project to the OpenCV Android SDK you just downloaded:
- Open gradle.properties file and edit the line with your own OpenCV Android SDK directory path:
opencvsdk=Users/Example/Downloads/OpenCV-android-sdk - Sync Gradle and run the application on your Android Device!
Bootstrap a New Android Project with Native OpenCV Support
- Follow the steps in the previous section to download and install Android Studio and NDK.
- Create a new *Native Android Studio project*:
- Select File – New – New Project… from the main menu.
- Click the Phone and Tablet tab, select Native C++ and click next.
- Choose an Application Name, select your favorite language (Kotlin or Java), choose Minimum API level (28 here) and select next.
- Choose Toolchain default as C++ standard and click Finish.
- Install the OpenCV Android release as previously described.
- Add OpenCV Android SDK as a module into your project:
- Open settings.gradle file and append these lines:
include ':opencv' project(':opencv').projectDir = new File(opencvsdk + '/sdk') - Open gradle.properties file and append:
- Open build.gradle file and add the implementation to dependencies:
- Add the following config to the app/build.gradle file:
- In android – defaultConfig – externalNativeBuild – cmake section, add:
cppFlags "-frtti -fexceptions" abiFilters "x86", "x86_64", "armeabi-v7a", "arm64-v8a" arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native" - Add the following config to CMakeLists.txt:
- Before the add_library instruction, add:
include_directories($OpenCV_DIR/jni/include) add_library(lib_opencv SHARED IMPORTED) set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION $OpenCV_DIR/libs/$ANDROID_ABI/libopencv_java4.so) - In the target_link_libraries instruction, add:
- Add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera"/> <uses-feature android:name="android.hardware.camera.autofocus"/> <uses-feature android:name="android.hardware.camera.front"/> <uses-feature android:name="android.hardware.camera.front.autofocus"/> - Create your MainActivity and activity_main.xml using copies of the respective files from the repository, adapting the package names where necessary.
- Add native code in native-lib.cpp and remember to adapt the method name as advised.
- Sync Gradle and run the application on your Android Device!
opencvsdk=Users/Example/Downloads/OpenCV-android-sdk
dependencies {
...
implementation project(path: ':opencv')
}
lib_opencv
Troubleshooting Ideas
If you encounter issues during setup, here are some tips to consider:
- Ensure that the NDK and CMake paths in Android Studio are correct and matched with the project settings.
- Verify that the OpenCV SDK is correctly downloaded and the path configured in gradle.properties is accurate.
- Make sure all required permissions are granted in the AndroidManifest.xml file.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Final Thoughts
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Conclusion
Now you’re equipped with the knowledge to integrate Native OpenCV with Android using NDK! Don’t hesitate to showcase your baking skills (image processing), and get creative with your new app!

