Ask Your Question

android 3rd party library using opencv

asked 2012-10-22 12:58:26 -0500

cooperz gravatar image

hi there,

I'm encountering problems when trying to compile and use another library on android (let us name it CoreVision) that uses opencv and cmake.

The library is using a cmake file and I'm using the opencv 2.4.2 android.toolchain.cmake file provided, as well as the OpenCVConfig.cmake from the Opencv-2.4.2/sdk/native/jni folder.

The library cross-compiles successfully. I try to add it to (and modify) one of the opencv android examples that uses JNI (I named the library that contains the JNI code CoreVisionIntegration), but everything explodes when I try to load the CoreVision library. While I do not know the exact cause, since the stack trace is not showing any useful information, the library has some static variables that are initialized to OpenCV types. Upon inspection of with the arm objdump, I notice that it does not link against, as is the Everything seems to be linked statically.

Any ideas on why that might be the case? Thanks, Andrei.

edit retag flag offensive close merge delete


I've reduced this to a very simple sample library (just one class and one function call).

If I introduce static member that is of cv::Mat type, I can reproduce the crash.

cooperz gravatar imagecooperz ( 2012-10-22 13:23:10 -0500 )edit

Eventually I've eliminated all the static non trivial opencv types (i.e. cv::Mat, cv::Point) and I have been able to eliminate the crashes. I hope it helps somebody out there.

cooperz gravatar imagecooperz ( 2012-10-27 19:08:24 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2012-10-29 04:31:41 -0500

  1. You have two independent versions of OpenCV. The first is linked against libCoreVision statically. The second is libopencv_java that is linked against integration library dynamically. The problem appears when you try to put some OpenCV object from one library to another. The root of strange crashes may be different versions of STL or different compiler options. To solve the problem you need to use the same shared library in both cases.
  2. Java machine does not load dependent libs from libs directory in your package. You need to load all your libs in reverse order to satisfy all dependencies. Move all System.LoadLibrary() calls to onManagerConnected() callback to load all your libs after OpenCV initialization.
edit flag offensive delete link more


If libopencv_java is the preferred way of linking against opencv on Android, then this should really be reflected in the OpenCVConfig.cmake file from sdk/native/jni folder, to resemble more what is found in for NDK, no ?

cooperz gravatar imagecooperz ( 2012-10-30 11:39:39 -0500 )edit

I've modified the file OpenCVConfig.cmake, by replacing:

SET(OpenCV_LIB_COMPONENTS opencv_java)

and adding:

set(OpenCV_opencv_java_LIBNAME_OPT "") set(OpenCV_opencv_java_DEPS_OPT ) set(OpenCV_opencv_java_EXTRA_DEPS_OPT )

Now arm-linux-androideabi-objdump -p confirms that it links against

cooperz gravatar imagecooperz ( 2012-10-30 12:03:05 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-10-22 12:58:26 -0500

Seen: 450 times

Last updated: Oct 29 '12