Leon
Leon

Reputation: 21

Android jni and opencv runtime couldn't load libs

Recently I'm writing android JNI codes with opencv for picture processing. When I port the C++ code to JNI, there's no error in compiling, but when I run the code on my phone, the program crashes all the time. The logcat shows that

'Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1892]: could not load needed library 'libopencv_java.so' for 'libmixed_sample.so' (load_library[1094] : Library 'libopencv_java.so' not found)'.

The error means that there's no libopencv_java.so library, so I copy the .so to JNI folder and run again and the same runtime error again.

I search for the answer for the problem, and find that 'add

System.loadLibrary("opencv_java")

to java code' may solve the problem. I tried, and the runtime error changed to

'Caused by: java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: find library returned null'.

I'm confused. I searched and tried many methods to solve this problem, but none of them can work.

Anyone know of the fix to this?

Thanks a lot!

Upvotes: 2

Views: 3215

Answers (3)

flaviussn
flaviussn

Reputation: 1355

You have to build a shared library of OpenCV

OPENCV_LIB_TYPE:=SHARED
OPENCV_PATH:=C:\PATH_TO_JNI\OpenCV.mk
$(info $(LOCAL_SRC_FILES))
include $(OPENCV_PATH)
include $(BUILD_SHARED_LIBRARY)

Upvotes: 0

Leon
Leon

Reputation: 21

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

include ../../sdk/native/jni/OpenCV.mk

LOCAL_MODULE    := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

Thank you a lot.

Upvotes: 0

Alex Cohn
Alex Cohn

Reputation: 57173

libopencv_java.so library should not be in JNI folder. To be automagically installed from your APK, put it in libs/armeabi subdirectory of your Android project. The trick is that if your project has custom JNI code, this directory will be purged during ndk-build. Therefore, you should add

LOCAL_PREBUILT_SHARED_LIBRARIES += ocvj

…

include $(CLEAR_VARS)
LOCAL_MODULE:= ocvj
LOCAL_SRC_FILES := $(FOO_PATH)/libopencv_java.so
include $(PREBUILD_SHARED_LIBRARY)

Maybe, you need to add more libraries. Maybe, you use armeabi-v7a instead of armeabi.

Upvotes: 0

Related Questions