You are on page 1of 4

I follow the steps below mentioned in the book Mastering opencv android application

and was able to use SIFT and SUFT methods:

- Download Android ndk and extract it to a directory in your computer:
Link download:

- Download nonfree_init.cpp, precomp.cpp, sift.cpp, and surf.cpp files github

Link download:
Get file, extract it and these files should be under \
modules\nonfree\src folder.

- In Android studio, create a directory called jni and copy all these to that directory

- Open precomp.hpp and remove the lines #include "cvconfig.h" and #include

- Open nonfree_init.cpp and remove the lines of code starting from #ifdef
HAVE_OPENCV_OCL and ending at #endif.

- Under the jni folder, create a file called and paste these lines into it:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)


include C:/OpenCV-android-sdk-2.4.13/sdk/native/jni/ <-Note:

Remember to change this line to your corresponding directory

LOCAL_MODULE := nonfree
LOCAL_SRC_FILES := nonfree_init.cpp \
sift.cpp \
LOCAL_LDLIBS += -llog -ldl


- Under jni folder, create another file called and paste these lines
into it

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a mips x86 <- Note: I had problem running armeabi abi,
if you dont have that problem, simply add armeabi to this line. They are
separated by a space.
APP_PLATFORM := android-22
Here is a screen shot of this folder:

- Next step, Open the build.gradle fle in your app folder. Under the android section,
add the following:

sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
- Open terminal or command prompt, point to your project jni folder of your project
cd <project_directory>/app/src/main/jni

- Mine is:

- But yours would be different. And then run: <ndk_dir>/ndk-build

<ndk_dir> is where you extract your Android ndk files.

- After building successfully, your project would look like this:

- Now last step, in your java code, add this line System.loadLibrary("nonfree")
under event LoaderCallbackInterface.SUCCESS your code might look like this

public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Now we can use those nonfree algorithms in OpenCv. There is also a video on YouTube
showing how to handle this task. In this video, instead of building the .so files, he copies
those .so from internet and pastes directly to the project. I have not tested it but it
seems working and a bit faster:
Here is the link: