title: Use NDK in Android Studio (OpenCV)
body: <p>I am trying to setup OpenCV in Android Studio, using NDK. The steps I have followed are:</p>
  
  <ol>
  <li>Create a <strong>library project</strong> for <em>OpenCV</em> and import it in <em>MyProject</em>. </li>
  <li>Add all OpenCV <strong>library files</strong> to the <em>/jni</em> folder.</li>
  <li>Create <strong>Android.mk</strong> file</li>
  </ol>
  
  <p>However, when I run the Android.mk with <strong>ndk-build</strong> I get this error:</p>
  
  <pre><code>Nothing to be done for `.../MyProject/jni/Android.mk'.
  </code></pre>
  
  <p>This is the content of Android.mk:</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  
  include $(CLEAR_VARS)
  
  OPENCV_CAMERA_MODULES:=on
  OPENCV_INSTALL_MODULES:=on
  
  include /.../MyProject/jni/OpenCV.mk
  </code></pre>
  
  <p>I am stuck at building the libraries, any help?</p>
  
  <hr>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>I managed to use NDK by linking gradle to an external installation of it, and by also using <em>javah</em> to created implementation headers for native methods.
  Follow these steps to setup Gradle to use NDK: </p>
  
  <ul>
  <li><p><strong>install</strong> ndk (it's not built into Android Studio, so it needs to be installed manually)</p></li>
  <li><p>setup ndk.dir in local.properties and ndkDir in gradle.properties to link to <strong>installation path</strong></p></li>
  <li><p>create "<strong>jni</strong>" folder in src/main </p></li>
  <li><p>add <strong>Application.mk</strong> file to jni</p>
  
  <p>APP_ABI := armeabi
  APP_PLATFORM := android-9</p></li>
  <li><p>add <strong>Android.mk</strong> file to jni (change LOCAL_SRC_FILES and LOCAL_MODULE values)</p>
  
  <p>LOCAL_PATH := $(call my-dir)</p>
  
  <p>include $(CLEAR_VARS)</p>
  
  <p>LOCAL_SRC_FILES := main.c
  LOCAL_LDLIBS += -llog
  LOCAL_MODULE := hello</p>
  
  <p>include $(BUILD_SHARED_LIBRARY)</p></li>
  <li><p>add this code to gradle</p>
  
  <pre><code>    sourceSets.main.jni.srcDirs = []
  
      // take the built .so files and place them in jniLibs
      //noinspection GroovyAssignabilityCheck
      task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
          commandLine "$ndkDir/ndk-build",
                  'NDK_PROJECT_PATH=build/intermediates/ndk',
                  'NDK_LIBS_OUT=src/main/jniLibs',
                  'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                  'NDK_APPLICATION_MK=src/main/jni/Application.mk'
      }
  
      tasks.withType(JavaCompile) {
          compileTask -&gt; compileTask.dependsOn ndkBuild
      }
  </code></pre></li>
  <li><p><strong>setup javah</strong> as External tool (point to your installation directory)
  Program: /usr/bin/javah
  Params: -v -jni -d $ModuleFileDir$/src/main/jni $FileClass$
  Working dir: $SourcepathEntry$</p></li>
  <li><p><strong>run javah</strong> on .java file containing native method -> header is created</p></li>
  <li><p>create implementation file</p></li>
  <li><p>specifiy module in gradle:</p>
  
  <pre><code>defaultConfig {
      minSdkVersion 9
      targetSdkVersion 21
      versionCode 4
      versionName '1.11'
  
      ndk {
          moduleName "hello"
      }
  }
  </code></pre></li>
  <li><p>done!</p></li>
  </ul>