1

I included native OpenCV 3.0 into Android Studio as explained here.

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv 
OPENCVROOT:= D:/OpenCV-android-sdk_3 
OPENCV_CAMERA_MODULES:=on 
OPENCV_INSTALL_MODULES:=on 
OPENCV_LIB_TYPE:=STATIC 
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk 
include $(OPENCV_MK_PATH)

LOCAL_C_INCLUDES += $(LOCAL_PATH)

LOCAL_SRC_FILES := main.cpp 
LOCAL_LDLIBS += -llog -ldl 
LOCAL_MODULE := hello

include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := all
APP_PLATFORM := android-19

For NDK compilation I created an external tool calling ndk-build.cmd with these parameters:

NDK_PROJECT_PATH=$ModuleFileDir$/build/intermediates/ndk NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs NDK_APPLICATION_MK=$ModuleFileDir$/src/main/jni/Application.mk APP_BUILD_SCRIPT=$ModuleFileDir$/src/main/jni/Android.mk V=1

Problem:

I still cannot include any opencv header like

#include <opencv2/core/core.hpp

it always stays red. I read many other threads, but I could not find a solution. What Am I missing?

I'm using Windows 7 64bit.

Thanks in advance!

EDIT:

I now included OpenCV as a module and implemented the experimental gradle like that:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 19
        buildToolsVersion = "23.0.2"

        defaultConfig {
            applicationId = "..."
            minSdkVersion = 16
            targetSdkVersion = 19
            versionCode = 1
            versionName = "1.0"

        }
    }

    android.ndk{
        moduleName = "openCVLibrary300"
        ldLibs += ['log']
        cppFlags += "-std=c++11"
        cppFlags += "-fexceptions"
        cppFlags  += "-I${file("D:\\OpenCV-android-sdk_3\\sdk\\native\\jni\\include")}".toString()
        cppFlags  += "-I${file("D:\\OpenCV-android-sdk_3\\sdk\\native\\jni\\include\\opencv")}".toString()



//        ldFlags += linkOpt
        ldLibs += ["android", "EGL", "GLESv2", "dl", "log", "z"]// , "ibopencv_core"
        stl = "gnustl_shared"//"gnustl_static"//"gnustl_shared"//"stlport_static"
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles += file('proguard-rules.pro')
        }
    }

    android.productFlavors {
        create("arm") {
            ndk.with {
                abiFilters += "armeabi"

                File curDir = file('./')
                curDir = file(curDir.absolutePath)
                String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi\\" //"-L" +

                ldLibs += libsDir + "libopencv_core.a"
                ldLibs += libsDir + "libopencv_core.a"
                ldLibs += libsDir + "libopencv_highgui.a"
                ldLibs += libsDir + "libopencv_imgproc.a"
                ldLibs += libsDir + "libopencv_ml.a"
                ldLibs += libsDir + "libopencv_ts.a"

            }
        }
        create("armv7") {
            ndk.with {
                abiFilters += "armeabi-v7a"

                File curDir = file('./')
                curDir = file(curDir.absolutePath)
                String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi-v7a\\" //"-L" +

                ldLibs += libsDir + "libopencv_core.a"
                ldLibs += libsDir + "libopencv_core.a"
                ldLibs += libsDir + "libopencv_highgui.a"
                ldLibs += libsDir + "libopencv_imgproc.a"
                ldLibs += libsDir + "libopencv_ml.a"
                ldLibs += libsDir + "libopencv_ts.a"

            }
        }
        create("x86") {
            ndk.with {
                abiFilters += "x86"
            }
        }
        create("mips") {
            ndk.with {
                abiFilters += "mips"
            }
        }
        create("fat") {

        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile project(':openCVLibrary300')
}

Now I keep getting this undetailled error:

Error:Cause: com.android.build.gradle.managed.AndroidConfig_Impl

As per google it could be some syntax problem, but I cannot resolve it.

AKNOT
  • 11
  • 3
  • Your setup is good for compiling your module; for true integration with Android Studio, see e.g. http://stackoverflow.com/questions/32171259/opencv-with-android-studio-1-3-using-new-gradle-undefined-reference – Alex Cohn Jan 08 '16 at 08:19
  • Thanks for the hint, I thought that was not necessary anymore because of what I already did. OK. But unfortunately the way explained in your link does not work for me. It already fails in including 'com.android.model.application', it cannot be found. Although Im not sure what is meant by build.gradle (module), the one of the opencv300 module which I now added? That does not really make sense to me, but I am a novice in Android Studio and gradle. Btw., I am using Version 1.5.1. Could you be more detailled? Unfortunately I cannot not comment in the thread you linked to. Thx! – AKNOT Jan 10 '16 at 12:05
  • The link I provided uses the [*experimental* gradle plugin](http://tools.android.com/tech-docs/new-build-system/gradle-experimental). – Alex Cohn Jan 10 '16 at 15:04
  • Ah, that helped...but I'm still getting a diffuse error (see edit), do you have an idea? – AKNOT Jan 10 '16 at 17:52
  • If your top-level **build.gradle** file uses `classpath "com.android.tools.build:gradle-experimental:0.4.0"` or higher, **+=** syntax [is not suported anymore](http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-0.2.x---0.4.0). You need **.add()** instead. – Alex Cohn Jan 11 '16 at 10:31
  • 1
    OK, I was using a lower version, but updatet to 0.4.0 and made the changes you sugested. The error persisted but vanished after restarting Android Studio -.- Thanks for your help! – AKNOT Jan 23 '16 at 10:37
  • When switching plugin versions, it's necessary but sometimes not enough to restart AS. It may be necessary to rebuild all. – Alex Cohn Jan 23 '16 at 10:41

0 Answers0