2016-11-28 1 views
1

나는 안드로이드 스튜디오 2.2에서 OpenCV의 기본 libs와 연결하기 위해 노력하고있어. 내가 찾은 모든 주제는 Android.mk 파일 또는 Android Studio가 찾을 수없는 build.gradle 파일의 다른 메소드를 사용하는 것입니다.안드로이드 스튜디오 2.2 링크 OpenCV의 정적 라이브러리

나는 안드로이드 스튜디오 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 파일에서 수행해야합니다. 내가 정적 라이브러리를 연결하는 방법을 잘 모릅니다 때문에 내가 여기에 붙어

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' 
} 

:

이 지금까지 내 build.gradle 파일입니다. 이미 동일한 문제에 대한 많은 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

당신은 CMakeLists.txt에 라이브러리로 'opencv.hpp'을 추가 했습니까? – 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) 
관련 문제