Android Studio OpenCV

I am currently developing a project in Android Studio and I need to use some OpenCV libraries.

I know Android Studio does not currently support NDK development, but while doing my research, I found there are ways to work around the situation.

I followed this tutorial to add opencv libraries to my project:

How to use opencv in android studio using gradle build tool?

Everything worked fine, but when I tried to test an OpenCV example like "FaceDetection" by doing this:

Go to OpenCV Android SDK, select the sample project you would like to try from.

First, delete the res folder in your own project app / src / main, then put the res folder from samples inside your app / src / main folder.

First remove the java folder from the application / src / main, then copy the src file to the folder from the samples (note that src needs to be renamed to Java).

If you are creating an example with native C ++ files, you need to have the NDK installed. Download it from google developer portal and add it to your at the top level of your project, below is the sdk.dir line: ndk.dir=/path/to/your/android-ndk

Build and run example!

I got the following errors:

java.lang.UnsatisfiedLinkError: Couldn't load detection_based_tracker from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.ricardonascimento.opencvexamples-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.ricardonascimento.opencvexamples-2, /vendor/lib, /system/lib]]]: findLibrary returned null
            at java.lang.Runtime.loadLibrary(
            at java.lang.System.loadLibrary(


the problem is loading the library

// Load native library after(!) OpenCV initialization



What do you propose to solve this problem? I searched all over the net but couldn't find anything.


source to share

2 answers

I ran into a similar issue while installing and configuring OPENCV libraries in AndroidStudio. If this is not a problem, please ignore this answer (I know this was asked a year ago). Hopefully others may find this helpful for quick troubleshooting. The github simple sample uses the OPENCV libraries. Everything is already set up to make it work very quickly. My AndroidStudio 2.0 Preview 2 (released 5 days ago).




Can you show your app build.gradle? You should check the "sourceSets" settings, the default source for the native module is "jniLibs". So the jniLibs directory should contain the required module, eg. armeabi-v7a, mips, x86, etc.

Here is an example assembly: (Note: I configured "jniLibs" in "libs", just personal configuration ...)

~/AndroidStudioProjects/OpenCV3-FaceDetection/app/jni$ ndk-build
[armeabi-v7a] Compile++ thumb: detection_based_tracker <= DetectionBasedTracker_jni.cpp
[armeabi-v7a] Prebuilt       : <= /home/cobalt/Android/OpenCV-android-sdk/sdk/native/jni/../libs/armeabi-v7a/
[armeabi-v7a] SharedLibrary  :
/home/cobalt/Android/adt-bundle-linux-x86-20131030/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: warning: hidden symbol '__aeabi_atexit' in /home/cobalt/Android/adt-bundle-linux-x86-20131030/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/thumb/libgnustl_static.a(atexit_arm.o) is referenced by DSO /home/cobalt/AndroidStudioProjects/OpenCV3-FaceDetection/app/obj/local/armeabi-v7a/
[armeabi-v7a] Install        : => libs/armeabi-v7a/
[armeabi-v7a] Install        : => libs/armeabi-v7a/


You can also configure "jniLibs" in "libs" from the application directory as follows:


sourceSets {
    main {
        jni.srcDirs = []


This can be found in the app module, the complete build.gradle app looks like this (example):

apply plugin: ''

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "org.opencv.samples.opencv3_facedetection"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

    sourceSets {
        main {
            jni.srcDirs = []

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ''
    compile project(':libraries:opencv')


Here's a sample project importing the OpenCV 3.0 Face Detection Sample into Android Studio:

The procedure can be found in .

Also, if you want to try Android Studio Gradle Experimental Plugin for native cpp development with OpenCV, you might be interested in the following sample projects:



All Articles