Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,772,940 Members 46,173 Now Online
XDA Developers Android and Mobile Development Forum

[TUT] Configuring Arch Linux to build Android

Tip us?
 
GermainZ
Old
#1  
GermainZ's Avatar
Forum Moderator / Recognized Developer - OP
Thanks Meter 6101
Posts: 5,304
Join Date: Aug 2012
Default [TUT] Configuring Arch Linux to build Android

Arch Linux is a rolling release distribution, but Android requires you to have jdk6 (or openjdk6) and python2 to build it. It took me some time to setup everything, and I hope this guide will be useful to anyone who's trying to do the same.

The ArchWiki is a bit outdated. Among other things, it suggests changing the "python" symlink to point to python2, which I didn't want to do as it could cause problems ("python" is supposed to point to python3). Also, Arch Linux uses openjdk7, so you'd have to downgrade to openjdk6 (or Oracle's jdk6) - which isn't mentioned in the Wiki, and is something I'd rather avoid as I want to keep using the latest version.

Basically, I'd like to share this here for now to get some input, and later update the ArchWiki.

0- Quick note about this guide
I think it's pretty safe to assume you're familiar with the terminal.
Also note that this guide will not really talk about how to actually build Android, it's focused on setting up your Arch distro to do so in an easy way.

1- Prerequisites
The ArchWiki covers installing the Android SDK and ADB pretty well.

You'll need these packages for building Android:
Code:
git gnupg flex bison gperf sdl wxgtk squashfs-tools curl ncurses zlib schedtool perl-switch zip unzip
And for 64 bit systems (make sure multilib is enabled):
Code:
lib32-zlib lib32-ncurses lib32-readline gcc-libs-multilib gcc-multilib lib32-gcc-libs
You also need the "repo" utility, which is available in the AUR.

2- Setting up Java
The AUR has packages that should install alongside openjdk7, but that didn't work for me and I found it easier to simply install jdk6 independently of pacman's packages.
To do so, download the latest Linux .bin file (jdk-6u45-linux-x64.bin), and place it in /opt (run a root shell, "sudo bash"):
Code:
cp ~/Downloads/jdk-6u45-linux-x64.bin
Make it executable and run it as root:
Code:
cd /opt
chmod +x jdk-6u45-linux-x64.bin
./jdk-6u45-linux-x64.bin
We'll be updating our path before building to use this version.

3- Setting up Python
We're going to create a python symlink to python2, and place it in /opt/android-build. We'll use this later by updating our path when necessary.
Code:
mkdir /opt/android-build
cd /opt/android-build
ln -s $(which python2) python
4- (Temporarily) Setting up our PATH to test if everything's fine
We just installed jdk6 and made a symlink to python2, but we still need to update our path. In this step, we'll do that in order to test that everything's fine:
Code:
export PATH="/opt/android-build:/opt/jdk1.6.0_45/bin:$PATH"
Make sure jdk6 and python2 are being used:
Code:
echo -n $(python -V)
echo -n $(java -version)
The output should be similar to:
Code:
Python 2.x.x
java version "1.6.xx"
Java(TM) SE Runtime Environment (build 1.6.xx)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
5- Make a build script (or just build).
I'd highly suggest making a build script and placing it in ~/bin. You can then easily run it, and it will take care of everything.
Otherwise, you can run this in the terminal every time before building:
Code:
export PATH="/opt/android-build:/opt/jdk1.6.0_45/bin:$PATH"
And then proceed normally, for example:
Code:
. build/envsetup.sh
lunch cyanogen_cooperve-eng
make clean
time make -j6 bacon
This is my (trimmed down to remove some git revert checks) personal build script, you can modify it to fit your needs. The working directory is your home folder, change the "path" variable around the middle if yours is different.
 

Code:
Help() {
    echo -n "Usage: build [OPTIONS]
    -h, --help      show this screen
    -s, --sync      run repo sync before building
    -c, --clean     make clean"
    echo
}

box () {
    tput setaf 6
    echo
    str="$@"
    len=$((${#1}+4))
    for ((i = 1; i <= $len; i++)); do
        echo -n '*'
    done
    echo
    echo "* $str *"
    for ((i = 1; i <= $len; i++)); do
        echo -n '*'
    done
    echo
    tput sgr0
}

path="/home/germain/"
sync=0
clean=0
while :; do
    case $1 in
        -h | --help)
            Help
            exit 0
            ;;
        -s | --sync)
            sync=1
            shift
            ;;
        -c | --clean)
            clean=1
            shift
            ;;
        --)
            shift
            break
            ;;
        -*)
            echo "Unknown option $1, ignoring" >&2
            shift
            ;;
        *)
            break
            ;;
    esac
