Compiling AOSP for your Nexus device on Mac OSX

  3 mins read

Requirements

  • 8GB of RAM, recommended 16GB
  • 100 gb of free space on your drive

Preparing the environment

First we need a case-sensistive file system, so we need to create a sparseimage because by default the OSX system is not case-sensitive

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 60g ~/android.dmg

If we want to resize this image later we can do it by executing (just in case):

hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage

Install the following:

JDK7 (last revision) and MacPorts from macports.org
When Macports is installed, run the following in order to install the dependencies to compile AOSP:

POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg

Now we have to add some variables and some of them add to the PATH, so edit bash_profile:

nano .bash_profile

And paste the following code:

# set the number of open files to be 1024
ulimit -S -n 1024
# Compiler cache
export USE_CCACHE=1
# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/untitled; }
# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/untitled; }
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH=~/bin:$PATH
export PATH=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin:$PATH
export ANDROID_JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

Please note the 2 last lines, and replace the jdk1.7.0_79.jdk with the version of your installed version, if not the path will not be found.

To load the previous changes without re-run a terminal, run the following command:

source ~/.bash_profile

You need Xcode 5.1.1 to compile AOSP, the latest one don’t works at least to build Lollipop (5.1), so go to the Developer center and download Xcode 5.1.1 image.

  1. Mount downloaded XCode5.1.1.dmg
  2. Right click on XCode and select Show Package Contents
  3. Create the folder /Developer/SDK
  4. Copy MacOSX10.8.sdk from Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ to /Developer/SDK
  5. Copy Xcode.app from the Xcode5.1.1.dmg to /Developer
  6. Eject & delete the .dmg

Let’s setup Xcode to use the old command line tools that we’ve downloaded in the previous step:

  1. Open up Xcode 6
  2. From the menu, select XCode -> Preferences…
  3. Click on the Location tab
  4. The last option, “Command Line Tools”, select “Xcode 5.1.1″

Downloading the source code

First, we are going to download “Repo”, is a tool to manage the source code from git developed by Google specially mind for AOSP. To do that run the following commands:

mkdir ~/bin
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

Mount the previous android image that we’ve created with the function that we installed in the bash_profile, you can also double click the sparseimage we created before.

mountAndroid

Get inside the image with:

cd /Volumes/android

Create a working directory and enter inside:

mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

Initialize the manifest (It has nothing to do with AndroidManifest.xml) repository with:

repo init -u https://android.googlesource.com/platform/manifest

Checkout the desired branch, for the example I’m going to checkout “android-5.1.0_r5” but you can checkout whatever you want, here is a link of all branches created.

repo init -u https://android.googlesource.com/platform/manifest -b android-5.1.0_r5
repo sync

After download the code, initialize the Compiler Cache with:

prebuilts/misc/darwin-x86/ccache/ccache -M 50G

Building Android

Initialize the environment with:

source build/envsetup.sh

Note: if you close the terminal and enter later, you’ll need to do this command again because it sets some variables, etc.

If you are creating this AOSP for a device, you’ll need to embed the drivers for this device, Im building for a Nexus 4 so downlaod the drivers from the Google driver page uncompress the tgz files and you’ll find a .sh script. Execute both 3 accept the license, and you’ll have a “vendor” directory, copy to the root of previous created folder “WORKING_DIRECTORY”.

Now, you need to choose a target, launch the command:

lunch

And select the target you want, I’m building a Nexus4 rom, so mine is: full_mako-eng. You can also do lunch full_mako-eng if you know the target you want to point. note the suffix of the target, userdebug or eng for production.

Build the source with:

make -j20

Where -jX is the number of parallel tasks you want to execute.

Run AOSP

If all the steps are ok, you have now an image of AOSP! You have 2 options now, execute in a emulator (coward) and flash to a device.

Let’s enter the result directory, in my case:

cd out/target/product/mako

Replace “mako” with the codename of your device.

If you want to use the emulator, just run the command:

emulator

And you are done, if you want to flash a device, put it in fastboot mode and run:

fastboot flashall -w

The final -w will wipe all previous data you had in your device.

And you’re done, at this point you’ll have a system ready and booting like mine:

Screenshot_2015-07-29-17-38-38

Documentation I used to create this post:
Build Android 5.0 os OSX 10.10 from @raminmahmoodi
Official documentation from Google
Missing symbols building android
Make your own aosp system nexus4

Written by:

Christian Panadero Martinez