liming0791 liming0791 - 5 months ago 84
Android Question

Android Studio with opencv for android ndk, opencv header files not found

I'm getting to Android Studio for Android OpenCV developing, but when I compile the project which was ok in eclipse, I got this error:

D:\software\AndroidStudioProjects\CameraMe\openCVSamplefacedetection\src\main\jni\DetectionBasedTracker_jni.cpp:2:33: fatal error: opencv2/core/core.hpp: No such file or directory

I guess the headers for opencv was not found, but I don't know what's wrong.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

include D:\eclipse\OpenCV_2.4.9_android_sdk\sdk\native\jni\

LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_LDLIBS += -llog -ldl

LOCAL_MODULE := detection_based_tracker


APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8


#include <DetectionBasedTracker_jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/detection_based_tracker.hpp>


as you're using Android Studio, your Makefiles are ignored by default and new ones are generated on-the-fly, without properly referencing OpenCV as it's not supported.

This is how NDK builds are currently working from Android Studio and it's deprecated while a better way to do it is in the work.

You can deactivate this built-in NDK support and get your Makefiles to be used instead, by doing this inside your build.gradle:


apply plugin: ''

android {

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs' //set .so files directory to libs
        jni.srcDirs = [] //disable automatic ndk-build call

    // call regular ndk-build(.cmd) script from app directory
    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine 'ndk-build', '-C', file('src/main').absolutePath

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild

btw, I see you set APP_ABI only to armeabi-v7a, but OpenCV also supports x86 and mips, so you can also easily extend your support to these platforms.