简体   繁体   中英

Gradle can't find OpenCV Libraries for debugging with NDK

I've been developing Android apps with C++ code by just executing ndk-build . I've always used OpenCV, so in my Android.mk there was:

include $(CLEAR_VARS)

OPENCV_LIB_TYPE:=SHARED
OPENCV_CAMERA_MODULES:=off
OPENCV_INSTALL_MODULES:=on

include /Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk

include $(OPENCV_PATH)

and it's been working good.

Now I want to debug the C++ code with Android Studio, and I had to do some modifications in the gradle.

The first thing, the gradle wasn't seeing the include files, but I read this question in StackOverflow which taught me how to include the files. Now when I try to build my app, I get the following errors:

Error:(60) undefined reference to 'cv::_InputArray::_InputArray(cv::Mat const&)'

Error:(60) undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'

Error:(60) undefined reference to 'cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'

Error:(62) undefined reference to 'cv::_InputArray::_InputArray(cv::Mat const&)'

Error:(62) undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'

Error:(62) undefined reference to 'cv::resize(cv::_InputArray const&, cv:: OutputArray const&, cv::Size , double, double, int)'

Error:(73) undefined reference to 'cv::Mat::t() const'

Error:(73) undefined reference to 'cv::_InputArray::_InputArray(cv::MatExpr const&)'

Error:(73) undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'

which makes me suspect that the libraries are not found. I try to add it in my cFlags as:

productFlavors {
    x86 {
        flavorDimension "abi"
        ndk {
            abiFilter "x86"
            stl "stlport_shared"
            cFlags "-std=c99 " +
                    "-fexceptions " +
                    "-I/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/include " +
                    "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/x86 " +
                    "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab"
        }
    }
    arm {
        flavorDimension "abi"
        ndk {
            abiFilter "armeabi-v7a"
            stl "stlport_shared"
            cFlags "-std=c99 " +
                    "-fexceptions " +
                    "-I/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/include " +
                    "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/armeabi-v7a " +
                    "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab"
        }
    }
}

I have tried this as well:

buildTypes {
    debug {
        debuggable true
        jniDebuggable = true
        ndk {
            abiFilter "x86"
            stl "stlport_shared"
            cFlags "-std=c99 " +
                    "-fexceptions " +
                    "-I/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/include " +
                    "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/x86 " +
                    "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/armeabi-v7a " +
                    "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab"
    }
    release {
        debuggable true
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

}

but the errors are still there. It's like it can't find the libraries.

How should I do it?

Thank you in advance.

You need ldLibs and ldFlags to specify link libraries. With exeprimental plugin, I use the following fragment:

def appAbi = "armeabi-v7a"
def OpenCV_sdk = "~/opencv/sdk"
…
model { android.ndk {
…
    ldFlags += "-L$OpenCV_sdk/native/libs/$appAbi".toString()
    ldFlags += "-L$OpenCV_sdk/native/3rdparty/libs/$appAbi".toString()
    ldLibs += ['opencv_imgproc', 'opencv_core', 'opencv_hal', 'tbb']
} }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM