Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio, Gradle, OpenCV and NDK

I am trying to test OpenCV Android, on Android Studio, I am confused about how to include the NDK.

What I want to do is run the samples which come with OpenCV. Of the 6 samples provided I managed to run 4 successfully. The exceptions were face-detection and native-activity.

I suspect the reason is I have not set up my NDK correctly.

Googling I found a bunch of discussions but do not really understand them. This is my first time I am trying to work with both the NDK and OpenCV, and my Gradle understanding is limited.

I set an environment variable in my .bash_profile

export ANDROID_NDK=pathTo/android-ndk-r9

I do not understand how to get this to studio.

I see reference to jniFolder but do not understand what these are and should I care right now. Stackoverflow.com/questions/17767557

tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(new File(projectDir, 'native-libs'))

}

What am I supposed to do with this paste at the end of my build.gradle file ?

In summation, my questions are.

  1. How do I get Android Studio to read the NDK variable ?
  2. What exactly are the jniFolders ?
  3. Is it enough just to paste at the end of my build.gradle file ?

Google Group Discussions on Gradle and NDK


For anyone coming across this this is how I resolved it apart from Xaviers anwser. First I read OVERVIEW.html which comes with the NDK, in the docs directory. I then compiled the .mk and .cpp files into an .so file. I did this inplace in the sample jni directory This created the .so file in the libs folder which I copied to the destination as given by Xavier.

like image 961
Ryan Heitner Avatar asked Jan 04 '14 13:01

Ryan Heitner


1 Answers

If you have libraries that you build with the ndk and want to put them in a gradle-enabled android project (using version 0.7.+ of the plugin), you can just put them in

src/main/jniLibs/<abi>/libfoo.so

for example:

src/main/jniLibs/armeabi-v7a/libfoo.so
src/main/jniLibs/x86/libfoo.so

and they'll get packaged automatically.

If you want to keep them in the native-libs folder you can put the following in your gradle file:

android {
    sourceSets.main {
        jniLibs.srcDirs = ['native-libs']
    }
}

All this does really is tell gradle where the jniLibs folder for the main source set is (relative to the project root.)

The snippet you showed is doing something different. It's telling the package task to also include some native libraries. This was a hack that used to work in a previous version using undocumented API of the task that are no longer supported.

like image 78
Xavier Ducrohet Avatar answered Oct 06 '22 06:10

Xavier Ducrohet