Ask Your Question
0

How to build with opencv and native_app_glue

asked 2013-02-26 18:15:28 -0500

xaffeine gravatar image

I have a couple of "fully native" c++ applications (based on the native_activity and android_app infrastructure) that I am trying to add OpenCV capability to. I can get them to compile and link, but when I try to launch them, I get errors in the logcat and an "Unfortunately, NativeActivity has stopped" box on the device (a Nexus 10). The errors are part of the loading/launching process -- I haven't even added any code of my own yet!

Using the OpenCV4Android 2.4.4 beta, I can get tutorials 1 and 2 and the face-detection sample to work. The problem is when I try to add OpenCV libraries to a project that uses native_app_glue. I add the following to my android.mk file, after the include $(CLEAR_VARS) line.

#OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=off
#OPENCV_LIB_TYPE:=STATIC # doesn't help, conflicts with png
include c:/ThirdParty/OpenCV/OpenCV-2.4.3.2-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_C_INCLUDES += OPENCV_LOCAL_C_INCLUDES

(The commented-out lines are things that I also tried at some point.) After listing my local_src_files, I have the following before the include $(BUILD_SHARED_LIBRARY) line.

LOCAL_LDLIBS    += -landroid -llog -lOpenSLES -lEGL -lGLESv2
LOCAL_LDLIBS += -ldl # have also tried without this

LOCAL_STATIC_LIBRARIES += android_native_app_glue
LOCAL_STATIC_LIBRARIES += png

I have tried many variations on this approach and have also tried it with OpenCV4Android 2.4.3. I get these errors just by trying to link with OpenCV, without even writing any code of my own!

Typical logcat output:

> 02-26 13:33:23.350:
> D/AndroidRuntime(22440): Shutting down
> VM 02-26 13:33:23.350:
> W/dalvikvm(22440): threadid=1: thread
> exiting with uncaught exception
> (group=0x40b58930) 02-26 13:33:23.350:
> E/AndroidRuntime(22440): FATAL
> EXCEPTION: main 02-26 13:33:23.350:
> E/AndroidRuntime(22440):
> java.lang.RuntimeException: Unable to
> start activity
> ComponentInfo{com.droidblaster/android.app.NativeActivity}:
> java.lang.IllegalArgumentException:
> Unable to load native library:
> /data/app-lib/com.droidblaster-2/libdroidblaster.so
> 02-26 13:33:23.350:
> E/AndroidRuntime(22440):  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
> 02-26 13:33:23.350:
> E/AndroidRuntime(22440):  at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

What am I missing? How can I get this to work?

edit retag flag offensive close merge delete

Comments

I have determined that this problem is not unique to the Nexus 10. I have the same issue with an Acer Iconia tab A700. It appears that my app is not correctly linking to the opencv shared library. The OpenCV Manager is installed and up to date on both tablets.

xaffeine gravatar imagexaffeine ( 2013-02-27 13:34:00 -0500 )edit

Still no answer, but I have a temporary workaround. Static linking with version 2.4.3.2 allows me to at least load my program and do a few native C++ OpenCV calls. So, there are up to 2 bugs. One is that 2.4.4 static linking causes a conflict with libpng. The other is that there is no documentation for how to dynamically link a NativeActivity-based app.

xaffeine gravatar imagexaffeine ( 2013-02-27 17:11:06 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-01 06:23:58 -0500

  1. OpenCV Manager works with Java apps only. I do not appropriate way of initialization with OpenCV Manager in your case. The root of the problem is link time. In Java case OpenCV must be load in any time before its using. In case of native libraries, linker must know path to OpenCV before loading of your native library.

  2. Android looks all library dependencies in /system/lib/ only by default. You need to load libopencv_java.so manually in C++ with dlopen, or in Java by implementation of your NativeActivity class. See mo details about Java way in Stackoverflow thread.

edit flag offensive delete link more

Comments

Thanks for that note. I do hope that using OpenCV as a static library will work. Is there any reason why it shouldn't?

xaffeine gravatar imagexaffeine ( 2013-03-01 12:42:44 -0500 )edit

xaffeine: What did you do in the end? Static linking or dlopen or Java? If you have a small working sample project that you don't mind sharing as an example that will be great! Thanks!

anopencvdummy gravatar imageanopencvdummy ( 2014-05-03 11:18:26 -0500 )edit

I did static linking. It was a long time ago, though, so things may have changed.

xaffeine gravatar imagexaffeine ( 2014-05-05 12:30:59 -0500 )edit

Thank you!

anopencvdummy gravatar imageanopencvdummy ( 2014-05-05 19:23:14 -0500 )edit

Question Tools

Stats

Asked: 2013-02-26 18:15:28 -0500

Seen: 3,178 times

Last updated: Mar 01 '13