Building libjpeg-turbo for Android
In order to build libjpeg-turbo for Android, here are the tools you’ll need:
- Linux (Ubuntu Linux works fine)
- Android NDK
- libjpeg-turbo source code
The compilation process won’t work with Cygwin since the Android NDK doesn’t officially support it so the make will fail. No worries, you can easily work with a Linux distribution. If you don’t have it already installed in your system, you can setup a VMWare virtual machine with Ubuntu and everything will work just fine: download the free VMWare player and get an Ubuntu ISO to create the virtual machine.
Before starting the build process, we must have all the needed tools ready. In this tutorial I’m going to work with the following environment:
- Windows 8 Pro 64bit
- VMWare Player 5.0.1 with an Ubuntu Desktop 12.10 64bit virtual machine
- Android NDK Revision 8e (for Linux 64bit)
- libjpeg-turbo 1.2.90
Setup the Android NDK and create a standalone toolchain
Download the Android NDK from http://developer.android.com/tools/sdk/ndk/index.html. Make sure you get the one for the Linux 64-bit (x86) platform (or the 32bit version if you’re using a 32bit Linux distribution).
Place the downloaded file in your home directory of the Linux distribution (in this tutorial my home directory will be /home/andrea), open a Terminal window and from your home directory extract the file content typing
tar -xf android-ndk-r8e-linux-x86_64.tar.bz2
This will create the android-ndk-r8e folder with the whole NDK. NOTE: it’s important that you use the tar command from the commandline and don’t extract the compressed archive through the Linux GUI as it might mess up with the files permissions causing you some troubles later.
Now it’s time to create a standalone toolchain from the NDK so we’ll be able to use it later to compile libjpeg-turbo. If you need information about the standalone toolchains, you can read the related document at android-ndk-r8e/docs/STANDALONE-TOOLCHAIN.html.
From a Terminal window, in your home directory, type the following commands:
cd android-ndk-r8e mkdir standalone-toolchains cd build/tools ./make-standalone-toolchain.sh --ndk-dir=/home/andrea/android-ndk-r8e --platform=android-9 --arch=arm --toolchain=arm-linux-androideabi-4.7 --install-dir=/home/andrea/android-ndk-r8e/standalone-toolchains/android-9-arm-4.7 --system=linux-x86_64
Now your standalone toolchain has been created in /home/andrea/android-ndk-r8e/standalone-toolchains/android-9-arm-4.7.
First of all, we need a copy of libjpeg-turbo. The official website of the project is http://www.libjpeg-turbo.org and we can get our copy from the SourceForge project page (download the tar.gz archive with the source code). Here I’m going to use version 1.2.90.
From the usual Terminal window in Linux we are going to unpack the archive so you need to put the tar.gz archive in your home directory and type
tar -xf libjpeg-turbo-1.2.90.tar.gz
This will create the libjpeg-turbo-1.2.90 folder in your home directory. Before starting the build process, you need to fix the library package by downloading the latest version of the configure files that you can find at http://git.savannah.gnu.org/gitweb/?p=config.git;a=tree. You need both config.guess and config.sub. These files need to be updated because otherwise androideabi will not be recognized during our configuration process before building the library and we need it to compile everything for Android. So, replace config.guess and config.sub in the root directory of your libjpeg-turbo package with those you just downloaded.
Now we’re ready to start the compilation process. Let’s create a destination folder for the build. From your home directory in Linux, type the following:
cd libjpeg-turbo-1.2.90 mkdir build cd build mkdir arm_neon cd arm_neon
The destination folder of the build will be /home/andrea/libjpeg-turbo-1.2.90/build/arm_neon. In that folder, create a file named configure_build.sh with the following content:
#!/bin/sh export TOOLCHAIN=/home/andrea/android-ndk-r8e/standalone-toolchains/android-9-arm-4.7 export CROSS_COMPILE=arm-linux-androideabi export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/$CROSS_COMPILE/bin:$PATH export SYSROOT=$TOOLCHAIN/sysroot export CC=$CROSS_COMPILE-gcc export CXX=$CROSS_COMPILE-g++ export CPP=$CROSS_COMPILE-cpp export CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -O3" export LDFLAGS="-Wl,--fix-cortex-a8" export LIBJPEG_TURBO_BASE_DIR=/home/andrea/libjpeg-turbo-1.2.90 echo "Configuring..." sh $LIBJPEG_TURBO_BASE_DIR/configure --host=arm-linux-androideabi --prefix=$SYSROOT/usr
Change the permissions of the file (to make it executable) and launch it
chmod 777 configure_build.sh ./configure_build.sh
Create another file named make_build.sh with the following content:
#!/bin/sh export TOOLCHAIN=/home/andrea/android-ndk-r8e/standalone-toolchains/android-9-arm-4.7 export CROSS_COMPILE=arm-linux-androideabi export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/$CROSS_COMPILE/bin:$PATH echo "Making libjpeg-turbo for Android" make
Change the permissions of the file and launch it
chmod 777 make_build.sh ./make_build.sh
Create another file named install_build.sh with the following content:
#!/bin/sh export TOOLCHAIN=/home/andrea/android-ndk-r8e/standalone-toolchains/android-9-arm-4.7 export CROSS_COMPILE=arm-linux-androideabi export PATH=$TOOLCHAIN/bin:$TOOLCHAIN/$CROSS_COMPILE/bin:$PATH echo "Installing libjpeg-turbo for Android" make install prefix=/home/andrea/libjpeg-turbo-1.2.90/build/arm_neon/installdir libdir=/home/andrea/libjpeg-turbo-1.2.90/build/arm_neon/libs
Change the permissions of the file and launch it
chmod 777 install_build.sh ./install_build.sh
Inside /home/andrea/libjpeg-turbo-1.2.90/build/arm_neon/libs you finally have your compiled libjpeg.a!
To find out how you could use it with OpenCV, check my other tutorial Building OpenCV for Android with libjpeg-turbo.