11

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.

Community
  • 1
  • 1
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
  • I managed to do this without compiling anything. Check out: http://stackoverflow.com/a/35135495/5611377, maybe it will help. – ssimm Mar 06 '16 at 19:27

1 Answers1

32

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.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • 1
    Does not work in a library project, you have to copy the .so files to your app project, and then this works perfectly. – Frank Jan 20 '14 at 16:14
  • Yes, this will be fixed in the next version of the plugin. – Xavier Ducrohet Jan 20 '14 at 16:55
  • Is the feature released to export binary "*.so" files from library project? – Hossain Khan Mar 07 '14 at 20:38
  • Yes aar should package their so files now. – Xavier Ducrohet Mar 08 '14 at 01:55
  • Thanks a lot. most answers on Stackoverflow are outdated and show old methods for that problem. What you need to do on Android Studio: Put the files into src/main/jniLibs/xxx/*.so, right click the folder in project structure and "synchronize" – John Mar 21 '14 at 22:48
  • 1
    How can I do this with gradle 0.9 if the *.so files are in a library project? – Fernando Gallego May 07 '14 at 12:54
  • Please check my answer here. It was made for Android Studio v1.4 but I know someone who also got it to work with v1.5: http://stackoverflow.com/a/35135495/5611377 – ssimm Mar 06 '16 at 19:24