Android Studio, Gradle, OpenCV and NDK - android

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.
How do I get Android Studio to read the NDK variable ?
What exactly are the jniFolders ?
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.

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.

Related

Add pre-built NDK library (.so file) to Android project

I inherited an Android project which includes a sub-project to build a JNI library from C source. I want to remove the C source and include pre-built library instead. Based on tips I found here and there, I copied the .so files from the .apk to paths like:
app/src/main/jniLibs/armeabi-v7a/libxcore.so
But, when I tried to build the app I get "cannot find symbol" errors for classes defined in the library. I'm guessing I need to add something to build.gradle that points to the .so files, but am very new to Java/Android/Gradle and can't figure out what. Tried a variety of things suggested here, with no luck. Thanks for any help.
The error had nothing to do with the location of the .so files, but with the fact that the library sub-project I deleted also contained some Java sources. Once I added back the original library project, and deleted only the C sources, everything worked fine.

Intellij Idea not supporting cpp files

I am trying to create an NDK application to test. I have already downloaded NDK and included in the application but there is no option to create a new C++ file, even the headed files like jni.h are not found. I have also added an image of Intellij Idea.
I have also tried to add NDK path in the local.properties file but it did not work.
Please help.

How to integrate CLSTM library in Android application?

I'm working on a project that requires me to integrate CLSTM library (a C++ implementation of LSTM networks for OCR) in an Android application. I'm a beginner Android developer and this is my first time working with NDK.
I'm having trouble integrating the C++ code with my project. I have been trying to do it for a couple of days, but have still gotten nowhere. I have been looking for solutions everywhere, but I can't find exact instructions for my problem.
What I have been able to do till now is:
Created new project with C++ support
Downloaded Android NDK
Downloaded CMake
Created JNI Folder
Cloned the source code into JNI Folder
I am not sure if I was supposed to do steps 4, 5 above. Also, from all the tutorials I have found, I think after these I am supposed to create an Android.mk and Application.mk files. I'm not sure what to write in them.
Can anyone guide me with exact steps for how to integrate this particular library in my Android application?
It's not really clear what exact problem you are having but I'll give it a shot as I've been dabbling with this myself recently. These are the steps I took:
Using classpath 'com.android.tools.build:gradle:2.2.0 as my build tools version
Added the C libraries in my app/src/main/jni directory
Added an Android.mk and Application.mk file in my app/src/main/jni directory
Added the following to my app/build.gradle:
```
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
```
You can find the contents of my Application.mk and Android.mk here.
I based my setup of the .mk files & contents upon the keepassdroid project.
Edit:
if you also want to use travis-ci you can use this to download and use the android ndk in your builds:
before_install:
- curl -L http://dl.google.com/android/repository/android-ndk-r14-linux-x86_64.zip -O
- unzip -q android-ndk-r14-linux-x86_64.zip
- export ANDROID_NDK_HOME=`pwd`/android-ndk-r14
- export PATH=$PATH:${ANDROID_NDK_HOME}

How do you link third party library in Android cmake external build system?

Android Studio 2.2 introduces cmake external build system. The problem is that documentation is really lacking and I do not know how should I link third party libraries? I've tried cmake directive target_link_libraries:
target_link_libraries(native-lib libs/libSomething.so)
And it "works" in that app compiles but then I get dlopen error at runtime because libSomething.so has not been packaged with application. The libs directory is under "app" if that changes anything and I've started with default JNI project generated by Android Studio 2.2...
[Update]
I've tried putting libSomething.so under app/src/main/jniLibs/armeabi-v7a but now the "main" (native-lib) library is not packaged.
[Update2]
I've added source set config that includes cmake output dir and this works but is ugly as hell and is not really a permanent solution...
sourceSet
{
main
{
jniLibs.srcDirs = [ "libs", ".externalNativeBuild/cmake/debug/obj"]
}
}
For now I ended up copying libSomething.so to cmake library output directory in a post build step. This works because it turns out that Android Studio copies into apk EVERYTHING that is in that directory.
Command in cmake is the following:
add_custom_command(TARGET native-lib POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libSomething.so
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libSomething.so
)
For now, you could also put your shared libs into directory, and configure jniLibs to point to that directory, that will pack it. One sample is here: https://github.com/googlesamples/android-ndk/tree/master/hello-libs, follow gperf see if that helps. This way app not depending on the generated build folders. When android studio added packing libs, the jniLibs workaround is not necessary anymore

Get rid of ndk build from a project

I have added ndk support in my project because I needed to convert from one pixel format to another and I thought it would be faster in native code than in java. Now I realized I don't actually need to convert anything so I no longer need the ndk-build in my project. How do I get rid of android building the *.so files that I no longer use?
in Eclipse follow:
Project -> Properties -> Builders
and disable CDT
Remove complete jni folder.
Also from java remove all the native methods.Remove the libraries that are present under libs/armeabi
Also remove obj/local/armeabi

Categories

Resources