Compiling Native C/C++ library for Android


This article describes method to compile C/C++ library for mobile devices which use Android OS.

Installation and Code Compilation

Before Proceeding make sure that you have all the below software components installed and configured in Eclipse

  • Eclipse IDE

Develope the code on Desktop Computer and check if you are able to compile it properly without errors.

The present example consists of files containing following classes

  • UniStrokeGestureRecognizer
  • UniStrokeGesture
  • GesturePoint

The library has been successfully compiled on the Ubuntu OS and now we proceed with cross compilation of the library for ARM based mobile devices which use the Android OS.

Cross Compilation

The simplest approach to do this is to use the Eclipse IDE.The Eclipse IDE provides features for adding native C/C++ support to an existing Android based project.

The  project name is AndroidGesture.Right click on an Android project and

select Android Tools -> Add native support.

And enter the desired library name as OpenVision

This will configure the AndroidProject for the native build.Create a jni folder with OpenVision.cpp file and associated make file

Copy the all the C/C++ project files in the jni folder and then proceed to modify the file to configure for native build.

Create a directory called OpenVision in the jni directory

Copy all of the following files in the ImageApp subdirectory 

  • UniStrokeGestureRecognizer.cpp
  • UniStrokeGestureRecognizer.hpp
  • UniStrokeGesture.cpp
  • UniStrokeGesture.hpp
  • GesturePoint.cpp
  • GesturePoint.hpp

Copy the file OpenCVCommon.hpp in the Common Subdirectory

The preset code uses OpenCV libraries.Copy the attached OpenCV pre-compiled libraries for ARM in the libs/armeabi and libs/armeabi2 directories.


below are the contents of file.This file is like a standard make file containing the include paths,source files,library dependencies etc.Few of the syntaxes are specific to android build and explaination is provided in the comments file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

# name of the library to be built
LOCAL_MODULE    := OpenVision
#list of source files to be build as part of the library
LOCAL_SRC_FILES := ImgApp/GesturePoint.cpp ImgApp/UniStrokeGesture.cpp ImgApp/UniStrokeGestureRecognizer.cpp

# list of dependent 3rd party or external libraries are included in the LOCAL_SHARED_LIBRARY variable
LOCAL_SHARED_LIBRARIES := $(foreach module,$(OPENCV_LIBS3),opencv_$(module)) 
OPENCV_MODULES3:=core imgproc flann contrib features2d video  highgui legacy ml objdetect  

# list of dependent system libraries
LOCAL_LDLIBS +=  -fPIC -llog -ldl -lm  -lz -lm -lc -lgcc -Wl,-rpath,'libs/armeabi-v7a' 
LOCAL_LDLIBS += -L$(LOCAL_PATH)/../libs/armeabi -llog -Llibs/armeabi-v7a/ 

# include path for header files for C and C++ applications
LOCAL_C_INCLUDES +=/usr/local/include /usr/local/include/opencv2 /home/pi19404/repository/OpenVision/OpenVision
LOCAL_CPP_INCLUDES +=/usr/local/include /usr/local/include/opencv2 /home/pi19404/repository/OpenVision/OpenVision
#The compilation flags for C/C++ applications
LOCAL_CPPFLAGS += -DHAVE_NEON -fPIC -DANDROID -I/usr/local/include/opencv  -I/usr/local/include   -I/OpenVision -I/home/pi19404/repository/OpenVision/OpenVision -fPIC
LOCAL_CFLAGS += -DHAVE_NEON -fPIC -DANDROID  -I/usr/local/include/opencv -I/usr/local/include -I/OpenVision -I/home/pi19404/repository/OpenVision/OpenVision -fPIC
LOCAL_CPP_FEATURES += exceptions

#statement specifies build of a shared library

#files in the libs/armeabi are deleted during each build
#we need to have 3rd party opencv libraries in this directory
#the files are placed in the armeabi2 directory
#when ever a native build is trigged the opencv library files specified in the OPENCV_MODULES2
#variable are copied from the armeabi2 directory to the armeabi or armeabi-v7a directory
#as per the specification of APP_ABI in the file

include $(CLEAR_VARS)
OPENCV_MODULES2:= calib3d contrib  core features2d flann highgui imgproc  legacy ml nonfree objdetect photo stitching  video videostab

define add_opencv_module1	
include $(CLEAR_VARS)
	LOCAL_PATH := libs/armeabi2

$(foreach module,$(OPENCV_LIBS2),$(eval $(call add_opencv_module1,$(module)))) make file

APP_ABI :=   armeabi-v7a armeabi
APP_STL := gnustl_static
APP_PLATFORM    := android-8
APP_CPPFLAGS := -frtti -fexceptions -ftree-vectorize  -mfpu=neon -O3 -mfloat-abi=softfp -ffast-math

After building the project the files can be found in the libs/armeabi and libs/armeabi-v7a directories.These have been cross-compiled for use on android based devices.

These can now be loaded and called from java application using JNI Interface


The pre compiled opencv libraries for Android can be found at

The source and make files used above came be found in the OpenVision repository at

The and files and contents of jni directory can be found below


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s