3

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.

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=off
#OPENCV_LIB_TYPE:=SHARED
include D:\eclipse\OpenCV_2.4.9_android_sdk\sdk\native\jni\OpenCV.mk

LOCAL_SRC_FILES  := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS     += -llog -ldl

LOCAL_MODULE     := detection_based_tracker

include $(BUILD_SHARED_LIBRARY)

Application.mk

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

DetectionBasedTracker_jni.cpp

#include <DetectionBasedTracker_jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/detection_based_tracker.hpp>
......
liming0791
  • 43
  • 1
  • 5

1 Answers1

10

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:

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

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.

ph0b
  • 14,353
  • 4
  • 43
  • 41
  • @ph0b can you help me in this. I am trying to implement face detection and facing many errors in jni. I do not know how to fix those. – LuminiousAndroid Jun 03 '16 at 08:42
  • Thanks for the suggestion, however when I do this Android Studio jniLibs and jni are underlined, with the error "Cannot resolve symbol jniLibs". I wish I could post my entire app build.gradle file, but there is not room in these response comment boxes. Should I post more details as an "Answer"? Any suggestions? – cdahms Jul 18 '16 at 23:38
  • I just posted more detail and some accompanying screenshots on a different post I have going where I attempted this fix but it did not resolve the concern, the post is here: http://stackoverflow.com/questions/38416566/android-studio-opencv-c-jni-ndk-unable-to-configure If anybody who has gotten this to work could give it a quick look that would be greatly appreciated – cdahms Jul 18 '16 at 23:51
  • it was almost done with gradle-experimental, and now there is a new, new way in the works with cmake: http://tools.android.com/tech-docs/new-build-system/gradle-experimental/migrate-to-stable – ph0b Aug 16 '16 at 14:29
  • Did this but i get "could not find property 'Os' on task", it seems that gradle doesn't recognize "Os" for some reason... any suggestion? – Hugo Apr 25 '17 at 08:43
  • @ph0b :I have a similar problem, but I have built my project differently using native c++ support while starting the project, in that case, can you suggest me how to add the "#include " file in my cpp file – Pratik Vyas Apr 13 '18 at 11:36