title: Opencv in android studio does not work?
body: <p>I'm trying to add native code to my Android app. NDK worked fine with simple C++ code like "Hello from C++", but I need to use openCV, and I am stuck.</p>
  
  <p>Project build output:</p>
  
  <pre><code>:app:generateArmDebugSources UP-TO-DATE
  :app:compileArmDebugJava UP-TO-DATE
  :app:compileArmDebugNdk
  D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::~Mat():jni_part.cpp(.text._ZN2cv3MatD2Ev+0x3c): error: undefined reference to 'cv::fastFree(void*)'
  D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::release():jni_part.cpp(.text._ZN2cv3Mat7releaseEv+0x6c): error: undefined reference to 'cv::Mat::deallocate()'
  D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::FastFeatureDetector::~FastFeatureDetector():jni_part.cpp(.text._ZN2cv19FastFeatureDetectorD1Ev+0xa0): error: undefined reference to 'cv::FeatureDetector::~FeatureDetector()'
  ...25+ similar lines...
  collect2: ld returned 1 exit status
  make.exe: *** [D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/libjni_part.so] Error 1
  
   FAILED
  
  FAILURE: Build failed with an exception.
  
  * What went wrong:
  Execution failed for task ':app:compileArmDebugNdk'.
  &gt; com.android.ide.common.internal.LoggedErrorException: Failed to run command:
      D:\android-ndk-r10c\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\Android.mk APP_PLATFORM=android-21 NDK_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj NDK_LIBS_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\lib NDK_DEBUG=1 APP_STL=stlport_static APP_ABI=armeabi-v7a
  Error Code:
      2
  FAILURE: Build failed with an exception.
  
  * What went wrong:
  Execution failed for task ':app:compileArmDebugNdk'.
  &gt; com.android.ide.common.internal.LoggedErrorException: Failed to run command:
      D:\android-ndk-r10c\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\inter
  
  mediates\ndk\arm\debug\Android.mk APP_PLATFORM=android-21 NDK_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj NDK_LIBS_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\lib NDK_DEBUG=1 APP_STL=stlport_static APP_ABI=armeabi-v7a
  Error Code:
      2
  Output:
      D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::~Mat():jni_part.cpp(.text._ZN2cv3MatD2Ev+0x3c): error: undefined reference to 'cv::fastFree(void*)'
      D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::release():jni_part.cpp(.text._ZN2cv3Mat7releaseEv+0x6c): error: undefined reference to 'cv::Mat::deallocate()'
      D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::FastFeatureDetector::~FastFeatureDetector():jni_part.cpp(.text._ZN2cv19FastFeatureDetectorD1Ev+0xa0): error: undefined reference to 'cv::FeatureDetector::~FeatureDetector()'
      ...and 25+ similar lines again...
  collect2: ld returned 1 exit status
      make.exe: *** [D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/libjni_part.so] Error 1
  
  
  * Try:
  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  
  
  
   BUILD FAILED
  
  Total time: 12.261 secs
  </code></pre>
  
  <p>I noticed a similar question <a href="https://stackoverflow.com/a/22427267/4595220">https://stackoverflow.com/a/22427267/4595220</a> but I still haven't found a solution. What I am doing wrong?</p>
  
  <p>build.gradle</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 21
      buildToolsVersion "21.1.2"
  
      defaultConfig {
          applicationId "com.gogiant.opencvfeaturedetection"
          minSdkVersion 15
          targetSdkVersion 21
      versionCode 1
      versionName "1.0"
  
      ndk {
          moduleName "jni_part"
          cFlags "-DANDROID_NDK"
          ldLibs "log"
          stl "stlport_static"
      }
  }
  
  buildTypes {
      release {
  
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
  
      // make per-variant version code
      applicationVariants.all { variant -&gt;
          // get the single flavor
          def flavorVersion = variant.productFlavors.get(0).versionCode
  
          // set the composite code
          variant.mergedFlavor.versionCode = flavorVersion * 1000000 + defaultConfig.versionCode
      }
  
      productFlavors {
          x86 {
              ndk {
                  abiFilter "x86"
              }
  
              // this is the flavor part of the version code.
              // It must be higher than the arm one for devices supporting
              // both, as x86 is preferred.
              versionCode = 3
          }
          arm {
              ndk {
                  abiFilter "armeabi-v7a"
              }
              versionCode = 2
          }
          mips {
              ndk {
                  abiFilter "mips"
              }
              versionCode = 1
          }
          fat {
              // fat binary, lowest version code to be
              // the last option
              versionCode = 0
          }
      }
  
          debug.jniDebuggable true
      }
  }
  
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:21.0.3'
      compile 'com.android.support:support-v4:21.0.3'
      compile project(':libraries:opencv')
  }
  </code></pre>
  
  <p>Android.mk</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
  OPENCV_LIB_TYPE:=STATIC
  OPENCV_INSTALL_MODULES:=on
  OPENCV_CAMERA_MODULES:=off
  
  
  include D:\OpenCV-2.4.9-android-sdk\sdk\native\jni\OpenCV.mk
  
  LOCAL_C_INCLUDES += D:\AndroidStudioProjects\OpenCVFeatureDetection\libraries\opencv\include
  
  
  LOCAL_MODULE    := jni_part
  LOCAL_SRC_FILES := jni_part.cpp
  
  LOCAL_STATIC_LIBRARIES += libopencv_contrib libopencv_legacy libopencv_ml libopencv_stitching libopencv_nonfree libopencv_objdetect libopencv_videostab libopencv_calib3d libopencv_photo libopencv_video libopencv_features2d libopencv_highgui libopencv_androidcamera libopencv_flann libopencv_imgproc libopencv_ts libopencv_core
  
  LOCAL_LDLIBS +=  -llog -ldl
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>Application.mk</p>
  
  <pre><code>APP_STL := gnustl_static
  APP_CPPFLAGS := -frtti -fexceptions
  APP_ABI := all
  APP_PLATFORM := android-8
  </code></pre>
  
  <p>Project structure: <a href="https://i.stack.imgur.com/Yg5Jl.png" rel="nofollow noreferrer">http://i.stack.imgur.com/Yg5Jl.png</a>
  Complete project: <a href="https://yadi.sk/d/brR5GUjber2Qo" rel="nofollow noreferrer">https://yadi.sk/d/brR5GUjber2Qo</a></p>
  
  <p>Thanks for any help.</p>