Objectives : compile a general-use C/C++ library under Linux, targeting Android.
It has been asked many times, how to cross-compile a general-use library (Libgee, SQLite, etc...) from Linux to Android.
We will here take Libgee as an example, as it has few dependencies and no changes to its code are needed. However, remember that things are not always so simple ;-)
Libgee requires both a toolchain and GLib. We could use the relatively recent Android NDK r7b, but we will install the old NDK r4c from Mozilla instead. Why ? Because GLib is hard to compile, so we won't do it ourselves, but use mine, and I compiled it with NDK r4c ;-) ; hence the need to use the same toolchain to retain ABI compatibility.
- Download the Android NDK r4c (Mozilla) for Linux :
android-ndk-r4c-0moz3.tar.bz2 (119 Mb)
(and extract it to a new "/opt/android" folder :
mv android-ndk-r4c-0moz3.tar.bz2 /opt
tar xfvj android-ndk-r4c-0moz3.tar.bz2)
(et install it to "/opt", making it executable :
mv agcc.pl-r4c0moz3 /opt
chmod a+x /opt/agcc.pl-r4c0moz3
ln -s /opt/agcc.pl-r4c0moz3 /usr/bin/agcc)
GLib is Libgee's sole hard dependency ; we use a precompiled version here, because it's easier. Depending of what we try to build, we may need to install or compile more stuff ourselves.
- Download GLib 2.28.1 for Android :
glib-2.28.1-android_(TARNYKO).tar.bz2 (1.81 Mb)
(and extract it to "/opt/android" :
mv glib-2.28.1-android_\(TARNYKO\).tar.bz2 /opt/android
tar xfvj glib-2.28.1-android_\(TARNYKO\).tar.bz2)
- We will add the Android NDK to the PATH (so agcc will find it) :
- We will tell "pkg-config" to locate Android libraries in our custom path (so it will find GLib) :
- Finally, we will define some environment variables used by the "./configure" script to generate the Makefile :
Extract the LibGee source tarball, cd to its directory, and run the (fast !) usual :
./configure --prefix=/opt/android --host=arm-eabi-linux --enable-shared --enable-static
Here we go !
Here, everything went well because -by chance- Libgee uses only compatible system calls. That means, functions that are present in both GNU/Linux and Android.
However, remember that Android uses its own C library -called Bionic- which doesn't implement all of Linux' GLibc. In other words, for some libraries it won't work, and you will need your C developer skills to patch the source code... ;-)