I want to use OpenCV 2.4.0 native code in Android 2.3.3. For that, I used NDK release-8 to build lib.so shared libraries using the ndk-build.cmd script on Windows 7. I used Eclipse to create a project and build the .apk file.
Here’s the problem, I get an UnsatisfiedLinkError exception thrown when I try to load the shared libraries using System.loadLibrary() in the Java code. I have tried doing the same thing with the hello-jni sample in NDK, and it works absolutely fine.
I followed instructions on this page for creating a project and for writing the Android.mk and Application.mk files.
Here’s my java code:
package my.package.ocvtest1
// import android.foo.bar statements
public class OCVTest1 extends Activity
{
public void onCreate(Bundle savedInstance)
{
super.onCreate(Bundle savedInstance)
// code to display strings returned by native functions
}
public native String funtionName1();
public native String functionName2();
static
{
System.loadLibrary("ocvtest1");
}
}
Here’s the (Project dir)/jni/ocvtest1.c file which implement native functions:
#include <jni.h>
// Other header files and some global variables
jstring Java_my_package_ocvtest1_OCVTest1 (JNIEnv *ptr, jobject obj)
{
// code here
}
jstring Java_my_package_ocvtest1_OCVTest1_functionName2 (JNIEnv *ptr, jobject obj)
{
// code here
}
// End of file
I’ve googled this problem and tried the solutions, and even after implementing those solutions I get this exception. Here’s all the things I’ve tried till now:
Used dynamic linking by copying the libopencv_java.so and the static *.a library files into the (Project dir)/libs and (Project dir)/obj/local folders and changed static block of java source code to
static
{
System.loadLibrary("opencv_java");
System.loadLibrary("ocvtest");
}
Used static linking by adding OPENCV_LIB_TYPE:=STATIC as below
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
include (<Path to Opencv.mk>)
Included header file generated by executing javah.exe my.package.OCVTest1 in the command prompt while using dynamic linking.
Changed armeabi-v7a to armeabi for the abi version variable in Application.mk file.
For all the above changes, to make sure that the library files were loaded into the app’s lib folder, I executed
adb push <path to library on disk> <path to /lib in app>
which copied the library files into the proper directory on the phone.
After trying all of this fixes, I’m still not able to resolve this exception.
You have to copy only
libopencv_java.so(*.afile is not needed) to(Project dir)/libs/armeabi-v7a.Also you have:
But in native c code there are only these functions:
It seems that name of the first function is wrong.
That should fix your problem.