Introduction
FreeType is a font service middleware that is written in industry-standard ANSI C. It comes with the build system that is based on GNU Make. The native development kit (NDK) is a toolset that allows you to implement C and C++ in Android apps, auto-generate project and build files, build native libraries, copy the libraries into appropriate folders, and more. For more information on the NDK, see http://developer.android.com/tools/sdk/ndk/index.html. To download and install the latest NDK build, go to http://developer.android.com/ndk/downloads/index.html.
There are many ways to build middleware libraries. You can compile middleware using an automatic NDK build system. Alternatively, you can cross-compile middleware from Cygwin* using the standalone toolchain option. For the list of some Android* middleware libraries that support x86, visit https://software.intel.com/en-us/blogs/2015/06/26/building-android-middleware-libraries-for-x86-devices-using-the-android-ndk. In this document, we’ll describe how to configure Eclipse* to set up an automatic NDK build for FreeType.
Creating a New Android* Application Project in Eclipse
For detailed instruction of how to create a new Android application project in Eclipse*, see http://developer.android.com/tools/projects/projects-eclipse.html. After you have created a new Android* application project, create a “jni” folder inside the Android project as shown below:
Figure 1: Create a jni folder
Downloading the FreeType Library
To download the latest FreeType middleware, browse to http://sourceforge.net/projects/freetype/files/freetype2. As of this writing, the latest FreeType version is 2.6. Change to the jni directory, and untar the freetype-2.6.tar.gz using a cgwin or Linux* console.
Figure 2: FreeType source files under the jni folder
Adding Android.mk and Application.mk into the Android* Project
Android.mk and Application.mk are located in the subdirectory jni of the Android* application project. Android.mk is a GNU makefile that the build system parses. Android.mk specifies source files and shared libraries to the build system. For a description of the syntax of the Android.mk build file for the C and C++ source files to link to the Android* NDK, go to https://developer.android.com/ndk/guides/android_mk.html. Create the native code makefile Android.mk in the subdirectory jni for all FreeType fonts, which looks like this:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) FREETYPE_SRC_PATH := LOCAL_MODULE := freetype LOCAL_CFLAGS := -DANDROID_NDK \ -DFT2_BUILD_LIBRARY=1 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include_all \ $(FREETYPE_SRC_PATH)include \ $(FREETYPE_SRC_PATH)src LOCAL_SRC_FILES := \ $(FREETYPE_SRC_PATH)src/autofit/autofit.c \ $(FREETYPE_SRC_PATH)src/base/basepic.c \ $(FREETYPE_SRC_PATH)src/base/ftapi.c \ $(FREETYPE_SRC_PATH)src/base/ftbase.c \ $(FREETYPE_SRC_PATH)src/base/ftbbox.c \ $(FREETYPE_SRC_PATH)src/base/ftbitmap.c \ $(FREETYPE_SRC_PATH)src/base/ftdbgmem.c \ $(FREETYPE_SRC_PATH)src/base/ftdebug.c \ $(FREETYPE_SRC_PATH)src/base/ftglyph.c \ $(FREETYPE_SRC_PATH)src/base/ftinit.c \ $(FREETYPE_SRC_PATH)src/base/ftpic.c \ $(FREETYPE_SRC_PATH)src/base/ftstroke.c \ $(FREETYPE_SRC_PATH)src/base/ftsynth.c \ $(FREETYPE_SRC_PATH)src/base/ftsystem.c \ $(FREETYPE_SRC_PATH)src/cff/cff.c \ $(FREETYPE_SRC_PATH)src/pshinter/pshinter.c \ $(FREETYPE_SRC_PATH)src/pshinter/pshglob.c \ $(FREETYPE_SRC_PATH)src/pshinter/pshpic.c \ $(FREETYPE_SRC_PATH)src/pshinter/pshrec.c \ $(FREETYPE_SRC_PATH)src/psnames/psnames.c \ $(FREETYPE_SRC_PATH)src/psnames/pspic.c \ $(FREETYPE_SRC_PATH)src/raster/raster.c \ $(FREETYPE_SRC_PATH)src/raster/rastpic.c \ $(FREETYPE_SRC_PATH)src/sfnt/pngshim.c \ $(FREETYPE_SRC_PATH)src/sfnt/sfntpic.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttbdf.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttkern.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttload.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttmtx.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttpost.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttsbit.c \ $(FREETYPE_SRC_PATH)src/sfnt/sfobjs.c \ $(FREETYPE_SRC_PATH)src/sfnt/ttcmap.c \ $(FREETYPE_SRC_PATH)src/sfnt/sfdriver.c \ $(FREETYPE_SRC_PATH)src/smooth/smooth.c \ $(FREETYPE_SRC_PATH)src/smooth/ftspic.c \ $(FREETYPE_SRC_PATH)src/truetype/truetype.c \ $(FREETYPE_SRC_PATH)src/type1/t1driver.c \ $(FREETYPE_SRC_PATH)src/cid/cidgload.c \ $(FREETYPE_SRC_PATH)src/cid/cidload.c \ $(FREETYPE_SRC_PATH)src/cid/cidobjs.c \ $(FREETYPE_SRC_PATH)src/cid/cidparse.c \ $(FREETYPE_SRC_PATH)src/cid/cidriver.c \ $(FREETYPE_SRC_PATH)src/pfr/pfr.c \ $(FREETYPE_SRC_PATH)src/pfr/pfrgload.c \ $(FREETYPE_SRC_PATH)src/pfr/pfrload.c \ $(FREETYPE_SRC_PATH)src/pfr/pfrobjs.c \ $(FREETYPE_SRC_PATH)src/pfr/pfrsbit.c \ $(FREETYPE_SRC_PATH)src/type42/t42objs.c \ $(FREETYPE_SRC_PATH)src/type42/t42parse.c \ $(FREETYPE_SRC_PATH)src/type42/type42.c \ $(FREETYPE_SRC_PATH)src/winfonts/winfnt.c \ $(FREETYPE_SRC_PATH)src/pcf/pcfread.c \ $(FREETYPE_SRC_PATH)src/pcf/pcfutil.c \ $(FREETYPE_SRC_PATH)src/pcf/pcfdrivr.c \ $(FREETYPE_SRC_PATH)src/psaux/afmparse.c \ $(FREETYPE_SRC_PATH)src/psaux/psaux.c \ $(FREETYPE_SRC_PATH)src/psaux/psconv.c \ $(FREETYPE_SRC_PATH)src/psaux/psobjs.c \ $(FREETYPE_SRC_PATH)src/psaux/t1decode.c \ $(FREETYPE_SRC_PATH)src/tools/apinames.c \ $(FREETYPE_SRC_PATH)src/type1/t1afm.c \ $(FREETYPE_SRC_PATH)src/type1/t1gload.c \ $(FREETYPE_SRC_PATH)src/type1/t1load.c \ $(FREETYPE_SRC_PATH)src/type1/t1objs.c \ $(FREETYPE_SRC_PATH)src/type1/t1parse.c\ $(FREETYPE_SRC_PATH)src/bdf/bdfdrivr.c\ $(FREETYPE_SRC_PATH)src/bdf/bdflib.c\ $(FREETYPE_SRC_PATH)src/gzip/ftgzip.c\ $(FREETYPE_SRC_PATH)src/lzw/ftlzw.c \ LOCAL_LDLIBS := -ldl -llog include $(BUILD_SHARED_LIBRARY)
Code Sample 1: Android.mk
The NDK build system generates machine code for armeabi by default. To add support to the Intel® architecture, simply add x86 to the APP_ABI variable in jni/Application.mk. You can specify multiple architectures on the same line using space delimited.
APP_ABI := armeabi-v7a x86 #can also be x86_64, armeabi, arm64-v8a, mips, mips64
To generate machine code for all supported instruction sets:
APP_ABI := all #can also be all32 or all64
APP_PLATFORM specifies the name of the target Android* platform. Create the simple native modules build file Application.mk in the subdirectory jni as below:
APP_ABI :=all APP_PLATFORM := android-20
Code Sample 2: Application.mk
Figure 3: Setting APP_ABI in Application.mk
Setting Up Automatic NDK Builds
The first step is to configure the Builders for the project, then select which Builders to enable for the project.
- Right-click your Android* project, and then choose properties.
- To add the new builder to the list, select Builders on the left side, and then click the New button.
Figure 4: Project properties
- From the list of configuration types, select Program. The program option allows you to define an external tool location and how to execute the script.
Figure 5: Configuration type
- Name: Type the name of the new builder.
- Location: Location of the ndk-build.cmd.
- Working Directory: Browse to the working project.
Figure 6: NDK configuration
Building the Project
After the project successfully built, all the libfreetype.so files for all the supported architectures are placed under /lib<APP_ABI>. The <APP_ABI> is x86, x86_64, armeabi, arm64-v8a, armeabi-v7a, mips, or mips64, depending how you set it in APP_ABI in Application.mk.
Figure 7: libfreetype.so for x86 devices
Summary
We have described how to set APP_ABI in Application.mk and generate libfreetype.so libraries for x86 devices using an automatic NDK build system.
References
- FreeType: http://www.freetype.org/freetype2/docs/index.html
- Android* application in Eclipse*: http://developer.android.com/tools/projects/projects-eclipse.html
- Android* NDK: http://developer.android.com/tools/sdk/ndk/index.html
- Android.mk: https://developer.android.com/ndk/guides/android_mk.html
- Application.mk: https://developer.android.com/ndk/guides/application_mk.html
- Building Android* middleware libraries for x86 devices: https://software.intel.com/en-us/blogs/2015/06/26/building-android-middleware-libraries-for-x86-devices-using-the-android-ndk
About the Author
Nancy Le is a software engineer at Intel® Corporation in the Software and Services Group working on Intel® Atom™ processor scale-enabling projects.