简体   繁体   English

在Android 2.3.3上对OpenCv使用本机代码时引发UnsatisfiedLinkError

[英]UnsatisfiedLinkError thrown while using native code for OpenCv on Android 2.3.3

I want to use OpenCV 2.4.0 native code in Android 2.3.3. 我想在Android 2.3.3中使用OpenCV 2.4.0本机代码。 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. 为此,我使用NDK release-8在Windows 7上使用ndk-build.cmd脚本构建了lib.so共享库。我使用Eclipse创建了一个项目并构建了.apk文件。

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. 这是问题所在,当我尝试使用Java代码中的System.loadLibrary( )加载共享库时,抛出UnsatisfiedLinkError异常。 I have tried doing the same thing with the hello-jni sample in NDK, and it works absolutely fine. 我已经尝试使用NDK中的hello-jni示例执行相同的操作,并且它绝对可以正常工作。

I followed instructions on this page for creating a project and for writing the Android.mk and Application.mk files. 我按照本页上的说明进行操作,以创建项目以及编写Android.mkApplication.mk文件。

Here's my java code: 这是我的Java代码:

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: 这是实现本机功能的(Project dir)/jni/ocvtest1.c文件:

#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 通过将libopencv_java.so和静态*.a库文件复制到(Project dir)/libs(Project dir)/obj/local文件夹中,并将Java源代码的静态块更改为使用的动态链接

static
{
    System.loadLibrary("opencv_java");
    System.loadLibrary("ocvtest");
}

Used static linking by adding OPENCV_LIB_TYPE:=STATIC as below 通过添加OPENCV_LIB_TYPE:=STATIC来使用静态链接,如下所示

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. 使用动态链接时,通过在命令提示符下执行javah.exe my.package.OCVTest1生成的头文件。

Changed armeabi-v7a to armeabi for the abi version variable in Application.mk file. 更改armeabi-v7aarmeabi在ABI版本变量Application.mk文件。

For all the above changes, to make sure that the library files were loaded into the app's lib folder, I executed 对于上述所有更改,为确保将库文件加载到应用程序的lib文件夹中,我执行了

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. 在尝试所有这些修复程序之后,我仍然无法解决此异常。

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 通过将libopencv_java.so和静态* .a库文件复制到(Project dir)/ libs和(Project dir)/ obj / local文件夹中,并将Java源代码的静态块更改为使用的动态链接

You have to copy only libopencv_java.so ( *.a file is not needed) to (Project dir)/libs/armeabi-v7a . 您只需libopencv_java.so (不需要*.a文件)复制到(Project dir)/libs/armeabi-v7a

Also you have: 您也有:

public native String funtionName1();

But in native c code there are only these functions: 但是在本机C代码中,只有以下功能:

jstring Java_my_package_ocvtest1_OCVTest1 (JNIEnv *ptr, jobject obj) // mistake?
jstring Java_my_package_ocvtest1_OCVTest1_functionName2 (JNIEnv *ptr, jobject obj)

It seems that name of the first function is wrong. 似乎第一个功能的名称是错误的。

That should fix your problem. 那应该解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM