Android Studio opencv C++ 在运行时编译链接库错误
Posted
技术标签:
【中文标题】Android Studio opencv C++ 在运行时编译链接库错误【英文标题】:Android Studio opencv C++ compile link libraries error at runtime 【发布时间】:2015-09-30 04:18:17 【问题描述】:我正在使用带有 NDK 的 Gradle 2.6 的 android Studio 1.3.2。
这是我的错误:
dlopen("/data/app/face.rt.jeanc.facert-2/lib/arm/libFace.so", RTLD_LAZY) failed: dlopen failed: "/data/app/face.rt.jeanc.facert-2/lib/arm/libFace.so" has unexpected e_machine: 40
另外,我的 lib Face 应该在我的文件夹(armabi、armabi-v7 等)中显示为 libFace.so,但不会出现(编译?)
这是我的 build.gradle:
apply plugin: 'com.android.model.application'
model
android
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with
applicationId = "face.rt.jeanc.facert"
minSdkVersion.apiLevel = 19
targetSdkVersion.apiLevel = 19
versionCode = 1
versionName = "1.0.1"
android.buildTypes
release
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
android.ndk // keeping it to make AS correctly support C++ code editing and debugging
moduleName = "Face"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I$file("src/main/jni/prebuilts/include")".toString()
cppFlags += "-I$file("C:/Android-dev/SDKs/OpenCV-android-sdk/sdk/native/jni/include")".toString()
cppFlags += "-I$file("C:/Android-dev/SDKs/OpenCV-android-sdk/sdk/native/jni/include/opencv")".toString()
cppFlags += "-I$file("C:/Android-dev/SDKs/OpenCV-android-sdk/sdk/native/jni/include/opencv2")".toString()
ldLibs += ["android", "EGL", "GLESv2", "dl", "log", "z"]// , "ibopencv_core"
stl = "gnustl_shared" //"stlport_static"
android.productFlavors
// for detailed abiFilter descriptions, refer to "Supported ABIs" @
// https://developer.android.com/ndk/guides/abis.html#sa
create("arm")
ndk.with
abiFilters += "armeabi"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
create("armv7")
ndk.with
abiFilters += "armeabi-v7a"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi-v7a\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
create("x86")
ndk.with
abiFilters += "x86"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\x86\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
create("mips")
ndk.with
abiFilters += "mips"
create("fat")
dependencies
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile project(':openCVLibrary2411')
这是我“应该”加载不同库的代码的一部分:
////// JNI C++
static System.loadLibrary("Face");
//Externals C++ Libraries
static System.loadLibrary("libnative_camera_r4.3.0.so");
static System.loadLibrary("libopencv_contrib.a");
static System.loadLibrary("libopencv_core.a");
static System.loadLibrary("libopencv_highgui.a");
static System.loadLibrary("libopencv_imgproc.a");
static System.loadLibrary("libopencv_info.so");
static System.loadLibrary("libopencv_java.so");
static System.loadLibrary("libopencv_legacy.a");
static System.loadLibrary("libopencv_ml.a");
static System.loadLibrary("libopencv_ts.a");
我现在有点卡住了,需要一些帮助。
编辑:在这个版本的 Android Studio 和实验插件中,我没有 Android.Mk 文件来编译 jni c++。所以我想知道现在是它自己完成还是我必须手动编译?
如果我没有 Android.Mk,我该如何手动操作?
【问题讨论】:
【参考方案1】:您的构建确实成功,但您没有将共享库从 OpenCV 复制到应用程序的 libs 目录。遵循How to use opencv in android studio using gradle build tool?中的建议
请注意,可以将 Android.mk 的全部功能与 experimental 插件相结合:请参阅 define LOCAL_SRC_FILES in ndk DSL。
【讨论】:
我在你发送的第一个线程和第二个线程中做了所有事情,它仍然无法正常工作。 不工作 - 比如什么?你的 Android.mk 在做什么? 当我尝试编译时,我得到这个:[armeabi] Compile++ thumb: Face ^ 编译终止。 对于 OpenCV 3.0,您需要NDK_TOOLCHAIN_VERSION=4.8 APP_STL=gnustl_static
。这些是在Application.mk
还是在 ndk-build ocmm 命令行中定义的?
实际上我使用的是 OpenCV 2.4.11 for Android 而不是 3.0 版,但是这两个变量都已经在我的新 Application.mk 中设置了(因为我之前没有)跨度>
以上是关于Android Studio opencv C++ 在运行时编译链接库错误的主要内容,如果未能解决你的问题,请参考以下文章
Opencv不同版本与visual studio2013的环境配置
visual studio 2019中C++安装opencv