我已将 OpenCV 模块依赖项从 3.4.3 更新到 4.1.1,现在我收到消息
OpenCV Manager package was not found
尽管我已经包含了所有 libopencv_java4.so 原生库。
对于以前的版本 (3.4.x),仅当我省略包含 native *.so
库时才需要包管理器,但对于此版本 (4.1.1) 似乎没有任何区别,每次都要求 OpenCV Manager。
我不希望该应用程序依赖于单独的 OpenCV 管理器。我该如何解决这个错误?
错误日志是:
OpenCV error: Cannot load info library for OpenCV
W/System.err: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
W/System.err: at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
W/System.err: at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
W/System.err: at java.lang.System.loadLibrary(System.java:1667)
W/System.err: at org.opencv.android.StaticHelper.loadLibrary(StaticHelper.java:64)
W/System.err: at org.opencv.android.StaticHelper.initOpenCVLibs(StaticHelper.java:95)
W/System.err: at org.opencv.android.StaticHelper.initOpenCV(StaticHelper.java:39)
W/System.err: at org.opencv.android.OpenCVLoader.initDebug(OpenCVLoader.java:107)
W/System.err: at com.mysite.myapp.OpenCVTestActivity.onResume(OpenCVTestActivity.java:144)
W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)
W/System.err: at android.app.Activity.performResume(Activity.java:7939)
W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
W/System.err: at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
W/System.err: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:107)
W/System.err: at android.os.Looper.loop(Looper.java:214)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7356)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
编辑
问题与模块配置无关,而是由 OpenCV native 库崩溃引起的,由于某种原因,以前的 3.4.x 版本不会发生这种情况。出现此错误后,重新初始化 OpenCV 模块将无法触发此错误。
最佳答案
我创建了一个 Android C++/C 项目,我意识到要使用 openCV 4.1.1 我们需要在 gradle中强制使用 CMAKE强>.因此,如果我们创建一个 dummy CMAKE 和一个 dummy-lib,库 libc++_shared.so 将添加到 APK。
然后,首先创建一个cpp文件夹,里面有CMakeLists.txt和dummy-lib.cpp文件在/app/src/main/ 目录。
CMakeLists.txt 是一个虚拟文件,里面放这个:
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
dummy-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
dummy-lib.cpp)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
target_link_libraries( # Specifies the target library.
dummy-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
在dummy-lib.cpp中添加:
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
whatever(
JNIEnv *env,
jobject /* this */){
std::string hello = "Hello";
return env->NewStringUTF(hello.c_str());
};
然后,在应用的gradle文件中添加:
android {
....
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments "-DANDROID_STL=c++_shared"
}
}
}
...
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
所以,制作项目,一切都完成了!
如果分析生成的APK,可以看到里面的库libc++_shared.so!
关于android - 从 3.4.3 更新到 4.1.1 后无法检测到 OpenCV 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57698796/