简体   繁体   中英

How use OpenCV from native C++ on Android (using NDK)

I have an android project using OpenCV and I need use OpenCV from native C++ code. But I have problem, to config cmake to link libraries. I have no knowledge about cmake, nor with gradle.

I found some pieces of advice and tried to write CMakeLists.txt on my own, but I wasn't successful. Currently, my CMakeLists.txt looks like:

cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)

add_library(cpp_native_code SHARED src/main/cpp/jni_interface.cpp)
# find all other source files
file ( GLOB cpp_code_files src/main/cpp/*.cpp src/main/cpp/*.hpp )
# add then to project
target_sources(cpp_native_code PUBLIC ${cpp_code_files})

find_library(log-lib log)
target_link_libraries(cpp_native_code ${log-lib})

set(path_to_opencv C:/Programs/OpenCV_Android_SDK)
include_directories(${path_to_opencv}/sdk/native/jni/include)

add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION 
    ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
target_link_libraries( cpp_native_code lib_opencv )

When I try to run my project, I get this error:

Error:error: '../../../../src/main/jniLibs/arm64-v8a/libopencv_java3.so', 
needed by '../../../../build/intermediates/cmake/debug/obj/arm64-
v8a/libcpp_native_code.so', missing and no known rule to make it

I am using OpenCV 3.3.0. If needed, I provide screenshot of my project structure here . OpenCV works when used from android directly.

Please, could you someone advise me, whan am I doing wrong? Or if there is some information, I didn't write and which is important.

Thanks, for your advice.

Look at this project

Note, this is not my project, I didn't test it.

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