Can OpenCV for Android leverage the standard C++ Support to get native build support on Android Studio 2.2 for Windows?

。_饼干妹妹 提交于 2019-11-27 07:06:31
Bruno Alexandre Krinski

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

Do same as Bruno Alexandre Krinski's Answer but

in place of this line

abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'

put this line, (I don't know why this worked for me)

abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'

(Follow all the instructions of above answer except this)

ABIs [armeabi] are not supported for Android platform

Supported ABIs are [arm64-v8a, armeabi-v7a, x86, x86_64].

externalNativeBuild {
            cmake {
              //  cppFlags ""
                cppFlags "-std=c++11 -frtti -fexceptions"
               // abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
                abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!