7

What are the steps necessary for me to integrate,

the latest version of OpenCV

into a pure C++(No Java Code) Android NDK project, such as Android NDK Google tutorial:

Endless-Tunnel ?

official documentation for android integration refers to much older version :OpenCV-2.4 and folder structures are no longer the same.

I am using Android Studio on Linux.

All help is highly Appreciated.

Sadern Alwis
  • 104
  • 1
  • 4
  • 17

2 Answers2

9
  1. Download opencv Android package (e.g. opencv-4.0.1-android-sdk) and unpack to, say, ~/android.

  2. To the bottom of CMakeLists.txt, add

    set( OpenCV_DIR "~/android/OpenCV-android-sdk/sdk/native/jni" )
    find_package( OpenCV REQUIRED )
    target_link_libraries(game opencv_java)
    

The package will define the following variables:

OpenCV_LIBS : The list of all imported targets for OpenCV modules.

OpenCV_INCLUDE_DIRS : The list of OpenCV include directories. With CMake >= 2.8.11 you don't even need to write

include_directories(${OpenCV_INCLUDE_DIRS})

This version of prebuilt OpenCV SDK defines also

OpenCV_VERSION : The version of this OpenCV build: "4.0.1"

OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API: "16".

This means that your app manifest needs minSdkVersion 16 or higher (the original sample needs a fix here).

Instead of the shared library that contains all OpenCV functionality, you can use static libraries (opencv_imgcodecs, opencv_stitching, et al). These static libraries assume the default ANDROID_STL=c++_static.

For best results, use NDK r.18 or r.19.

UPDATE: NDK r.21 works well for opencv 4.3.0.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • With the recent improvements in both NDK and OpenCV, the older tricky ways (cf. *[Adding OpenCV to Native C code through CMake on Android Studio](https://stackoverflow.com/a/43886764/192373)*) are no longer necessary. – Alex Cohn Mar 04 '19 at 09:46
  • 1
    Looks like the perfect answer. but i need like 3 days to confirm. thank you so much. – Sadern Alwis Mar 05 '19 at 15:45
  • @AlexCohen now it compiles with a sample opencv code but when running i get -> java.lang.UnsatisfiedLinkError: Unable to load native library "/data/app/com.google.sample.tunnel-4TMDUdEDiQKyO7Y9pTu-cg==/lib/arm64/libgame.so": dlopen failed: library "libopencv_java4.so" not found – Sadern Alwis Mar 09 '19 at 05:45
  • i have not used Cmake at this extent before. so i think i may have not included add_library linking to libopencv_java4.so, maybe? – Sadern Alwis Mar 09 '19 at 05:56
  • If you are more comfortable with **Android.mk**, the OpenCV Android SDK includes **OpenCV.mk**. There, you need an extra `OPENCV_INSTALL_MODULES:=on` to have the shared library included in the APK for you. – Alex Cohn Mar 09 '19 at 07:02
  • by prefernece i would like to try to make it with Cmake, because many libraries that my app will depend on uses makefiles. plus i understand that i need to learn more about cmake. – Sadern Alwis Mar 09 '19 at 07:13
  • 2
    This looks to me as a glitch in Android Gradle plugin. I must experiment a bit more. The easy workaround is to add `/sdk/native/libs` to **jniLIbs.src** – Alex Cohn Mar 09 '19 at 07:17
  • 1
    Manually created the jniLibs folder and copied the ~/OpenCV-android-sdk/sdk/native/libs content to that folder. [link]https://www.youtube.com/watch?v=Lg24S-Y8NU8 Thank you @AlexCohn. if you find a solution after you experiment , please let me know. – Sadern Alwis Mar 09 '19 at 08:36
  • After some experiments: I believe that the easiest solution is https://stackoverflow.com/a/39205396/192373. – Alex Cohn Mar 09 '19 at 17:15
  • 1
    Thank you @AlexCohn – Sadern Alwis Mar 10 '19 at 14:36
  • 1
    The custom copy command is no longer necessary for imported targets when using the Android Gradle Plugin 4.0, see here: https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs – spectralio Jul 07 '20 at 07:50
  • I was always having a hard time building the whole project with `ccmake`. I should have seen this earlier thanks! – 김선달 Sep 13 '21 at 09:52
2

If you would like to create shared objects(.so) and compile without using Android studio, here is my blog. This is often useful when you would like to create a native third-party library.

Milind Deore
  • 2,887
  • 5
  • 25
  • 40