5

I'm having trouble using native OpenCv 3.0.0 with Android Studio 2.2, with new ndk support i.e usin CMAKE build script. Below is the error which i am getting.Am i missing any thing in my gradle or cmake file? Please let me know.

Error:FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:externalNativeBuildDebug'.

    Build command failed. Error while executing 'C:\Users\User\AppData\Local\Android\sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build E:\OpenCvAndroid\OPecvTry2CSupport\app.externalNativeBuild\cmake\debug\mips64 --target native-lib} [1/1] Linking CXX shared library ..\obj\mips64\libnative-lib.so FAILED: cmd.exe /C "cd . && C:\Users\User\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe -target mips64el-none-linux-android -gcc-toolchain C:/Users/User/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/User/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "C:/Users/User/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ." CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function ~Mat': E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:278: undefined reference tocv::fastFree(void*)' CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function cv::Mat::release()': E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: undefined reference tocv::Mat::deallocate()' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.

And my Cmake.txt

 cmake_minimum_required(VERSION 3.4.1)
 add_library( native-lib
         SHARED
         src/main/cpp/native-lib.cpp )
include_directories(E\:\\OpenCVSdk\\sdk\\native\\jni\\include )
find_library( log-lib log )
target_link_libraries(native-lib ${log-lib} )

And my Gradle Build file

 {    apply plugin: 'com.android.application'

 android {

compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
    applicationId "viki.opecvtry2csupport"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"



    externalNativeBuild {
        cmake {
            cppFlags ""

        }
    }
}
Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32
vicky
  • 340
  • 5
  • 13
  • include directory should stay with cmake syntax, I thought: include_directories(path/to/your/sdk/native/jni/include ) target_link_libraries(native-lib log) – Gerry Sep 26 '16 at 15:12
  • I'm having similar issue here with imported ffmpeg shared library, did you find solution? – Krzysztof Kansy Oct 09 '16 at 17:18
  • Still i have not found a solution – vicky Oct 12 '16 at 05:14
  • I was getting to now the new system, and I think you should add desired library in CMakeLists.txt with addLibrary and then provide it's location with set_target_properties. But most definately you should check out this: https://github.com/sengsational/HelloCv sample project. It's working for me out of the box. – Krzysztof Kansy Oct 12 '16 at 12:43
  • I could not implement using Cmake file,but i have managed to implement it using .mk file.chek out this https://github.com/VikasRao/NDK-OpenCVAndroid – vicky Oct 14 '16 at 10:27
  • @vicky Thanks for the sample. I've also decided to use the old Android makefile system. – Krzysztof Kansy Oct 17 '16 at 08:48
  • Hey guys, checkout my answer which includes OpenCV with CMake and Android Gradle Plugin 2.3.1 integration: http://stackoverflow.com/questions/43766092/reg-adding-opencv-to-native-c-code-through-cmake-on-android-studio/43886764#43886764 – ahasbini May 10 '17 at 14:45

2 Answers2

4

I fixed my problem by add "lib_opencv" lib to target_link_libraries

target_link_libraries(
                   native-lib lib_opencv #Just add the lib_opencv
                   ${log-lib} )

don't forget to Link Gradle to your native library

my full CMakeList.txt

cmake_minimum_required(VERSION 3.4.1)

add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp )

find_library( log-lib
              log )

target_link_libraries(native-lib lib_opencv
                       ${log-lib} )

set(CMAKE_VERBOSE_MAKEFILE on)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION /PROJECT_PATH/openCVLibrary320/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

include_directories(/OpenCV-android-sdk/sdk/native/jni/include)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
Sally
  • 950
  • 9
  • 15
3

Setting OpenCV_DIR has worked for me with OpenCV 3.1 and AS 2.2 and the latest Gradle plugin. This is the relevant bit of my CMakeLists.txt.

cmake_minimum_required(VERSION 3.6)

SET(OpenCV_DIR $ENV{HOME}/AndroidDevelopment/opencv-3.1.0/sdk/native/jni)

find_package(OpenCV REQUIRED)
message(STATUS "opencv found: ${OpenCV_LIBS}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}
                    ${OpenCV_DIR}/include/)
[...]
target_link_libraries(myTarget log ${OpenCV_LIBS} m z android )
user1906
  • 2,310
  • 2
  • 20
  • 37