Android OpenCV 未定义对本机类的引用

Posted

技术标签:

【中文标题】Android OpenCV 未定义对本机类的引用【英文标题】:Android OpenCV undefined refrence to native class 【发布时间】:2017-02-25 18:39:06 【问题描述】:

我在 openCv 参考中创建我的 android 项目作为指令并配置我的项目,在 SDK 中添加 java 作为模块并复制 jinLibs 中的所有库(不同的 CPU 类型)!我有一个名为libdetection_based_tracker的本地类

当我尝试构建我的项目时,我遇到了这个异常:

CMakeFiles/libdetection_based_tracker.dir/src/main/cpp/libdetection_based_tracker.cpp.o: In function `Java_ir_whiteapp_maremat_DetectionBasedTracker_nativeCreateObject':
G:\AR\Maremat\app\src\main\cpp/libdetection_based_tracker.cpp:28: undefined reference to `DetectionBasedTracker::Parameters::Parameters()'
G:\AR\Maremat\app\src\main\cpp/libdetection_based_tracker.cpp:31: undefined reference to `DetectionBasedTracker::DetectionBasedTracker(std::string const&, DetectionBasedTracker::Parameters const&)'
CMakeFiles/libdetection_based_tracker.dir/src/main/cpp/libdetection_based_tracker.cpp.o: In function `Java_ir_whiteapp_maremat_DetectionBasedTracker_nativeSetFaceSize':
G:\AR\Maremat\app\src\main\cpp/libdetection_based_tracker.cpp:64: undefined reference to `DetectionBasedTracker::getParameters()'
G:\AR\Maremat\app\src\main\cpp/libdetection_based_tracker.cpp:66: undefined reference to `DetectionBasedTracker::setParameters(DetectionBasedTracker::Parameters const&)'
CMakeFiles/libdetection_based_tracker.dir/src/main/cpp/libdetection_based_tracker.cpp.o: In function `cv::Mat::operator=(cv::Mat const&)':

这是我的应用级 gradle 文件:

apply plugin: 'com.android.application'

android 
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig 
        applicationId "ir.whiteapp.maremat"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild 
            cmake 
                cppFlags ""
            
        
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    
    externalNativeBuild 
        cmake 
            path "CMakeLists.txt"
        
    
    sourceSets  main  jni.srcDirs = ['src/main/jni', 'src/main/jniLibs/']  


dependencies 
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
        exclude group: 'com.android.support', module: 'support-annotations'
    )
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
    compile project(':openCVLibrary320')

还有我的 CmakeList

cmake_minimum_required(VERSION 3.4.1)

include_directories(src/main/jniLibs/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION  $CMAKE_CURRENT_SOURCE_DIR/src/main/jniLibs/$ANDROID_ABI/libopencv_java3.so)
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )
add_library( libdetection_based_tracker SHARED src/main/cpp/libdetection_based_tracker.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.
                   native-lib

                   # Links the target library to the log library
                   # included in the NDK.
                   $log-lib )

整个项目是here,没有jinLibs文件!这是我第一次尝试使用 NDK 和 OpenCV,然后问我是否有一部分问题含糊不清!

【问题讨论】:

【参考方案1】:

根据this,Android.mk 必须如下所示:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := asmlibrary

LOCAL_SRC_FILES := so/$(TARGET_ARCH_ABI)/libasmlibrary.so #must be included
include $(PREBUILT_SHARED_LIBRARY) 

include $(CLEAR_VARS)

#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=off
#OPENCV_LIB_TYPE:=SHARED

include /youropencv/OpenCV-2.4.6-android-sdk/sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES  := DemoFit.cpp

LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_CFLAGS    += -DOPENCV_OLDER_VISION #(if still get errors, disable OPENCV_OLDER_VISION )

LOCAL_LDLIBS     += -llog -ldl  

LOCAL_MODULE     := jni-asmlibrary

LOCAL_SHARED_LIBRARIES := asmlibrary

include $(BUILD_SHARED_LIBRARY)

【讨论】:

我下载了 OpenCV 3.2.0 但我的 lib 文件夹不包含 libasmlibrary.so!!我的项目有必要吗??

以上是关于Android OpenCV 未定义对本机类的引用的主要内容,如果未能解决你的问题,请参考以下文章

对函数 Android NDK 的未定义引用

CMAKE未定义引用'cv :: fastFree(void *)错误的OpenCV Android实现

OpenCV:对 imread() 的未定义引用

OpenCV“对'cv :: imread'等C ++的未定义引用

OPENCV 错误:未定义对“cvLoadImage”的引用

OpenCv3.3安装错误——未定义引用