[Miscellaneous Android Development] MasterAwesome:
Journey through the build process

Android is based on the Linux Kernel. Read on Wikipedia .

Before we get started, I created a binary long ago and thought I'd share it which helps you quickly setup your environment. If you're new to this, select option 3 and it should setup the environment for you. make sure to run it with `sudo` perms something like

sudo ./setupEnv

. This will basically install library dependencies and set CCACHE=1 also size of ccache to a max of 15GiB(recommended). It should set your swappiness to 10 so that kernel doesnt swap out memory and lesser IO access == faster compile times. Now that that's over. Let's get into business!

Before we begin you must know at least the slightest of :

1. Java

2. C/C++

3. Makefiles

Knowing these will help you debug the ROM as well as solve any compile issues.

Short description about the folders present:

ART: Default Runtime used in lollipop

Bionic - the C-runtime for Android. It's mostly on BSD-derived sources. In this folder you will find the source for the c-library, math and other core runtime libraries.

Bootable: Contains the source for recovery.

Build - the build system implementation including all the core make file templates.

Device - product specific code for different devices.

External - contains source code for all external open source projects such as SQLite, Freetype and webkit.

Frameworks - this folder is essential to Android since it contains the sources for the framework. Consists of native libraries and core Java classes required for android. Example(ListView,TextView) classes are present here.

Hardware - hardware related source code such as the Android hardware abstraction layer specification and implementation. Also contains RIL implementations.

Kernel- Contains the sources for the Android version of the Linux kernel.

Packages - contains the source code for the default applications

Prebuilt - contains files that are distributed in binary form for convenience. Examples include the cross compilations toolchains for different development machines.

System - source code files for the core Android system. That is the minimal Linux system that is started before the VM and any java based services are enabled. This includes the source code for the init process and the default init.rc script that provide the dynamic configuration of the platform.

Read more about it at http://elinux.org/Android-4.0.3_r1. It's ICS but still gives a understanding of what each folder contains.

setupEnv

You should be able to setup build environment and initialise repository needed through my binary.

The device tree

.Contains the necessary PRODUCT_PACKAGES specified by your makefile which suits your device.

.CPU architecture, Partition size everything is set here.

.A skeleton tree can be created using

./build/tools/device/mkvendor.sh vendor devicename pathtoboot.img

.Doing this will provide a prebuilt kernel and not an inline kernel build process. using a prebuilt kernel is deprecated since it might not work correctly with the ROM you're building

.Custom init files are set here(Different radio types)

.Custom overlays for your device is set here.

.Custom init scripts are set here

Lets examine each of these files:

BoardConfig.mk

This file contains vital architectual and build information about the architecture of your device's motherboard, CPU, and other hardware. Getting this file right is essential.

To get a basic recovery booting, a few parameters need to be set in this file.

The following parameters must be set properly in BoardConfig to compile a working recovery image:

TARGET_ARCH: this is the architecture of the device it is usually something like arm or omap3.

BOARD_KERNEL_CMDLINE: not all devices pass boot parameters however if your device does this must be filled out properly in order to boot successfully. You can find this information in the ramdisk.img. (You can learn more about configuring the integrated kernel build-from-source here.)

BOARD_KERNEL_PAGESIZE: the pagesize of the stock boot.img and must be set properly in order to boot. Typical values for this are 2048 and 4096 and this information can be extracted from the stock kernel.

BOARD_BOOTIMAGE_PARTITION_SIZE: the number of bytes allocated to the kernel image partition.

BOARD_RECOVERYIMAGE_PARTITION_SIZE: the number of bytes allocated to the recovery image partition.

BOARD_SYSTEMIMAGE_PARTITION_SIZE: the number of bytes allocated to the Android system filesystem partition.

BOARD_USERDATAIMAGE_PARTITION_SIZE: the number of bytes allocated to the Android data filesystem partition.

Note:

The above information can be obtained by multiplying the size from /proc/partitions or /proc/mtd by the block size, typically 1024.

BOARD_HAS_NO_SELECT_BUTTON: (optional), use this if your device needs to use its Power button to confirm selections in recovery.

BOARD_FORCE_RAMDISK_ADDRESS / BOARD_MKBOOTIMG_ARGS: (optional), use these to force a specific address for the ramdisk. This is usually needed on larger partitions in order for the ramdisk to be loaded properly where it's expected to exist. This value can be obtained from the stock kernel. The former is deprecated as of Android 4.2.x and the latter will now be used in 4.2.x and beyond.

device_[codename].mk

The device_codename.mk makefile contains instructions about which Android packages to build , and where to copy specific files and packages, or specific properties to set during your compilation .

This file can be used to copy vital files into the ramdisk at compilation time.

PRODUCT_COPY_FILES: used to copy files during compilation into the ramdisk, which will be located at $OUT/recovery/root.

Example:

$(LOCAL_PATH)/sbin/offmode_charging:recovery/root/sbin/offmode_charging \

This will copy the file offmode_charging binary into the sbin folder within the ramdisk.

PRODUCT_NAME / PRODUCT_DEVICE: used for setting the value of your codename. This is the name of the device you load with Lunch.

This is simply the prebuilt kernel image or a kernel you built yourself used to boot the device. The format of the kernel may be in a zImage or uImage , depending on the requirements of the architecture of your device.

cm.mk

You'll need to make a few changes to this file to integrate with the lunch , brunch , and breakfast commands, so that your device shows up on the list and builds properly. You'll also set some variables (see other devices) to indicate what size splash animation should be used, whether this is a tablet or phone, etc.

Some of these settings aren't used for building just the recovery, but you may as well set them now because once recovery is done and working, the settings here will be important.

Again, take a look at a similar device to yours to get an idea of what the settings here should be. It's fairly intuitive.

recovery.fstab

recovery.fstab defines the file system mount point , file system type , and block device for each of the partitions in your device. It works almost exactly like /etc/fstab in a standard Linux operating system.

Example:

/system	ext4	/dev/block/mmcblk0p32

This sets the block device at mmcblk0p32 to be mounted on /system as filesystem type ext4

All mountpoints should exist in this file and it is crucial this information be correct or else very bad things can happen, such as a recovery flash writing to the wrong location.

Note:

The filesystem type datamedia can be used for internal sdcards as well as setting the block device to /dev/null.

vendorsetup.sh

vendorsetup.sh is called when setupenv.sh is run. It is used to add non-standard lunch combos to the lunch menu.

To add your device to the lunch menu:

add_lunch_combo cm_<codename>-userdebug

Then build a test recovery image

wiki.cyanogenmod.org/w/Doc:_porting_intro

Vendor tree.

.Contains proprietary files/libraries

Examples: Vendor RIL, camera libraries

Once you have all this set up, you need to

cd pathToSource
. build/envsetup.sh
lunch device_lunch_name
make recoveryimage/bacon/bootimage/Package/framework/library...
# Output generated in the out/target/product/devicename/

Download executable here : https://github.com/MasterAwesome/environment_setup/blob/master/Release/setupEnv?raw=true

I have added only Cyanogenmod and AOSP to my executable for creating a repository, for others someone fork my repo and create a pull request, if its suitable i'll merge and upload a newer version.

AOSP Guide : https://source.android.com/source/building.html

Cyanogenmod Docs : http://wiki.cyanogenmod.org/w/Development