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
- Android SDK
- Eclipse -> Window -> Preferences -> Android -> set path to SDK
- Android NDK
- Eclipse -> Window -> Preferences -> Android -> NDK -> set path to the NDK
- ADT and NDK plugin for Eclipse
- Eclipse CDT for C/C++ support
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
The library libOpenVision.so 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.
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 Android.mk make file
Copy the all the C/C++ project files in the jni folder and then proceed to modify the Android.mk 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
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 Android.mk 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
LOCAL_PATH := $(call my-dir)
# 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 Application.mk file
OPENCV_MODULES2:= calib3d contrib core features2d flann highgui imgproc legacy ml nonfree objdetect photo stitching video videostab
LOCAL_PATH := libs/armeabi2
$(foreach module,$(OPENCV_LIBS2),$(eval $(call add_opencv_module1,$(module))))
Application.mk 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 libOpenVision.so 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 www.github.com/pi19404/OpenCVAndroid
The source and make files used above came be found in the OpenVision repository at www.github.com/pi19404/OpenVision
The Android.mk and Application.mk files and contents of jni directory can be found below