使用 Android NDK 构建原生 OpenCV 可以带来"undefined reference to 'cv::String::deallocate()'"



我正试图在Android Studio中使用带有NDK的OpenCV。正如您可能注意到的,我正在使用另一个名为GStreamer的本地库。

我的建筑.gradle:

apply plugin: 'com.android.application'
def getNdkCommandLine(ndkRoot, target) {
    def gstRoot
    def opencvRoot
    gstRoot = 'C:/local/gstreamer-1.0-android-arm-1.8.0'
    opencvRoot = 'C:/local/OpenCV-3.1.0-android-sdk/OpenCV-android-sdk'
    if (ndkRoot == null)
        throw new GradleException('NDK not configured')
    return ["$ndkRoot/ndk-build.cmd",
            'NDK_PROJECT_PATH=build',
            'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
            'NDK_APPLICATION_MK=src/main/jni/Application.mk',
            'GSTREAMER_JAVA_SRC_DIR=src/main/java',
            "GSTREAMER_ROOT_ANDROID=$gstRoot",
            "OPENCV_ROOT_ANDROID=$opencvRoot",
            "$target"]
}
android {
    compileSdkVersion 19
    buildToolsVersion "23.0.2"
    sourceSets {
        main {
            // Avoid using the built in JNI generation plugin
            jni.srcDirs = []
            jniLibs.srcDirs = ['build/libs']
        }
    }
    defaultConfig {
        applicationId "com.mytestcom.mytestapp"
        minSdkVersion 19
        targetSdkVersion 19
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
        }
    }
    // Before compiling our app, prepare NDK code
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
    // Need to call clean on NDK ourselves too
    clean.dependsOn 'ndkClean'
    // Build native code using mk files like on Eclipse
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine getNdkCommandLine(android.ndkDirectory, 'TARGET_ARCH_ABI=armeabi-v7a')
    }
    task ndkClean(type: Exec, description: 'Clean JNI code built via NDK') {
        commandLine getNdkCommandLine(android.ndkDirectory, 'clean')
    }
    //Renames APK to current versionName found in Android Manifest
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = "My_Test_App-${versionName}.apk"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }
}
dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile project(':openCVLibrary310')
}

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gstplayer
LOCAL_SRC_FILES := player.cpp
LOCAL_C_INCLUDES := $(OPENCV_ROOT_ANDROID)/sdk/native/jni/include
LOCAL_SHARED_LIBRARIES := gstreamer_android
LOCAL_LDLIBS := -llog -landroid
include $(BUILD_SHARED_LIBRARY)
ifeq ($(TARGET_ARCH_ABI),armeabi)
GSTREAMER_ROOT        := $(GSTREAMER_ROOT_ARM)
else ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
GSTREAMER_ROOT        := $(GSTREAMER_ROOT_ARMV7)
else ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
GSTREAMER_ROOT        := $(GSTREAMER_ROOT_ARM64)
else ifeq ($(TARGET_ARCH_ABI),x86)
GSTREAMER_ROOT        := $(GSTREAMER_ROOT_X86)
else ifeq ($(TARGET_ARCH_ABI),x86_64)
GSTREAMER_ROOT        := $(GSTREAMER_ROOT_X86_64)
else
$(error Target arch ABI not supported)
endif
ifndef GSTREAMER_ROOT
ifndef GSTREAMER_ROOT_ANDROID
$(error GSTREAMER_ROOT_ANDROID is not defined!)
endif
GSTREAMER_ROOT        := $(GSTREAMER_ROOT_ANDROID)
endif
GSTREAMER_NDK_BUILD_PATH  := $(GSTREAMER_ROOT)/share/gst-android/ndk-build/
include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk
GSTREAMER_PLUGINS         := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_PLAYBACK) $(GSTREAMER_PLUGINS_CODECS) $(GSTREAMER_PLUGINS_NET) $(GSTREAMER_PLUGINS_SYS) $(GSTREAMER_PLUGINS_CODECS_RESTRICTED) $(GSTREAMER_CODECS_GPL) $(GSTREAMER_PLUGINS_ENCODING) $(GSTREAMER_PLUGINS_VIS) $(GSTREAMER_PLUGINS_EFFECTS) $(GSTREAMER_PLUGINS_NET_RESTRICTED)
GSTREAMER_EXTRA_DEPS      := gstreamer-player-1.0 gstreamer-video-1.0 glib-2.0
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERAMODULES:=off
OPENCV_LIB_TYPE:=STATIC
include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk
include $(OPENCV_ROOT_ANDROID)/sdk/native/jni/OpenCV.mk

Application.mk:

APP_PLATFORM=android-19
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a

我使用的是OpenCV-3.1.0-android-sdk,NDK r10e,android Studio 2.1,Windows。我尝试过使用不同版本的OpenCV(2.4.11),但没有帮助。

C:localOpenCV-3.1.0-android-sdkOpenCV-android-sdksdknativejniincludeopencv2corecvstd.hpp
Error:(625) undefined reference to 'cv::String::allocate(unsigned int)'
Error:(667) undefined reference to 'cv::String::deallocate()'
Error:(667) undefined reference to 'cv::String::deallocate()'
Error:(667) undefined reference to 'cv::String::deallocate()'
Error:(667) undefined reference to 'cv::String::deallocate()'
C:localOpenCV-3.1.0-android-sdkOpenCV-android-sdksdknativejniincludeopencv2coremat.inl.hpp
Error:(443) undefined reference to 'cv::error(int, cv::String const&, char const*, char const*, int)'
Error:(459) undefined reference to 'cv::error(int, cv::String const&, char const*, char const*, int)'
Error:(682) undefined reference to 'cv::Mat::deallocate()'
Error:(571) undefined reference to 'cv::fastFree(void*)'
Error:(592) undefined reference to 'cv::Mat::copySize(cv::Mat const&)'

几天前我遇到了类似的问题。伊莫,你应该试着用LOCAL_LDLIBS += -llog -landroid替换LOCAL_LDLIBS := -llog -landroid

对这些错误的解释是,GStreamer中使用的OpenCV代码无法访问库的其余部分,因此无法找到allocate()deallocate()等基本方法。

解决方案是在Android.mk中包含一些预构建的静态库,以便定义这些引用。所以在include $(OPENCV_ROOT_ANDROID)/sdk/native/jni/OpenCV.mk之后,我添加了这些行来添加相关的库:

include $(CLEAR_VARS)
LOCAL_MODULE := opencv-tbb
LOCAL_SRC_FILES := opencvLib/3rdparty/libs/$(TARGET_ARCH_ABI)/libtbb.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv-core
LOCAL_SRC_FILES := opencvLib/$(TARGET_ARCH_ABI)/libopencv_core.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/opencvLib/include/
LOCAL_STATIC_LIBRARIES := opencv-tbb
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv-imgproc
LOCAL_SRC_FILES := opencvLib/$(TARGET_ARCH_ABI)/libopencv_imgproc.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/opencvLib/include/
LOCAL_STATIC_LIBRARIES := opencv-core
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_EXPORT_C_INCLUDES变量设置与该库相对应的包含文件。在这种情况下,include文件夹适用于这两个库。

现在,为了让GStreamer代码能够使用这些库,我们必须将LOCAL_STATIC_LIBRARIES := opencv-core opencv-imgproc添加到gstplayer模块中,该模块引用了添加的2个OpenCV模块。

编辑:

此外,我忘了提及我在Application.mk 中将APP_STL := gnustl_static更改为APP_STL := gnustl_shared

相关内容

  • 没有找到相关文章