done

clear
box "Setting up \$PATH..."
export PATH="/opt/android-build:/opt/jdk1.6.0_45/bin:$PATH"
echo -n $(python -V)
echo -n $(java -version)

cd "$path"

(( "$sync"==1 )) && box "Syncing..." && repo sync -j64

box "Starting build..."
. build/envsetup.sh
lunch cyanogen_cooperve-eng
(( "$clean"==1 )) && box "Clean build!" && echo && make clean
time make -j6 bacon

box "Done!"

To set it up:
Code:
nano ~/bin/build
<copy the above script, press SHIFT + INSERT to paste, and CTRL+X followed by "Y" to save>
chmod +x ~/bin/build
Usage:
Code:
Usage: build [OPTIONS]
    -h, --help      show this screen
    -s, --sync      run repo sync before building
    -c, --clean     make clean
Sources
These web pages helped me a lot.
  1. http://f0rki.at/building-android-on-...ux-x86_64.html
  2. https://wiki.archlinux.org/index.php/Android
  3. https://bbs.archlinux.org/viewtopic.php?id=131939
The Following 22 Users Say Thank You to GermainZ For This Useful Post: [ Click to Expand ]
 
lelinuxien52
Old
#2  
lelinuxien52's Avatar
Senior Member
Thanks Meter 56
Posts: 264
Join Date: Jun 2012
Location: Aix-en-Provence (04100)
Good job dude. I already use Arch Linux for developing Android firmware but I haven't the time to make one tutorial for others.
Device : Sony Xperia Z1 (Black - International version C6903)
Current : Android JellyBean 4.4.4 -> Stock ROM : 14.4.A.0.108 - Root (Towelroot exploit) / Bootloader locked - but unlockable (TA partition saved)
Nokia 3310 > Sanyo S750 > Sony Ericsson K610i > Nokia 5800 Xpressmusic > Nokia N900 > Samsung Galaxy S3 > Sony Xperia Z1

If I help you, give me a "Thanks".
 
kalpik
Old
(Last edited by kalpik; 18th May 2013 at 04:58 AM.)
#3  
Recognized Contributor
Thanks Meter 1399
Posts: 3,630
Join Date: May 2010
Location: Delhi
Thanks for the guide, but even after I have set the correct java path, (java -version shows Java SE), while building, it still gives a warning that I'm building with Java 1.7 (openjdk). Any pointers?

Checking build tools versions...
************************************************** **********
You are attempting to build with an unsupported version
of java.

Your version is: java version "1.7.0_21".
The correct version is: Java SE 1.6 or 1.7.

Please follow the machine setup instructions at
https://source.android.com/source/download.html



EDIT: NVM. Fixed by exporting JAVA_HOME and J2SDKDIR
The Following User Says Thank You to kalpik For This Useful Post: [ Click to Expand ]
 
marno11
Old
#4  
Junior Member
Thanks Meter 1
Posts: 2
Join Date: Jun 2013
Geat job GermainZ!

The packages in the AUR which should install alongside openjdk7 should work (it works for me) if you:
Code:
export PATH="/opt/java6/bin:$PATH"
Then (as you mention) you can check by:
Code:
echo -n $(java -version)

Anyway thanks for the guide, I think it is definitely worth updating the Arch Wiki to include this information!
The Following User Says Thank You to marno11 For This Useful Post: [ Click to Expand ]
 
GermainZ
Old
#5  
GermainZ's Avatar
Forum Moderator / Recognized Developer - OP
Thanks Meter 6101
Posts: 5,304
Join Date: Aug 2012
Quote:
Originally Posted by marno11 View Post
Geat job GermainZ!

