Can OpenCV for Android leverage the standard C++ Support to get native build support on Android Studio 2.2 for Windows?
It seems you already have imported the opencv module, now, open your CMakeList.txt and add the follow lines:
set(CMAKE_VERBOSE_MAKEFILE on)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION
path-to-your-project/MyApplication/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
include_directories(path-to-opencv-directory/OpenCV-android-sdk/sdk/native/jni/include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
and edit the:
target_link_libraries( # Specifies the target library.
native-lib
lib_opencv
# Links the target library to the log library
# included in the NDK.
$\{log-lib} )
to include your lib_opencv that you have created. To finish, you add the follow line:
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
in your module app, like this:
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
and below of buildTypes you add:
sourceSets {
main {
jniLibs.srcDirs = ['path to your application /MyApplication/app/src/main/jniLibs/']
}
}
For more details, you can see this: https://github.com/googlesamples/android-ndk/tree/master/cmake/hello-libs
With OpenCV 3.2 configuration could be actually quite short:
set(OpenCV_STATIC ON)
set(OpenCV_DIR ${OPENCV_HOME}/sdk/native/jni)
find_package (OpenCV REQUIRED)
target_link_libraries(native-lib ${OpenCV_LIBS})
That is it, 4 lines and no need to copy anything into your project. Just be sure that OPENCV_HOME
points to the directory where OpenCV Android SDK is located.
One additional benefit of this approach - you can link with OpenCV statically, which would dramatically reduce the size of your app/library.
I use this approach in one of Github projects: https://github.com/Fotoapparat/FaceDetector