Unable to link native library in OpenCV Android sample

4,904 views
Skip to first unread message

Artem Storozhuk

unread,
Jun 2, 2012, 6:26:55 AM6/2/12
to android...@googlegroups.com
Here's my question on stackoverflow:
I cannot solve this problem for a long time.
Question: Did I miss something or that native samples don't work at all??? Have anyone tested them? Because I tested them on 3 different systems (linux 32/64 and Windows 64) and on many Android OS (2.3.7, 3.0, 4.0.3) and it didn't work (got same exception).
Thanks.

Artem Storozhuk

unread,
Jun 2, 2012, 6:08:31 PM6/2/12
to android...@googlegroups.com
Ok.

I'm trying to include OpenCV to hello-jni sample. Hello-jni (original) builds and runs OK. To add OpenCV to this project firstly I add line to Android.mk:
include /opt/OpenCV-2.4.0/share/OpenCV/OpenCV.mk

after:
include $(CLEAR_VARS)

So this is modified makefile:
LOCAL_PATH := $(call my-dir)
include $
(CLEAR_VARS)
include
/opt/OpenCV-2.4.0/share/OpenCV/OpenCV.mk #this is path to opencv.mk
LOCAL_MODULE    
:= hello-jni
LOCAL_SRC_FILES
:= hello-jni.c
include $
(BUILD_SHARED_LIBRARY)

And also added Application.mk file to jni folder with such content:
APP_STL := gnustl_static
APP_CPPFLAGS
:= -frtti -fexceptions
APP_ABI
:= armeabi-v7a

All this info is taken from paragraph Practice: Create an Android application, which uses OpenCV. So, I did everything correctly, right?

All other files I didn't modified at all. From ndk-build project builds correctly:
Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi-v7a/gdbserver
Gdbsetup       : libs/armeabi-v7a/gdb.setup
Compile thumb  : hello-jni <= hello-jni.c
SharedLibrary  : libhello-jni.so
Install        : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so
Prebuilt       : libnative_camera_r2.2.0.so <= /opt/OpenCV-2.4.0/share/OpenCV/../../libs/armeabi-v7a/
Install        : libnative_camera_r2.2.0.so => libs/armeabi-v7a/libnative_camera_r2.2.0.so
Prebuilt       : libnative_camera_r2.3.3.so <= /opt/OpenCV-2.4.0/share/OpenCV/../../libs/armeabi-v7a/
Install        : libnative_camera_r2.3.3.so => libs/armeabi-v7a/libnative_camera_r2.3.3.so
Prebuilt       : libnative_camera_r3.0.1.so <= /opt/OpenCV-2.4.0/share/OpenCV/../../libs/armeabi-v7a/
Install        : libnative_camera_r3.0.1.so => libs/armeabi-v7a/libnative_camera_r3.0.1.so
Prebuilt       : libnative_camera_r4.0.0.so <= /opt/OpenCV-2.4.0/share/OpenCV/../../libs/armeabi-v7a/
Install        : libnative_camera_r4.0.0.so => libs/armeabi-v7a/libnative_camera_r4.0.0.so
Prebuilt       : libnative_camera_r4.0.3.so <= /opt/OpenCV-2.4.0/share/OpenCV/../../libs/armeabi-v7a/
Install        : libnative_camera_r4.0.3.so => libs/armeabi-v7a/libnative_camera_r4.0.3.so

But when I run it from Eclipse I once again got this exception:
Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/hellojni/HelloJni;

So, what did I miss? I wasted so much time to fix this exception... 
Can someone upload simple working android native opencv (2.4.0) project?
Thanks!!!

Субота, 2 червня 2012 р. 13:26:55 UTC+3 користувач Artem Storozhuk написав:

Andrey Pavlenko

unread,
Jun 4, 2012, 7:34:05 AM6/4/12
to android...@googlegroups.com
Try to add the following code line to your Android.mk:
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
include
<your path>/OpenCV.mk




Andrey Pavlenko

unread,
Jun 5, 2012, 8:01:05 AM6/5/12
to android...@googlegroups.com
Have you referenced STL in your Application.mk?

Arseniy Potapov

unread,
Jun 6, 2012, 2:37:02 AM6/6/12
to android...@googlegroups.com
Contents of my Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS
:= -frtti -
fexceptions
APP_ABI
:= armeabi-v7a

вторник, 5 июня 2012 г., 18:01:05 UTC+6 пользователь Andrey Pavlenko написал:

Andrey Pavlenko

unread,
Jun 6, 2012, 4:39:32 AM6/6/12
to android...@googlegroups.com
What NDK version do you use?
I've just tried to run ndk-buil.cmd for 'tutorial-3-native' (static link) and 'tutorial-4-mixed' (dynamic) samples from 2.4.1 package and it built both JNI libs OK (just after fixing typo in includeOpenCV.mk 2.4.0 -> 2.4.1)