The packages in the AUR which should install alongside openjdk7 should work (it works for me) if you:
Code:
export PATH="/opt/java6/bin:$PATH"
Then (as you mention) you can check by:
Code:
echo -n $(java -version)

Anyway thanks for the guide, I think it is definitely worth updating the Arch Wiki to include this information!
I see they were recently updated. They weren't building for me at the time, and I didn't have enough time to check the PKGBUILD.
I'll do my best to update the wiki when I can, including both options. Thanks.
The Following 2 Users Say Thank You to GermainZ For This Useful Post: [ Click to Expand ]
 
stammler
Old
#6  
Senior Member
Thanks Meter 209
Posts: 107
Join Date: Jan 2011
Location: Frankfurt

 
DONATE TO ME
Thanks for putting this up, I landed on this after following the link in the Arch wiki.

I have a suggestion concerning point 3 (python). Wouldn't it be more sane to use python's virtualenv to set up a python distro for Android?

And BTW, I am trying out the new android-studio, which works for me out of the box, even with openjdk7. Just make sure you install openjdk7-src as Java sources are not (yet) in the dependencies of the android-studio package from the AUR.
Google Nexus 4 on Paranoid Android :: TWRP Recovery
OS: Arch Linux with Gnome 3.8
Xposed mods: Unsafe Volume :: Snapshare :: Keepchat (contributed)
The Following User Says Thank You to stammler For This Useful Post: [ Click to Expand ]
 
GermainZ
Old
#7  
GermainZ's Avatar
Forum Moderator / Recognized Developer - OP
Thanks Meter 6101
Posts: 5,304
Join Date: Aug 2012
Quote:
Originally Posted by stammler View Post
Thanks for putting this up, I landed on this after following the link in the Arch wiki.

I have a suggestion concerning point 3 (python). Wouldn't it be more sane to use python's virtualenv to set up a python distro for Android?

And BTW, I am trying out the new android-studio, which works for me out of the box, even with openjdk7. Just make sure you install openjdk7-src as Java sources are not (yet) in the dependencies of the android-studio package from the AUR.
So basically, create a python 2.7 virtualenv, activate and build from it?
Personally, I find that creating a symlink and adding it to your path (temporary) seems easier and requires no extra tools, but that's up to you. Have you tried it out? Can you please elaborate on why it would be preferable?

Thanks
 
Warped420
Old
#8  
Guest
Thanks Meter 164
Posts: 0
Join Date: Jun 2013
i have to ask, instead of doing all these steps, wouldn't it be easier just to install BBQLinux and change the java version with a purge command and reinstall 1.6 ?
 
GermainZ
Old
(Last edited by GermainZ; 24th June 2013 at 09:44 PM.)
#9  
GermainZ's Avatar
Forum Moderator / Recognized Developer - OP
Thanks Meter 6101
Posts: 5,304
Join Date: Aug 2012
Quote:
Originally Posted by Warped420 View Post
i have to ask, instead of doing all these steps, wouldn't it be easier just to install BBQLinux and change the java version with a purge command and reinstall 1.6 ?
Are you suggesting that installing a whole distro on top of the one you use, just to build ROMs, is easier than installing one package, making a symlink and prepending something to your path? Because if you think that's hard, then I must say Arch might be the wrong distro for you.

All these instructions should not take more than 2 minutes, excluding the download time (frankly, I spent most of my time deciding if I should get openjdk6 or java6, then customizing my build script (you can just use a bash profile which virtually takes no time to setup, but I needed some extra stuff)).

That being said, if you want to downgrade, you can also do it using Arch (get openjdk6 from the ARM (Arch Rollback Machine), install it, edit pacman.conf to ignore it - very straightforward to do). The whole point was not to, but you can.
 
Warped420
Old
(Last edited by Warped420; 24th June 2013 at 10:47 PM.)
#10  
Guest
Thanks Meter 164
Posts: 0
Join Date: Jun 2013
??

Tags
arch linux, building, custom rom, java, python
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes