0

My problem is about to use openCV in my native code in Android with CmakeLists.txt file. I read the following question and i followed the instructions, changed my CmakeLists.txt file and other gradle settings. Can OpenCV for Android leverage the standard C++ Support to get native build support on Android Studio 2.2 for Windows? Now i am using opencv in java code. But in my native code, openCV still is not avaliable. I am trying to use

#include <opencv2/core.hpp>

line in native-lib.cpp but getting error

Error:(3, 10) fatal error: 'opencv2/core.hpp' file not found 

This is my CmakeLists.txt file:

cmake_minimum_required(VERSION 3.4.1)

set(pathToProject C:/Users/lenovo/AndroidStudioProjects/AdviserOpenCV2)
set(pathToOpenCV C:/Users/lenovo/Desktop/openCV/opencv-3.4.1-android-sdk)

#Two sets suggested by Bruno Alexandre Krinski 20160825
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

#Addition suggested by Bruno Alexandre Krinski 20160825
include_directories(${pathToOpenCV}/sdk/native/jni/include)

#Added by IDE on project create
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )

#Addition suggested by Bruno Alexandre Krinski 20160825
add_library( lib_opencv SHARED IMPORTED )

#Addition suggested by Bruno Alexandre Krinski 20160825
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

#Added by IDE on project create
find_library( log-lib log )

#Added by IDE on project create, Removed and replace with additional parameter suggested by Bruno Alexandre Krinski 20160825
target_link_libraries( native-lib lib_opencv  ${log-lib} )

This is my MainActivity and its working correctly. In the screen textview showing "openCVLoader.initDebug, WORKING"

package com.example.lenovo.adviseropencv2;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    import org.opencv.android.OpenCVLoader;

    public class MainActivity extends AppCompatActivity {

        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");
            System.loadLibrary("opencv_java3");
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Example of a call to a native method
            TextView tv = (TextView) findViewById(R.id.sample_text);
            tv.setText(stringFromJNI());

            if(!OpenCVLoader.initDebug()){
                tv.setText(tv.getText() + "\n openCVLoader.initDebug, not working");
            }   else {
                tv.setText(tv.getText() + "\n openCVLoader.initDebug, WORKING");

            }
        }

        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();
    }

android_gradle_build.json file

