Saturday, April 30, 2011

How to compile Android source on Snow Leopard (Mac OS X 10.6.7)

So after that relatively long hiatus I am back. I have been busy mainly working on Android, and to a limited extent, iPhone apps. I wanted to compile android from source, I had heard that it wasn't too difficult. I had recently bought a MacBook Pro laptop and dedicated a weekend to this project. Well, like many things, it took longer, and was more involved and had several missteps. I will document what worked for me here, in case anyone else wants to do the same.

Google's instructions for this are provided here. There are other excellent blog posts about this, here and here. Unfortunately they all were about Android install on Mac OS x 10.5 or older. They also didn't talk about what happens if you install Xcode 4.

My Mac environment for this exercise is Snow Leopard Mac OS X 10.6.7 and Java 6 (1.6.0_24-b07-334). I also had Xcode 4 installed. Also ensure that you have a Google account, as you will be prompted for it during installation.

(Incidentally if you want to remove a version of Java and install another in Snow Leopard, delete /System/Library/Frameworks/JavaVM.framework folder and run Software Update on your mac. This will install it again).

So as described in Google website, run Disk Utility app on your Mac to create a 12 GHz disk image, with the format being Mac OS Extended (Case-sensitive, Journaled). It gets mounted in /Volumes, you can call it any name.

Now you have to install Macports. Some of the Google group posts say that it isn't needed for Snow Leopard, but I had already installed it. Also, to install it you have to compile it from source (following website instructions). Apparently the .pkg installer needed root permission but didn't not prompt for password. But with source compile we can use sudo command to work around that. After installing Macports don't forget to synchronize with the MacPort's rsync server.

Now ensure that /opt/local/bin is added to path. Macport install automatically modifies your .bash_profile to do that. If it is not setup correctly run 
$ export PATH=/opt/local/bin:$PATH.

Next follow the instructions on Google website to install gmake libsdl etc. And then revert gmake to older 3.81 version as detailed there.

After this add ulimit -S -n 1024 to your .bash_profile file to increase simultaneous file descriptor cap.

Now download the source using the Repo tool, set it your path, by following Google website instructions. Note that when you run repo sync it will take 10 or so minutes to download all source code.

Now run the following commands to compile:
$ source build/envsetup.sh
$ lunch full-eng
$ make -j2

Now on my MacBook Pro with Snow Leopard it will error out saying MacOSX10.5sdk folder not found. So download Xcode 3.2.6 from apple developer website, install it NOT in /Developer folder but in /Developer326 folder (otherwise it will overwrite your existing Xcode 4 install). Now copy /Developer326/SDKs/MacOSX10.5.sdk folder into /Developer/SDKs folder. Run again make -j2 and the compile will run for 25 or so minutes and you are done. You can now run emulator command to launch the emulator.