If you want to use FreeType in your native C/C++ Android app, you'll need to cross-compile it. The Android system uses FreeType internally but it doesn't expose it to native apps.

First, prepare the cross-compiler from the NDK:

/usr/src/android-ndk-r7/build/tools/make-standalone-toolchain.sh \
  --platform=android-9 \
  --install-dir=/usr/src/ndk-standalone-9
PATH=/usr/src/ndk-standalone-9/bin:$PATH

Then use it to cross-compile freetype:

tar xf freetype-2.4.8.tar.bz2
cd freetype-2.4.8/
CFLAGS="-std=gnu99" ./configure --host=arm-linux-androideabi --prefix=/freetype \
  --without-zlib
make
make install DESTDIR=$(pwd)

See this discussion for the --std option.

--without-zlib uses the internal copy of zlib inside freetype rather than one independently recompiled for Android. Otherwise you'd get errors such as:

./obj/local/armeabi/libfreetype.a(ftgzip.o): In function `ft_gzip_file_init':
ftgzip.c:(.text+0x3c4): undefined reference to `inflateInit2_'
./obj/local/armeabi/libfreetype.a(ftgzip.o): In function `ft_gzip_file_done':
ftgzip.c:(.text+0x43c): undefined reference to `inflateEnd'
./obj/local/armeabi/libfreetype.a(ftgzip.o): In function `ft_gzip_file_reset':
ftgzip.c:(.text+0x514): undefined reference to `inflateReset'
./obj/local/armeabi/libfreetype.a(ftgzip.o): In function `ft_gzip_file_fill_output':
ftgzip.c:(.text+0x780): undefined reference to `inflate'

Then write an Android.mk file in the new freetype/ directory:

#####
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := freetype
LOCAL_SRC_FILES := lib/libfreetype.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/include/freetype2
include $(PREBUILT_STATIC_LIBRARY)
#####

See docs/STANDALONE-TOOLCHAIN.html and docs/PREBUILTS.html in the NDK for details. The CLEAR_VARS bit is not documented, but is necessary to avoid mixed-up paths; it is used in the native_app_glue NDK module.

To use FreeType in your project, edit your Android.mk:

...
LOCAL_STATIC_LIBRARIES := ... freetype
...
$(call import-module,freetype)

You will need to specify the freetype directory location as well, using the NDK_MODULE_PATH variable. For instance, you can copy freetype in your project directory and build using:

    ndk-build NDK_MODULE_PATH=. ...

[Latest version of these instructions can be found at the OpenGL Wikibook]

At http://wiki.gnashdev.org/AndroidDependencies#freetype the Gnash project suggests CFLAGS="-mandroid" to compile FreeType. AFAICS this doesn't fix compilation errors :

In file included from /usr/src/ndk-standalone-9/bin/../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:64,
                 from /usr/src/freetype-2.4.8/include/freetype/config/ftstdlib.h:101,
                 from ./builds/unix/ftconfig.h:43,
                 from /usr/src/freetype-2.4.8/src/base/ftsystem.c:29:
/usr/src/ndk-standalone-9/bin/../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/sys/types.h:137: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'u_int64_t'
make: *** [/usr/src/freetype-2.4.8/objs/ftsystem.lo] Erreur 1

You still need CFLAGS="-std=gnu99".

Comment by beuc Sun Feb 5 00:19:01 2012
Comment by beuc Sat Mar 3 13:58:59 2012