1

I want to utilize openCV in an android app that I'm creating but I do not know how to set up the NDK and openCV in android studio. I also want to be able to upload my project to github so it can be imported and worked on by others. Could someone please explain how I can install the NDK into android studio and include openCV for android (also, will I have to change my .gitignore file).

core16
  • 113
  • 2
  • 17
  • NDK support in Android Studio is still not official; some nice people have published different workarounds, but these tend to become obsolete with new releases of AS. Integration of a sophisticated library such as OpenCV may be especially painful. I would recommend to stick with **[ADT](http://stackoverflow.com/questions/tagged/adt)** or wrap your OpenCV functionality in a library project, maintain the latter in ADT, and use this library in AS. – Alex Cohn Jun 12 '14 at 06:30
  • The only problem I have with ADT is that it creates a folder for the support library which gives me several problems with version control. – core16 Jun 24 '14 at 16:45
  • Try this? http://stackoverflow.com/a/35135495/5611377 – ssimm Oct 28 '16 at 16:47

1 Answers1

1

for OpenCV you can't yet directly use the Android Studio default way to call the NDK.

But you can integrate all your code and OpenCV like you would do for an eclipse project, and configure gradle to call ndk-build manually on your Android.mk file and integrating your .so files.

Here is how you can do this, from your build.gradle file:

import org.apache.tools.ant.taskdefs.condition.Os
...

android {
  ...

  sourceSets.main {
      jniLibs.srcDir 'src/main/libs' // use libs directory to get .so files, instead of jniLibs
      jni.srcDirs = [] //disable automatic ndk-build call
  }

  // call regular ndk-build(.cmd) script from app directory
  task ndkBuild(type: Exec) {
      if (Os.isFamily(Os.FAMILY_WINDOWS)) {
          commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
      } else {
          commandLine 'ndk-build', '-C', file('src/main').absolutePath
      }
  }

  tasks.withType(JavaCompile) {
      compileTask -> compileTask.dependsOn ndkBuild
  }
}
ph0b
  • 14,353
  • 4
  • 43
  • 41
  • And where do I put the NDK and openCV files/folders? – core16 Jun 13 '14 at 05:33
  • Do you mean you're not using the NDK already ? If you want to use the Java API for OpenCV, you don't need the NDK. You just need to copy the `/sdk/native/libs/` folder as a `jniLibs` folder inside your Android Studio project. You can even rely on OpenCV manager as describe inside this documentation: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android and you can get the OpenCV Java library as an Android Studio project from there: https://github.com/vRallev/OpenCV – ph0b Jun 13 '14 at 13:50
  • Sorry for the delay, I haven't worked with either the NDK or OpenCV (for android) before. My assumption was that the NDK was required (at least for some of the more advanced features). I have downloaded and unzipped the two folders required for installing the NDK; how do I go about installing this into a project? Also, what changes in the build.gradle are needed for OpenCV after I copy over `/sdk/native/libs/` as `jniLibs` – core16 Jun 20 '14 at 07:30
  • You usually use the ndk by calling `ndk-build` that is at the root of the ndk, from your application directory. It cross-compile your C/C++ sources from jni folder to native libraries (.so files) inside libs/ABI/. You don't "install" it but you can add its location to your environment path, and integrate it properly to your build.gradle using the snippet I've written above. But you need to use the NDK only if you're doing C/C++. Maybe the Java wrapper is enough, or you can also try javacv. .so files inside jniLibs are integrated without having to do any change to build.gradle. – ph0b Jun 20 '14 at 08:19
  • Okay, I have the environment path setup and I have copied over the OpenCV libs folder. Now could you please explain what the Android.mk file is and where I would create the jni folder. I read that the Android.mk file should include OpenCV.mk but I have not copied that over? – core16 Jun 23 '14 at 17:45