[英]OpenCV using Android ndk - 'could not be resolved' error
I'm working on an OpenCV project, using NDK. 我正在使用NDK开发OpenCV项目。 There are also some Java calls to OpenCV's library. 还有一些Java调用OpenCV的库。 The java calls are currently working fine, and the project compiles and runs fine. Java调用当前工作正常,项目可以编译并运行良好。 Once a native call is introduced however, the app crashes and then errors appear in the .cpp file referenced stating the could not be resolved
error (for eg. Symbol 'cv' could not be resolved
). 但是,一旦引入了本地调用,应用程序就会崩溃,然后在引用的.cpp文件中显示错误,指出could not be resolved
错误(例如, Symbol 'cv' could not be resolved
)。 Before opening the cpp file, there seem to be no errors, which is how the app is run initially. 在打开cpp文件之前,似乎没有错误,这是应用程序最初运行的方式。 With every Eclipse restart, the errors don't appear until triggered as mentioned. 每次重新启动Eclipse时,错误都不会出现,直到如上所述触发为止。
I've tested the cpp file beforehand on one of the OpenCV's provided samples (namely; sample 3 - native), and was working fine, so it must be somewhere in the linking process. 我已经在OpenCV提供的示例之一(即示例3-本机)上预先测试了cpp文件,并且工作正常,因此它必须在链接过程中。
After scanning through and comparing the project settings of the current project in question and the successfully running OpenCV's sample; 在浏览并比较当前有问题的项目设置和成功运行的OpenCV示例后; the differences I noticed were: 我注意到的差异是:
The project's Current ToolChain
(under C/C++ Build
> Tool Chain Editor
,in the project's properties) is Cygwin GCC
, while OpenCV's sample is No ToolChain
. 项目的Current ToolChain
(在项目属性中的C/C++ Build
> Tool Chain Editor
)是Cygwin GCC
,而OpenCV的示例是No ToolChain
。 I recall reading somewhere that this is a recommended setting, unsure why, but early when converting my Android project to a C/C++ project, I don't recall finding the tool chain option as adjustable. 我记得在某处读过这是推荐的设置,不确定为什么,但是在将我的Android项目转换为C / C ++项目的早期,我不记得找到可调的工具链选项。
The Configuration
in the project in question is Debug [Active]
while that in the OpenCV sample is Default [Active]
. 有关项目的Configuration
为Debug [Active]
而OpenCV示例中的配置为Default [Active]
。 I couldn't find the latter as an option in the Configuration
drop-down menu in my project. 我在项目的“ Configuration
下拉菜单中找不到后者作为选项。 The indexer build configuration
(Under C/C++ General
> Indexer
) in turn is also different where my project has Debug
, and OpenCV's sample has Default
. 反过来,在我的项目具有Debug
且OpenCV的示例具有Default
地方, indexer build configuration
(在C/C++ General
> Indexer
)也有所不同。
Below illustrates my project's aforementioned settings (not OpenCV's sample): 下面说明了我项目的上述设置(不是OpenCV的示例):
Tool Chain Editor settings 工具链编辑器设置
Indexer Build Configuration settings 索引器内部配置设置
The project is prepared with the recommended suggestions that can be found in most tutorials and in questions regarding the matter; 该项目是根据建议的建议准备的,可以在大多数教程和有关此问题的问题中找到这些建议; as adding necessary project paths and adding the NDKROOT
path (also mentioned here ). 作为添加必要的项目路径并添加NDKROOT
路径 (也在此处提到)。 Also the ndk-build
command has run successfully. ndk-build
命令也已成功运行。
Also, the method names in the cpp file are named as the naming convention that I've observed in the OpenCV sample, namely it follows this pattern; 同样,cpp文件中的方法名称被命名为我在OpenCV示例中观察到的命名约定,即遵循这种模式。 Java_packageName_callingJavaClass_functionName
. Java_packageName_callingJavaClass_functionName
。
Below is the Android.mk
outline: 以下是Android.mk
概述:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
include <Full Path To OpenCV>\OpenCV\sdk\native\jni\OpenCV.mk
LOCAL_MODULE := proc #The name referred to in System.loadLibrary() in the calling Android activity
LOCAL_SRC_FILES := proc.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
Application.mk
is as follows: Application.mk
如下:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi
EDIT Below is the logcat output for the first run (afterwards, compile errors, namely in the form of could not be resolved
errors, in the cpp are realised and thus no run is possible unless Eclipse is restarted). EDIT下面是第一次运行的logcat输出(此后,实现了cpp中的编译错误,即could not be resolved
错误形式,因此除非重新启动Eclipse,否则无法运行)。 median
is the native method called upon which the crash occurs. median
是发生崩溃时调用的本机方法。
01-29 19:14:23.786: W/dalvikvm(8750): No implementation found for native Lcom/ocv/MainActivity;.median (JJ)V
01-29 19:14:23.786: W/dalvikvm(8750): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
01-29 19:14:23.786: E/AndroidRuntime(8750): FATAL EXCEPTION: main
01-29 19:14:23.786: E/AndroidRuntime(8750): java.lang.UnsatisfiedLinkError: median
01-29 19:14:23.786: E/AndroidRuntime(8750): at com.ocv.MainActivity.median(Native Method)
01-29 19:14:23.786: E/AndroidRuntime(8750): at com.ocv.MainActivity.onActivityResult(MainActivity.java:155)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.app.ActivityThread.deliverResults(ActivityThread.java:3517)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3563)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.app.ActivityThread.access$2800(ActivityThread.java:126)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.os.Looper.loop(Looper.java:123)
01-29 19:14:23.786: E/AndroidRuntime(8750): at android.app.ActivityThread.main(ActivityThread.java:4633)
01-29 19:14:23.786: E/AndroidRuntime(8750): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 19:14:23.786: E/AndroidRuntime(8750): at java.lang.reflect.Method.invoke(Method.java:521)
01-29 19:14:23.786: E/AndroidRuntime(8750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-29 19:14:23.786: E/AndroidRuntime(8750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-29 19:14:23.786: E/AndroidRuntime(8750): at dalvik.system.NativeStart.main(Native Method)
EDIT Regarding the code used; 编辑关于使用的代码; for the .java activity code , namely MainActivity.java
, below are excerpts relevant to the native functionality. 下面的.java活动代码 MainActivity.java
是与本机功能相关的摘录。
// package name
package com.ocv;
// Class header
public class MainActivity extends Activity implements View.OnClickListener{
// native method declaration
public native void median(long matAddrGr, long matAddrRgba);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (OpenCVLoader.initDebug()){
/* This is line 155 referenced from logcat */
median(gray_img.getNativeObjAddr(), rgb_img.getNativeObjAddr());
}
}
}
Regarding the native code , proc.cpp
(following a similar pattern to that understood from OpenCV's samples): 关于本机代码 proc.cpp
(遵循与OpenCV示例中所理解的模式类似的模式):
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL Java_com_ocv_MainActivity_median(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{
Mat* pMatGr=(Mat*)addrGray;
Mat* pMatRgb=(Mat*)addrRgba;
vector<KeyPoint> v;
medianBlur(*pMatGr,*pMatRgb,3);
}
}
Regarding the library loading ; 关于图书馆的装载 ; OpenCV has successfully loaded (in the first run before the native cpp file's errors appear) as seen below (I even called Imgproc.cvtColor
successfully in Java (in the activity MainActivity.java
) before the native method median
was called): OpenCV已成功加载(在出现本机cpp文件错误之前的第一次运行中),如下所示(在调用本机方法median
之前,我什Imgproc.cvtColor
在Java中(在活动MainActivity.java
)也成功调用了Imgproc.cvtColor
):
The semantic errors resulting from proc.cpp
: proc.cpp
导致的语义错误 :
Are the above project properties' observations possibly relevant to the problem? 上述项目属性的观察结果可能与该问题有关吗? How can they be amended if so? 如果可以,如何修改? If not, any idea what might be causing the error? 如果不是,则可能是什么原因导致了该错误?
I'm using Eclipse Indigo Service Release 2 (and thus this solution to Eclipse Juno users is not applicable). 我正在使用Eclipse Indigo Service Release 2(因此该解决方案不适用于Eclipse Juno用户)。
I've found many similar questions online (some of which are linked earlier); 我在网上发现了许多类似的问题(其中一些早先已经链接了)。 but none of the suggested answers thus far seem to resolve the issue. 但到目前为止,所有建议的答案似乎都无法解决该问题。
Thank you in advance. 先感谢您。
When I had a similar problem I had to change this directory 当我遇到类似的问题时,我必须更改此目录
${ProjDirPath}/../../sdk/native/jni/include $ {} ProjDirPath SDK /../../ /本地/ JNI /包括
with this one 与这个
${ProjDirPath}/../OpenCV-2.4.3.2-android-sdk/sdk/native/jni/include $ {} ProjDirPath OpenCV的/../-2.4.3.2-Android的SDK / SDK /本地/ JNI /包括
However, someone said, it's a bug of eclipse that eclipse has difficult when include headers. 但是,有人说,eclipse的一个错误是当包含标头时,eclipse很难实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.