Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Compiling Native C/C++ library for Android

5.00/5 (1 vote)
20 Aug 2014CPOL2 min read 24.3K   217  
This article describes the method to cross compile C/C++ library for Android OS

Introduction

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 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.

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 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 

  • 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.

MakeFiles

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

Android.mk 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  
OPENCV_LIBS3:=$(OPENCV_MODULES3)

# 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
include $(BUILD_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

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


define add_opencv_module1	
include $(CLEAR_VARS)
	LOCAL_PATH := libs/armeabi2
	LOCAL_MODULE:=aaaopencv_$1
	LOCAL_SRC_FILES:=libopencv_$1.$(OPENCV_LIB_SUFFIX)
	include $(PREBUILT_$(OPENCV_LIB_TYPE)_LIBRARY)	
endef

$(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

Files

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)