Andrey Pavlenko

unread,
Jun 9, 2012, 7:30:08 AM6/9/12
to android...@googlegroups.com
Can't reproduce.
Please specify OpenCV and NDK versions.

Zohob

unread,
Jun 13, 2012, 11:58:06 AM6/13/12
to android...@googlegroups.com
Same Problem here, after switching from OpenCV-2.3.1 to OpenCV-2.4.1. Both times I used NDK 7b
Before adding
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES
:=on
I get a runtimeerror with.
2596 could not load needed library 'libopencv_java.so' for 'libNativeProcessing.so'

if I add those two lines, the build console aborts with error:
./obj/local/armeabi-v7a/libopencv_core.a(persistence.cpp.o): In function `cv::toUtf16(std::string const&)':

persistence.cpp:(.text._ZN2cv7toUtf16ERKSs+0x6a): undefined reference to `
std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::basic_string(wchar_t const*, std::allocator<wchar_t> const&)'

persistence.cpp:(.text._ZN2cv7toUtf16ERKSs+0xb0): undefined reference to `std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_Rep::_S_empty_rep_storage'


collect2
: ld returned 1 exit status


This is my Android.mk:

LOCAL_PATH := $(call my-dir)

include $
(CLEAR_VARS)

OPENCV_LIB_TYPE
:=STATIC
OPENCV_INSTALL_MODULES
:=on

#to avoid conflicts between java OpenCV and C++ OpenCV
#OPENCV_CAMERA_MODULES:=off

#include ../OpenCV-2.3.1/share/OpenCV/OpenCV.mk
include
../OpenCV-2.4.1/share/opencv/OpenCV.mk

LOCAL_MODULE    
:= NativeProcessing
### Add all source file names to be included in lib separated by a whitespace
LOCAL_SRC_FILES
:= NativeProcessing.cpp SomethingElse.cpp AnotherOne.cpp

LOCAL_LDLIBS
+=  -llog #Enable inclusion of android/log.h
LOCAL_LDLIBS
+= -ldl
LOCAL_LDLIBS
+= -ljnigraphics #Enable inclusion of android/bitmap.h

include $
(BUILD_SHARED_LIBRARY)

Can anybody help me?

Andrey Pavlenko

unread,
Jun 18, 2012, 8:55:54 AM6/18/12
to
OK, it seems I've understood the root of the problem:
When releasing the OpenCV4Android package, we usually compile our code with the latest available NDK, for 2.4.1 it's NDK-r8.
If you use older NDK version for your app-s JNI part compilation, it can fail due to either internal NDK changes or some issues in OpenCV.
I've tried NDK-r7b and got the build error like you described above.
So please use the NDK-r8 for building with 2.4.1 package.
We'll investigate this and will fix if the issue is on OpenCV side.
Message has been deleted

Zohob

unread,
Jun 20, 2012, 5:13:12 AM6/20/12
to android...@googlegroups.com
After several usages of "clean" during the migration process it now works flawlessly. Thanks!

Rui Marques

unread,
Oct 13, 2012, 10:13:09 AM10/13/12
to android...@googlegroups.com
I think he means performing "Project > Clean Project" in the Eclipse IDE. Cleaning projects will re-build the projects.

On Thursday, October 4, 2012 11:50:05 PM UTC+1, mayoor sadavarte wrote:
@zohob - what do you mean by ''several usages of 'clean''?? could you please explain the solution a bit in detail??

suhad

unread,
Nov 2, 2012, 4:55:33 PM11/2/12
to android...@googlegroups.com
Dear Artem Storozhuk,
i am a student doing master in germany can you guide me how to install opencv ,i tryied to install but it wont run.
thanks in advance.

Artem Storozhuk

unread,
Nov 3, 2012, 5:53:47 AM11/3/12
to android...@googlegroups.com
What exactly have you tried?

Пʼятниця, 2 листопада 2012 р. 22:55:33 UTC+2 користувач suhad написав:

Csaba Bolyós

unread,
Nov 4, 2012, 5:01:43 AM11/4/12
to android...@googlegroups.com

Hi If I use the 2.4.3- rc. What NDK should I use? The latest available?

Thanks

2012.06.18. 14:07, "Andrey Pavlenko" <andrey....@itseez.com> ezt írta:
OK, it seems I've understood the root of the problem:
When releasing the OpenCV4Android package, we usually compile our code with the latest available NDK, for 2.4.1 it's NDK-r8.
If you use older NDK version for your app-s JNI part compilation, it can fail due to internal NDK changes breaking binary compatibility.

Artem Storozhuk

unread,
Nov 4, 2012, 2:09:16 PM11/4/12
to android...@googlegroups.com
Hi,

and which NDK did you try?


Неділя, 4 листопада 2012 р. 12:01:45 UTC+2 користувач bladeszasza написав:
Reply all
Reply to author
Forward
0 new messages