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>
......

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@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.
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?
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: stackoverflow.com/questions/38416566/… If anybody who has gotten this to work could give it a quick look that would be greatly appreciated
it was almost done with gradle-experimental, and now there is a new, new way in the works with cmake: tools.android.com/tech-docs/new-build-system/…
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.