6

I have an android project, that has native code. In this native part I use OpenCV. Everything compiles and works OK when I use OpenCV 2.3.1, but when I tried to switch to version 2.4.1, I faced problem:

It compiles without any errors, but when I start my app, it can't load my library because of UnsatisfiedLinkError.

Cannot load library: link_image[1936]:    37 could not load needed library 'libopencv_java.so' for 'mylibrary.so' (load_library[1091]: Library 'libopencv_java.so' not found)

I see that Open CV tries to load libopencv_java.so, but I do not need it and I don't use OpenCV in java code. This library's size >5M.

How to compile without adding this lib to project?

Arseniy
  • 1,737
  • 1
  • 19
  • 35
  • This is actually update of [my question](http://stackoverflow.com/questions/10857301/unable-to-link-native-library-in-opencv-android-sample). Are you sure your `Android.mk` contains line `OPENCV_LIB_TYPE:=STATIC`? – ArtemStorozhuk Jun 05 '12 at 09:40
  • You marked you question as answered, so I thought it's OK to you to include libopencv_java.so in your project. But I want to exclude it at all, if possible. – Arseniy Jun 05 '12 at 09:48
  • Yes I know. I said update not duplicate :) So, what about my question? – ArtemStorozhuk Jun 05 '12 at 09:50
  • There isn't `OPENCV_LIB_TYPE:=STATIC` in my `Android.mk`, but it's OK when I use OpenCV 2.3.1 – Arseniy Jun 05 '12 at 09:50
  • Try to add it. This will probably fix it. See [this answer](https://groups.google.com/forum/?fromgroups#!topic/android-opencv/VUrB_z93rgs). – ArtemStorozhuk Jun 05 '12 at 09:52
  • Thank you, I'll try this. I can't compile it right now, because i get `undefined reference to cv::calcOpticalFlowPyrLK`. Looks like I'm doing something wrong.. – Arseniy Jun 05 '12 at 10:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12156/discussion-between-muzhig-and-astor) – Arseniy Jun 05 '12 at 10:42

2 Answers2

7

The libopencv_java.so in addition to JNI wrappers to C++ OpenCV interface contains all the OpenCV native code.
When you build your JNI library with OpenCV 2.4 for Android you can either link dynamically with libopencv_java.so (default option) and include it into your APK or link statically by adding this option explicitly:

include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
include <your path>/OpenCV.mk
OpenCV4Android
  • 266
  • 1
  • 2
  • 3
    This didn't work for me. For example, default opencv adnroid sample #3. See this [question](http://stackoverflow.com/questions/10857301/unable-to-link-native-library-in-opencv-android-sample). – ArtemStorozhuk Jun 05 '12 at 12:56
1

A variation of the following worked for me when I upgraded from 2.3 to 2.4 (follow instructions if you have a JNI part in your application. Details in link at bottom.):

1/ Replace the paths in Android.mk so that the OpenCV.mk link still works (in 2.4 it's in native/jni/ instead of share/OpenCV/).

2/ Then write this before you include OpenCV.mk:

OPENCV_INSTALL_MODULES:=on 
(this copies libopencv_java.so to your project's lib folder)

you can copy other libraries as well (e.g. OPENCV_CAMERA_MODULES as used in link at bottom)

3/ After that, in the static section of your Activity class you can load the library (load library before others that depend on it):

System.loadLibrary("opencv_java"); 
(this loads libopencv.so)

or:

if (!OpenCVLoader.initDebug()) {
    // Handle initialization error
})

(second seems cleaner but I haven't tried it yet)

Source (for parts) is steps 3 and 4 in link: Application Development with Static Initialization

studiou
  • 301
  • 3
  • 6