2016-11-28 121 views
1

我想鏈接Android Studio 2.2中的opencv本機庫。我發現的所有主題都是在Android Studio無法找到的build.gradle文件中使用Android.mk文件或其他方法。Android Studio 2.2鏈接opencv靜態庫

我正在使用Android Studio 2.2並通過使用C++支持創建一個新項目來創建該項目。

到目前爲止,我成功地包括在C++源文件OpenCV的庫:

#include <jni.h> 
#include <string> 
#include "opencv.hpp" 

extern "C" 
jstring 
Java_com_rvstudios_roomscanner_capp_MainActivity_stringFromJNI(
     JNIEnv *env, 
     jobject /* this */) { 
    std::string hello = "Hello from C++"; 

    cv::Mat image; 
    cv::cvtColor(image, image, CV_BGR2GRAY); 

    return env->NewStringUTF(hello.c_str()); 
} 

當我嘗試建立這個我得到了OpenCV函數未定義引用錯誤,是因爲在靜態庫(。一個文件)必須被鏈接。從我已經讀過的應該在build.gradle文件中完成。

這是迄今爲止我的build.gradle文件:

apply plugin: 'com.android.application' 

def targetPlatform = "mips" 

android { 
    compileSdkVersion 23 
    buildToolsVersion "25.0.1" 
    defaultConfig { 
     applicationId "com.rvstudios.roomscanner.capp" 
     minSdkVersion 18 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     externalNativeBuild { 
      cmake { 
       cppFlags "" 
       cppFlags.add("-isystem${project.rootDir}/app/src/main/cpp/vision".toString()) 
      } 
     } 
    } 
    sourceSets.main { 
     jni.srcDirs = ["${project.rootDir}/app/src/main/jniLibs/${targetPlatform.toString()}/"] 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    externalNativeBuild { 
     cmake { 
      path "CMakeLists.txt" 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    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:23.4.0' 
    compile 'com.android.support:design:23.4.0' 
    testCompile 'junit:junit:4.12' 
} 

我堅持在這裏,因爲我不知道如何來鏈接靜態庫。我已經看到了大量有關同樣問題的SO主題,但是我閱讀的所有內容都使用不同的項目設置,而且我對Android開發不熟悉,所以我根本就不瞭解它。

EDIT1: 我的CMakeLists.txt:

# Sets the minimum version of CMake required to build the native 
# library. You should either keep the default value or only pass a 
# value of 3.4.0 or lower. 

cmake_minimum_required(VERSION 3.4.1) 

# Creates and names a library, sets it as either STATIC 
# or SHARED, and provides the relative paths to its source code. 
# You can define multiple libraries, and CMake builds it for you. 
# Gradle automatically packages shared libraries with your APK. 

add_library(# Sets the name of the library. 
      native-lib 

      # Sets the library as a shared library. 
      SHARED 

      # Provides a relative path to your source file(s). 
      # Associated headers in the same location as their source 
      # file are automatically included. 
      src/main/cpp/native-lib.cpp 
      src/main/cpp/vision/opencv.hpp) 

# Searches for a specified prebuilt library and stores the path as a 
# variable. Because system libraries are included in the search path by 
# default, you only need to specify the name of the public NDK library 
# you want to add. CMake verifies that the library exists before 
# completing its build. 

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) 

# Specifies libraries CMake should link to your target library. You 
# can link multiple libraries, such as libraries you define in the 
# build script, prebuilt third-party libraries, or system libraries. 

target_link_libraries(# Specifies the target library. 
         native-lib 

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

         ) 
+0

你加 'opencv.hpp' 作爲一個庫的CMakeLists.txt? – mcemilg

+0

是的,我做到了!我將我的CMakeLists.txt添加到問題領域! –

+0

嘗試包含'opencv.hpp'文件,如下所示; #include「vision/opencv.hpp」 – mcemilg

回答

0

到的CMakeLists.txt,添加:

set(pathToOpenCV /Users/admin/Projects/OpenCV-android-sdk/sdk/native/) // Replace with your OpenCV SDK path 
include_directories(${pathToOpenCV}/jni/include)