Chapter 3. Set up and Build – the Emulator Way

In the previous chapter, we learned how to retrieve the source code and we had an overview of the folder's structure. We now know how the branching model works and how to contribute to the project. This is an important topic, because Android is an open source prot, but it's managed in a very different way compared to other popular open source projects.

In this chapter, we will set up the whole environment to get ready to build our first Android system and flash it to a real target. Our efforts will be focused to create a fully-working version for the official Android emulator.

The user will learn how to use tools such as adb and fastboot, two of the most important tools that Google provides.

Preparing the host system

To build a complex system such as Android, we need to satisfy a few hardware and software requirements. First of all the host system.

The official Linux distribution supporting the Android build environment is Ubuntu Linux. Google periodically releases new Android builds for its devices and all of them are created using Ubuntu. Currently, Google is using Ubuntu 14.04 even if this is not the latest version available.

Every example in this book will be developed and executed on a common notebook, with an Intel i5 CPU and 4 GB of RAM, running Ubuntu Linux 15.05, that's the latest available version. Using a different Linux version proves that if all the requirements are satisfied, you could build Android with any Linux distribution or even Mac OS X— if you can't set up Ubuntu, trying with a different version will be challenging, but will be worth trying, as a learning experience.

If you are a Microsoft Windows user, it is sad to say, you won't be able to build Android using the native operative system. A possible solution is using a virtual machine running Ubuntu, for instance.

Hardware requirements

Digging into hardware requirements, you will just need a recent personal computer. As anticipated in the previous section, we are going to use a middle-end notebook for our examples. It's a Lenovo x220, with Intel i5 CPU and 4GB of RAM: it's enough to do the job and it's affordable, but the build time won't be small.

To speed up the build time, using a high-end PC is advisable. A faster CPU, with more cores, and more RAM will take advantage of multithreading and parallel building and will significantly reduce the build time, allowing you to experiment more during the journey.

A critical point of the environment setup is the necessary hard disk free space. The required amount is considerable—the source code alone needs approximately 100 GB to be stored. The whole build process will require approximately 150GB. If we are trying to build as fast as possible, probably we will enable the building system caching option, ccache. The caching system will require even more free space.

The following table will give you a rough estimation about minimum and recommended hardware:

 

Minimum

Recommended

Processor

4 core processor at 2 GHz

8 core processor at 2.5 GHz

RAM

8 GB

16 GB

Disk Space

200 GB

500 GB

Software requirements

In this book, we are going to build the system using Ubuntu Linux 15.04. If you cannot obtain this version, you can successfully use an older version, like the guys at Google, a totally different distribution or even a Virtual Machine.

One of the basic requirements, when it comes to the operating system, is the architecture: if we are planning to build Android 2.3 or greater, we will need a 64-bit system. Older versions of Android will do fine with a 32-bit system, but that's an improbable scenario.

Installing Java JDK

Oracle's Java Development Kit is a crucial requirement, essential to be able to build Android. Every Android version needs a specific JDK version. According to what version we want to build, we are going to install:

  • JDK 5 for Cupcake to Froyo
  • JDK 6 for Gingerbread to KitKat
  • JDK 7 for KitKat, Lollipop, and Marshmallow

We are going to build Android Lollipop 5.1.1 and we are going to need at least JDK 7. Installing JDK on Ubuntu is quite straightforward. Let's start by opening a Terminal and firing the following command:

~$ sudo apt-get install openjdk-7-jdk

The apt-get command will resolve all the dependencies, download all the required packages and install them. If you are a Mouse and icons user, you can achieve the same goal using Ubuntu Software Center, as shown in the following screenshot:

Installing Java JDK

If you are a Java developer or you plan to build different Android versions for specific reasons, Ice Cream Sandwich and Lollipop, for instance, you could end up having more than just one version of the Java Development Kit. This multipurpose scenario brings a few more steps of configuration. We need to specify which JDK version will be used as the default one in the system. Using our trusted Terminal, let's run these commands:

~$ sudo update-alternative –config javac

The following screenshot shows the output. As you can see, it lists all the available JDK versions and lets you pick the one to set as default. In our scenario, we are using JDK 7 because we are planning to build Android 5 or greater.

Installing Java JDK

Installing system dependencies

Even if Java is a key player in the Android world, we also need a few low-level tools to satisfy all the Android build system requirements. Some of them are common tools and there is a chance that they are already installed, but our goal is to set up a whole system from scratch: we can't risk missing a dependency.

Using your Terminal, run the following apt-get command:

~$ sudo apt-get install bison g++-multilib git gperf libxml2-utils 
    make python-networkx zlib1g-dev:i386 zip

As usual, apt-get will resolve all the dependencies and install all the required packages. The following screenshot shows the output of the command in the scenario in which you already have all the required packages, lucky you:

Installing system dependencies

At this point, your Ubuntu contains all the required packages and applications to build the world's most popular mobile operating system.

Setting up a Mac OS X environment

One of the most important requirements to build Android is a case-sensitive filesystem. If you are planning to build Android using OS X, the most practical way to satisfy this requirement is to create a partition or a disk image containing a case-sensitive filesystem.

Creating a case-sensitive disk image

OS X provides a handy graphical utility to create a new disk image. Fire up Spotlight and launch Disk Utility. The upper toolbar contains a New Image button that takes you to the disk image creation screen, as shown in the following screenshot:

Creating a case-sensitive disk image

As you can see from the preceding screenshot, the crucial setting is the Format: it has to be Case-sensitive, Journaled. For the Size setting, the larger the better, keeping in mind that an Android build could use hundreds of gigabytes in no time. As minimal size, we suggest at least 50 GB.

If you are more a command line type, it's possible to create this disk image using the Terminal and hdiutil, as shown in the following command:

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

If the disk image creation succeeded, we now have a .dmg or .dmg.sparsefile file on our disk. Once we have mounted it, we can use it as a normal hark disk—downloading Android source code and proceeding with the building procedure.

The two following commands will give you the ability to mount and unmount the disk image:

~$ hdiutil attach ~/android.dmg -mountpoint /Volumes/android-disk;
~$ hdiutil detach /Volumes/android-disk;

Note

If you run out of space, the following command will give you the opportunity to resize the disk image and allow you to continue working on your desired Android build:

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

Installing the required software

Once we have our installation disk image, the same as for Linux, we need to install all those software requirements we need to properly build the system.

Installing the Java Development Kit is very straightforward: just download the proper .dmg file from http://www.oracle.com and install it. The same rules about Android target version and required Java version apply here too.

Further, we will need:

Once we have these two main pieces of the puzzle in place, we need to install make, git, bison, and GPG packages, using MacPorts, with the following command on your Terminal:

~$ POSIXLY_CORRECT=1 sudo port install gmake bison libsdl git gnupg

Last but not least, we need to increase the maximum number of possible file descriptors. OS X comes with a tiny value—the average user does not need all those file descriptors, but we are going to need a bigger amount due to the hundreds of files involved in the Android build procedure. To increase this value, we will need to launch our Terminal and run the following command:

~$ ulimit –S –n 1024 

Now, the limit is up to 1,024 files. We can make this value persistent by adding this to the ~/.bash_profile file, in your home folder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.54.255