Building Android on Arch Linux x86_64

Posted on Sa, 2012-10-13 in android

So it turns out building Android on Arch isn't quite that straight forward, but it is not impossible. There are some guides out there, but none of them is really complete. So here are my steps to get the building started.

First of all you need to enable multilib, if you haven't already [1]. Also you will have to get some stuff from AUR [2]. I use the yaourt wrapper to install AUR packages.

Obviously you should have the android-sdk stuff installed. Also take a look at the relevant page in the archlinux wiki [3].

yaourt -S android-sdk android-sdk-platform-tools android-udev repo

Get the following packages:

yaourt -S git gnupg flex bison gperf sdl wxgtk squashfs-tools zip curl ncurses zlib schedtool zip unzip

And some other packages from multilib:

yaourt -S lib32-zlib lib32-ncurses lib32-readline gcc-libs-multilib gcc-multilib lib32-gcc-libs

I found one guide [5], which suggests downgrading perl, git and make to match the versions suggested by Google/shipped with Ubuntu. This is not required. Latest git and make from arch repositories work just fine. But you have to install the perl-switch packages to get back the switch statement in perl, which is used in some scripts:

yaourt -S perl-switch

The next step is to get java. You can try to build android with openjdk6 or use the Oracle JDK. The openjdk6 package in arch repository unfortunately conflicts with jdk7-openjdk. So if you want to keep Java 7, you can install Oracle Java 6 from AUR:

yaourt -S jre6-compat jdk6-compat

To use the installed jdk6 you can use a script that is provided with the packages: "/opt/sun-java6/envsetup.sh" Update: I just noticed that this isn't provided (anymore). You need to do this manually.

Now you have everything installed for building Android.

The next problem you will encounter is that the Android build tools require that /usr/bin/python symlinks to python version 2.X. Most guides suggest changing the symlink for building android and then changing it back [3] [5]. I think this is dangerous, because it could seriously break other stuff. So I wouldn't do this if it is a daily used system. Instead I created a directory with a symlink to python2 and put this directory in $PATH, before the regular $PATH.

mkdir /opt/android-build
cd /opt/android-build
ln -s `which python2` python
export PATH="/opt/android-build:$PATH"

So now it's like we have python2 installed

$ python -V
Python 2.7.3

Finally I wrote a special bash .profile file for building Android:

android-build-env () {
    echo "Setting up \$PATH"
    # Android tools
    export PATH="/opt/android-sdk/tools:/opt/android-sdk/platform-tools:/opt/android-build:$PATH"

    # Java6
    export J2SDKDIR=/opt/java6
    export PATH=/opt/java6/bin:/opt/java6/db/bin:$PATH
    export JAVA_HOME=/opt/java6
    export DERBY_HOME=/opt/java6/db

    # CCACHE
    export USE_CCACHE=1

    echo "Changing directory"
    if [ "$1" == "cm" ]; then
        cd $CMSRCDIR
        export PATH="$CMSRCDIR/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin/:$PATH"
    else
        cd $AOSPSRCDIR
        export PATH="$AOSPSRCDIR/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin/:$PATH"
    fi

    export ARCH=arm
    export SUBARCH=arm
    export CROSS_COMPILE=arm-eabi-

    echo "Including android build source"
    export CCACHE_DIR=./.ccache
    # hmmm not enough space :( for such a big cache... let's leave default
    #./prebuilts/misc/linux-x86/ccache/ccache -M 50G
    . build/envsetup.sh

    # reading for building androids
    #clear
    echo
    if [ "$1" == "cm" ]; then
        echo "### CM10 Build Environment ###"
    else
        echo "### AOSP Build Environment ###"
    fi
    echo
}

I hope I didn't forget anything here. Please tell me if there's something missing.

References:

[1]https://wiki.archlinux.org/index.php/Arch64_FAQ#Multilib_Repository_-_Multilib_Project
[2]https://wiki.archlinux.org/index.php/AUR
[3](1, 2) https://wiki.archlinux.org/index.php/Android#Building_Android
[4]http://codenameandroid.com/how-to-setting-up-a-linux-development-environment/
[5](1, 2) http://rootzwiki.com/topic/9287-how-to-setup-android-build-environment-on-arch-linux-64bit/
[6]http://rootzwiki.com/topic/9287-how-to-setup-android-build-environment-on-arch-linux-64bit/page__st__60#entry854543
[7]http://forum.xda-developers.com/showpost.php?p=30998147&postcount=17