4

I'm developing (with Android Studio) an Android app that uses OpenCV 3.1.0. I'm using async OpenCV initialization. My questions is: do I need to copy OpenCV native libs (OpenCV-android-sdk/sdk/native/libs) to my jniLibs directory (app/src/main/jniLibs)?

In all the tutorials that I've read about how to setup OpenCV in Android Studio they copy the libraries. But, as I'm using OpenCV manager to access OpenCV libraries externally installed in the device (not static initialization), I think I don't need to copy the libraries. Am I right?

Thanks for your help.

David Miguel
  • 12,154
  • 3
  • 66
  • 68

2 Answers2

4

After some time working with OpenCV for Android, I can give an answer to my question:

Async initialization

If you are using async initialization with OpenCV Manager you don't need to copy the native OpenCV libs to your project. Because you are using OpenCV Manager for that:

OpenCV Manager is an Android service targeted to manage OpenCV library binaries on end users devices. It allows sharing the OpenCV dynamic libraries between applications on the same device.

More info: OpenCV Manager docs.

Static initialization

According to this approach all OpenCV binaries are included into your application package (this approach is deprecated for the production code).

App without JNI part:
You have to copy the contents of sdk/native/libs into your project directory to folder app/src/main/jniLibs.

App with JNI part:
You need to modify your Android.mk file. After that the OpenCV libraries will be copied to your application jniLibs folder during the JNI build.

More info: Static Initialization docs.

Community
  • 1
  • 1
David Miguel
  • 12,154
  • 3
  • 66
  • 68
  • Would suggest having a look at using `CMake` native build tool & `CMakeLists.txt` build script rather than the older `ndk-build` build tool and `Android.mk` as of Android Studio version `2.2.*` `ndk-build` is deprecated. See official documentation for further information: http://tools.android.com/tech-docs/new-build-system/gradle-experimental/migrate-to-stable – Kevin Crain Nov 02 '16 at 19:25
  • @Kevin I'll take a look and update the answer. The app that I'm working on at the moment doesn't have a JNI part, so I'm not very familiar with it. Thank you very much for your suggestion. – David Miguel Nov 02 '16 at 20:02
  • Are you using correct terminology here? Yours which is "Async initialization" seems to be inappropriate because you are comparing it with "static initialization". Probably you want to say "Dynamic initialization", right? – Second Person Shooter Mar 09 '18 at 14:12
  • I'm using the terminology used in the [official docs](https://docs.opencv.org/3.4.1/d5/df8/tutorial_dev_with_OCV_on_Android.html). You use OpenCV Manager (external app) to access OpenCV libraries externally installed in the target system. When you call `OpenCVLoader.initAsync()` you define `OnManagerConnected` callback that is called when initialisation finishes. – David Miguel Mar 09 '18 at 14:59
  • https://docs.opencv.org/3.0-beta/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization has setup instruction sections for Application Development with Async Initialization , Application Development with Static Initialization (both with and without JNI in your code) , and Native/C++ – WillC Apr 24 '18 at 13:12
  • Yes WillC, it's the link in the "More info". – David Miguel Apr 25 '18 at 08:15
0

Yes you will be needing to copy the contents of sdk/native/libs for OpenCV into the apps jniLibs directory if you usinf NDK, the java based classes are just a `wrapper to the OpenCV native libraries to make it easier for you to work with OpenCV.

See further official documentation at link provided below: http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/O4A_SDK.html

Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
  • After one month working with OpenCV in Android, I have corroborated my hypothesis: **No**, you don't need to include the native libs if you are using async initialization, because you are using [OpenCV Manager](http://docs.opencv.org/3.0-beta/platforms/android/service/doc/index.html) for that. But thank you for your answer. – David Miguel Nov 02 '16 at 13:59
  • @DavidMiguel Thanks David! thats really enlightening. Would I would need to use c++ late-binding? Did not know the exact purpose of the OpenCV Manager. Side note: I was thinking today if there was a way to do away with a user downloading the OpenCV manager app for OpenCV to work within your app, as this might put the user off from downloading your app. I am day 2 in NDK development and there is tons to learn that I am really excited about. – Kevin Crain Nov 02 '16 at 14:24
  • To anyone who may stumble across this comment thats new to NDK development and OpenCV. I hope @DavidMiguel comment and the one below helps you out. A lot of the learning I personally experienced is to differentiate between the deprecated toolchains, compilers used for NDK development, and a lot of the information you will find on the internet is out dated including samples. This can be good or bad depending on the project/IDE, toolchains etc. that you are working with. I often stray away from the official documentation but for NDK development currently its a best source to start off with. – Kevin Crain Nov 02 '16 at 14:28
  • In my opinion using OpenCV Manager might slightly decrease the user experience, but it gives you some relevant benefits (your app is around 40MB smaller, OpenCV Manager installs optimized binaries for the user hardware, OpenCV Manager is supposed to keep OpenCV libs updated and if the user has other apps that use OpenCV the OpenCV libs are shared). About the official documentation is really a pain in the neck, I've spent hours and hours trying to find updated articles... Thanks for your advices. – David Miguel Nov 02 '16 at 19:57
  • @DavidMiguel Anytime David and well noted, there is definitely benefits to having the OpenCV Manager app installed on your device as you have pointed out, thanks. I am also experiencing the same issue of lack of newer Android samples/tutorials using OpenCV official documentation. and spend hours looking at all kinds of other documentation/samples to either find its deprecated or not what I really need now. All the best with your OpenCV journey – Kevin Crain Nov 02 '16 at 20:43