Compiling Native C/C++ library for Android

download Compiling Native C/C++ library for Android

of 3

description

This article describes the method to cross compile C/C++ library for Android OS

Transcript of Compiling Native C/C++ library for Android

  • 5/21/2018 Compiling Native C/C++ library for Android

    1/3

    Please choose 'View Source' in your browser to view the HTML, or File | Save to save this file toyour hard drive for editing.

    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 inEclipse

    Eclipse IDE

    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

    Install Android SDK + NDK Plugins from Eclipse update site https://dl-ssl.google.com/android/eclipse/

    Eclipse CDT for C/C++ support

    Install CDT from Eclipse update site http://download.eclipse.org/tools/cdt/releases/indigo.

    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

    UniStrokeGestureRecognizerUniStrokeGestureGesturePoint

    The library libOpenVision.so has been successfully compiled on the Ubuntu OS and now we proceed withcross 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 addingnative 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 toconfigure for native build.

    http://download.eclipse.org/tools/cdt/releases/indigohttps://dl-ssl.google.com/android/eclipse/http://download.eclipse.org/tools/cdt/releases/indigohttps://dl-ssl.google.com/android/eclipse/
  • 5/21/2018 Compiling Native C/C++ library for Android

    2/3

    Create a directory called OpenVision in the jni directory

    Copy all of the following files in the ImageApp subdirectory

    UniStrokeGestureRecognizer.cppUniStrokeGestureRecognizer.hppUniStrokeGesture.cppUniStrokeGesture.hppGesturePoint.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 thelibs/armeabi and libs/armeabi2 directories.

    MakeFiles

    below are the contents of Android.mk file.This file is like a standard make file containing the includepaths,source files,library dependencies etc.Few of the syntaxes are specific to android build andexplaination 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/UniStrokeGestureR

    # list of dependent 3rd party or external libraries are included in the LOCAL_SHARED_LIBRARY va

    LOCAL_SHARED_LIBRARIES := $(foreach module,$(OPENCV_LIBS3),opencv_$(module))

    OPENCV_MODULES3:=core imgproc flann contrib features2d video highgui legacy ml objdetectOPENCV_LIBS3:=$(OPENCV_MODULES3)

    # list of dependent system librariesLOCAL_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/OpenV

    LOCAL_CPP_INCLUDES +=/usr/local/include /usr/local/include/opencv2 /home/pi19404/repository/Ope

    #The compilation flags for C/C++ applicationsLOCAL_CPPFLAGS += -DHAVE_NEON -fPIC -DANDROID -I/usr/local/include/opencv -I/usr/local/include

    LOCAL_CFLAGS += -DHAVE_NEON -fPIC -DANDROID -I/usr/local/include/opencv -I/usr/local/include -

    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 objOPENCV_LIBS2:=$(OPENCV_MODULES2)

    OPENCV_LIB_SUFFIX:=so

    OPENCV_LIB_TYPE:=SHARED

  • 5/21/2018 Compiling Native C/C++ library for Android

    3/3

    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-

    After building the project the libOpenVision.so files can be found in the libs/armeabi and libs/armeabi-v7adirectories.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 atwww.github.com/pi19404/OpenVision

    The Android.mk and Application.mk files and contents of jni directory can be found below

    Download Source - 5.6 KB

    http://www.codeproject.com/KB/recipes/809257/source_jni.ziphttp://www.github.com/pi19404/OpenVisionhttp://www.github.com/pi19404/OpenCVAndroid