{
    "buildFiles" : 
    [
        "C:/Users/lenovo/AndroidStudioProjects/AdviserOpenCV2/app/CMakeLists.txt"
    ],
    "cleanCommands" : 
    [
        "C:\\Users\\lenovo\\AppData\\Local\\Android\\Sdk\\cmake\\3.6.4111459\\bin\\cmake.exe --build C:\\Users\\lenovo\\AndroidStudioProjects\\AdviserOpenCV2\\app\\.externalNativeBuild\\cmake\\debug\\x86 --target clean"
    ],
    "cppFileExtensions" : [ "cpp" ],
    "libraries" : 
    {
        "lib_opencv-Debug-x86" : 
        {
            "abi" : "x86",
            "artifactName" : "lib_opencv",
            "buildType" : "debug",
            "output" : "C:/Users/lenovo/AndroidStudioProjects/AdviserOpenCV2/app/src/main/jniLibs/x86/libopencv_java3.so",
            "toolchain" : "2534711399"
        },
        "native-lib-Debug-x86" : 
        {
            "abi" : "x86",
            "artifactName" : "native-lib",
            "buildCommand" : "C:\\Users\\lenovo\\AppData\\Local\\Android\\Sdk\\cmake\\3.6.4111459\\bin\\cmake.exe --build C:\\Users\\lenovo\\AndroidStudioProjects\\AdviserOpenCV2\\app\\.externalNativeBuild\\cmake\\debug\\x86 --target native-lib",
            "buildType" : "debug",
            "files" : 
            [

                {
                    "flags" : "  --target=i686-none-linux-android --gcc-toolchain=C:/Users/lenovo/AppData/Local/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/lenovo/AppData/Local/Android/sdk/ndk-bundle/sysroot  -Dnative_lib_EXPORTS -IC:\\Users\\lenovo\\Desktop\\openCV\\opencv-3.4.1-android-sdk\\sdk\\native\\jni\\include -isystem C:\\Users\\lenovo\\AppData\\Local\\Android\\sdk\\ndk-bundle\\sources\\cxx-stl\\gnu-libstdc++\\4.9\\include -isystem C:\\Users\\lenovo\\AppData\\Local\\Android\\sdk\\ndk-bundle\\sources\\cxx-stl\\gnu-libstdc++\\4.9\\libs\\x86\\include -isystem C:\\Users\\lenovo\\AppData\\Local\\Android\\sdk\\ndk-bundle\\sources\\cxx-stl\\gnu-libstdc++\\4.9\\include\\backward  -isystem C:/Users/lenovo/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include/i686-linux-android -D__ANDROID_API__=15 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security  -std=c++11 -frtti -fexceptions -std=gnu++11 -O0 -fno-limit-debug-info  -fPIC    -c ",
                    "src" : "C:/Users/lenovo/AndroidStudioProjects/AdviserOpenCV2/app/src/main/cpp/native-lib.cpp",
                    "workingDirectory" : "C:/Users/lenovo/AndroidStudioProjects/AdviserOpenCV2/app/.externalNativeBuild/cmake/debug/x86"
                }
            ],
            "output" : "C:/Users/lenovo/AndroidStudioProjects/AdviserOpenCV2/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so",
            "toolchain" : "2534711399"
        }
    },
    "toolchains" : 
    {
        "2534711399" : 
        {
            "cCompilerExecutable" : "C:/Users/lenovo/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe",
            "cppCompilerExecutable" : "C:/Users/lenovo/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe"
        }
    }
}

How can i use OpenCV in my cpp code in Android?

  • NDK tools become very unhappy when they encounter paths with spaces, like `Ara Proje`. Try to move OpenCV to path that has no spaces, hopefully your project will build smoothly. – Alex Cohn Mar 25 '18 at 14:59
  • I created new project with no space path for opencv and it still not working in native cpp code. But in java code i am using opencv correctly. – Yusuf Çelebi Mar 25 '18 at 17:45
  • Same "'opencv2/core.hpp' file not found"? – Alex Cohn Mar 25 '18 at 19:38
  • With `CMAKE_VERBOSE_MAKEFILE`, you can easily see what commands CMake is generating. Does it add `-I C:/Users/lenovo/Desktop/openCV/opencv-3.4.1-android-sdk/sdk/native/jni/include` to compile `native-lib.cpp` ? If it looks correct, check if the file `C:/Users/lenovo/Desktop/openCV/opencv-3.4.1-android-sdk/sdk/native/jni/include/opencv2/core/core.hpp` really exists. – Alex Cohn Mar 26 '18 at 16:51
  • Thank you so much for your interest. I edited the post and added android_gradle_build.json file, it seems correct i guess. And İ checked, core.hpp really exist. Same error 'opencv2/core.hpp'... – Yusuf Çelebi Mar 26 '18 at 19:52
  • I notice that you use system header brackets `<>` for **opencv2/core.hpp**, but its path is not listed as **-isystem**. You should try `#include "opencv2/core.hpp"` – Alex Cohn Mar 27 '18 at 09:04
  • Unfortunately, any opencv functions is not available in my native source code. I can not include opencv. If i add this line #include "opencv2/core.hpp", it seems red and gradle is not build it, it giving fatal error and i mentioned it. – Yusuf Çelebi Mar 27 '18 at 09:21
  • No, I don't know what is wrong with your setup now. I would try to debug it by creating my own .h file somewhere, adding it to CMake include_directories, and checking if Android clang compiler can find it there. – Alex Cohn Mar 27 '18 at 12:02

0 Answers0