Using Android NDK with OpenCV 3.4.1

asked 2018-07-10 11:10:58 -0500

omatrot gravatar image

I'm unable to make my native C++ code to link with OpenCV, both dynamically or statically. I have basically downloaded the Android SDK and extracted it in some place, then included the relevant path in the cmakelists.txt file.

Dynamically I have the following error :

bridge_impl.cpp:132: error: undefined reference to 'cv::imencode(cv::String const&, cv::_InputArray const&, std::__ndk1::vector<unsigned char,="" std::__ndk1::allocator<unsigned="" char=""> >&, std::__ndk1::vector<int, std::__ndk1::allocator<int=""> > const&)' bridge_impl.cpp:211: error: undefined reference to 'cv::imencode(cv::String const&, cv::_InputArray const&, std::__ndk1::vector<unsigned char,="" std::__ndk1::allocator<unsigned="" char=""> >&, std::__ndk1::vector<int, std::__ndk1::allocator<int=""> > const&)' clang++: error: linker command failed with exit code 1

My cmakelists.txt content is :

add_library(opencv_java3 SHARED IMPORTED)

set_target_properties(opencv_java3 PROPERTIES IMPORTED_LOCATION ${CMAKE_HOME_DIRECTORY}/src/opencv/libs/${ANDROID_ABI}/libopencv_java3.so)

target_link_libraries(native-lib opencv_java3)

for what it's worth, the same code, which is cross platform, compiles and link fine on iOS.

Thanks for your help.

edit retag flag offensive close merge delete

Comments

You can do this follow snippet code In your CMakeLists.txt:

set(pathToProject <Path to your project>)
set(OPENCV_HOME <path to OpenCV>/OpenCV-android-sdk/sdk)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(OpenCV_STATIC ON)
set(OpenCV_DIR ${OPENCV_HOME}/native/jni)

FILE(GLOB_RECURSE cppfiles src/main/cpp/*)

cmake_minimum_required(VERSION 3.4.1)

include_directories(${pathToProject}/abc/src/main/jni/include);# abs is project name

#add_library(lib_opencv PROPERTIES IMPORTED)
add_library(native-lib SHARED ${cppfiles})

#set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/abc/src/main/jni/${ANDROID_ABI}/libopencv_java3.so)

--> next bellow

monxarat gravatar imagemonxarat ( 2018-07-19 04:32:02 -0500 )edit

find_library( log-lib log ) find_package (OpenCV REQUIRED)

    #lib_opencv
    target_link_libraries(native-lib jnigraphics ${log-lib} ${OpenCV_LIBS})

On your build.gradle

externalNativeBuild {
        cmake {
            cppFlags "-std=c++11 -frtti -fexceptions"
            abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            arguments "-DpathToProject:STRING=" + project.rootProject.getProjectDir().absolutePath
        }
    }

.....
    sourceSets {
        main {
            jniLibs.srcDirs(['src/main/jni/'])
        }
    }
monxarat gravatar imagemonxarat ( 2018-07-19 04:33:15 -0500 )edit