Android Build System

Source: Internet
Author: User

Android Build SystemOriginal address: Http://elinux.org/Android_Build_System Another better article: http://www.ibm.com/developerworks/cn/opensource/ Os-cn-android-build/#author before is a piece of understanding, after reading this article, the process of compiling Android system to comb clear.

Basics of the Android Build system were described at (down as of9/2011): Http://source.android.com/porting/build _system.html

Note that "Partner-setup" should is replaced with "Choosecombo" or even "lunch" on that description.

(Thisinformation seems to being duplicated at: http://pdk.android.com/online-pdk/guide/build_system.html I ' m Going to assume the source.android site are the most up-to-date, but I haven ' t checked.)

More information on the Android build system, and some of the rationale for it, is described at:http://android.git.ke Rnel.org/?p=platform/build.git;a=blob_plain;f=core/build-system.html

You use build/envsetup.sh to set up a "convenience environment" for working on the Android source code. This file should is source ' ed into your current shell environment. After doing so can type "help" (or ' hmm ') for a list of defined functions which is helpful for interacting with the S Ource.

Contents[Hide]
  • 1 Overview
  • 2 Some Details
    • 2.1 What tools is used
    • 2.2 telling the system where the Java toolchain is
    • 2.3 specifying to build
    • 2.4 actually building the system
  • 3 Build tricks
      • 3.1 seeing the actual Commands used to build the software
      • 3.2 make targets
      • 3.3 helper Macros and Functions
      • 3.4 speeding up the build
      • 3.5 building only Individual program or module
      • 3.6  setting module-specific build parameters
  • 4 Makefile Tricks
    • 4.1 Build Helper functions
    • 4.2 Add A file directly to the output area
  • 5 Adding A new program to build
    • 5.1 Steps for adding a new program to the Android source tree
  • 6 Building the kernel
Overview

The build system uses some pre-set environment variables and a series of ' make ' files on order to build an Android system and prepare it for deployment to a platform.

Android make files end in the extension '. Mk ' by convention, with the main make file in any particular source Dir Ectory being named ' Android.mk '.

There is only one official file named ' Makefile ' in the top of the source tree for the whole repository. You set the some environment variables, then type "make" to build stuff. You can add some options to the Do command line (other targets) to turn on verbose output, or perform different actions.

The build output is placed in ' out/host ' and ' Out/target ' Stuff under ' out/host ' be things compiled for your host platfor m (Your desktop machine). Stuff under ' out/target/product/<platform-name> ' eventually makes it ' s to a target device (or emulator).

The directory ' Out/target/product/<platform-name>/obj ' is used for staging "object" files, which was intermediate bi Nary images used for building the final programs. Stuff that actually lands in the file system of the "target is stored" in the directories root, system, and data, under ' out /target/product/<platform-name> '. Usually, these is bundled up to image files called system.img, ramdisk.img, and userdata.img.

This matches the separate file system partitions used on most Android devices.

Some DetailsWhat tools is used

The

During the build you'll be using the "make" to control the build steps themselves. A host toolchain (compiler, linker and other tools) and libraries would be used to build programs and tools that would run O n the host. A different toolchain is used to compile the C and C + + code that would wind up on the target (which was an embedded board, D Evice or the emulator). This is usually a ' cross ' toolchain that runs in an X86 platform, but produces code for some other platform (most commonly ARM). The kernel is compiled as a standalone binary (it does does use a program loader or link to any outside libraries). Other items, like native programs (e.g. init or toolbox), daemons or libraries would link against bionic or other system Li Braries.

You'll be using a Java compiler and a bunch of java-related tools to build most of the application framework, System Ser Vices and Android applications themselves. Finally, tools is used to package the applications and resource files, and to create the filesystem images the can is in Stalled on a device or used with the simulator.

telling the system where the Java toolchain is

Before you build anything and you have a to-tell the Android build system where your Java SDK is. (Installing a Java SDK is a pre-requisite for building).

Do this by setting a java_home environment variable.

Specifying to build

In order to decide-to-build, and how to build it, the build system requires this some variables be set. Different products, with Different packages and options can is built from the same source tree. The variables to control this can is set via a file with declarations of ' make ' variables, or can is specified in the ENVI Ronment.

A device vendor can create definition files that describe what's to be included on a particular board or for a particular Product. The definition file is called:buildspec.mk, and it's located in the top-level source directory. You can edit the manually to hardcode your selections.

If you had a buildspec.mk file, it sets all the "make variables needed for a build" and you don ' t had to mess with option S.

Another method of specifying options is to set environment variables. The build system has a rather ornate method of managing these options for you.

To set up your build environment, you need to load the variables and functions in build/envsetup.sh. Do this by ' source-ing ' the file to your shell environment, like this:

$ source Build/envsetup.sh

Or

$ . build/envsetup.sh

You can type "help" (or ' hmm ') at the this point to see some utility functions that is available to make it easier to work WI Th the source.

To select the set of things your want to build, and what items to build for, you use either the ' Choosecombo ' function or T He ' lunch ' function. ' Choosecombo ' would walk you through the different items that you had to select, One-by-one and while ' Lunch ' allows your select so Me pre-set combinations.

The items that has to is defined for a build is:

    • The product (' generic ' or some specific board or platform name)
    • The build variant (' User ', ' userdebug ', or ' eng ')
    • Whether you ' re running on a simulator (' True ' or ' false ')
    • The build type (' Release ' or ' Debug ')

Descriptions of these different build variants is at http://source.android.com/porting/build_system.html# Androidbuildvariants

The build process from a user perspective are described pretty well in this blog Post:first Android platform build by Code Painters, December 2009

actually building the system

Once you has things set up and you actually build the system with the ' make ' command.

To build the whole thing, run "make" in the top directory. The build would take a long time and if you were building everything (for example, the first time to do it).

Build Tricksseeing the actual commands used to build the software

Use the "Showcommands" target on your ' make ' line:

$ make-j4 Showcommands

This can is used in conjunction with another make target and to see the commands for that build. That's, ' showcommands ' is not a target itself, but just a modifier for the specified build.

In the example above, the-j4 are unrelated to the showcommands option, and are used to execute 4 make sessions this run in Parallel.

Make targets

Here are a list of different make targets you can use to build different parts of the system:

    • Make Sdk-build the tools that is part of the SDK (ADB, fastboot, etc)
    • Make Snod-build the system image from the current software binaries
    • Make services
    • Make runtime
    • Make Droid-make droid is the normal build.
    • Make All-make everything, whether it's included in the product definition or not
    • Make Clean-remove all built files (prepare for a new build). Same as Rm-rf out/<configuration>/
    • Make Modules-shows a list of submodules so can be built (list of all local_module definitions)
    • Make <local_module>-Make a specific module (note that this is not the same as directory name. It is the local_module definition in the android.mk file)
    • Make clean-<local_module>-Clean a specific module
Helper Macros and Functions

There is some helper macros and functions that is installed when you source envsetup.sh. They is documented at the top of the envesetup.sh, but this is information about a few of them:

    • Croot-change directory to the top of the tree
    • M-execute ' make ' from the top of the tree (even if your current directory is somewhere else)
    • Mm-builds all of the modules in the current directory
    • Mmm <dir1> ...-build all of the modules in the supplied directories
    • Cgrep <pattern>-grep on all local C + + files
    • Jgrep <pattern>-grep on all local Java files
    • Resgrep <pattern>-grep on all local res/*.xml files
    • Godir <filename>-Go to the directory containing a file
speeding up the build

You can use the '-j ' option with make and to-start multiple threads of make execution concurrently.

In my experience, your should specify about 2 more threads than you had processors on your machine. If you had 2 processors, use ' make-j4 ', if they is hyperthreaded (meaning you had 4 virtual processors), try ' make-j6 .

You can also specify to use the ' ccache ' compiler cache, which'll speed up things once you have built things a first Tim E. To does this, specify ' export use_ccache=1 ' at your shell command line. (Note that CCache are included in the prebuilt sections of the repository, and does not having to be installed on your host SE Parately.)

Building only a individual program or module

If You use build/envsetup.sh, you can use some of the defined functions to build only a part of the tree. Use the ' mm ' or ' mmm ' commands to does this.

The ' mm ' command makes stuff in the current directory (and Sub-Directories, I believe). With the ' mmm ' command, you specify a directory or list of directories, and it builds those.

To install your changes, does ' make Snod ' from the top of the tree. ' Make Snod ' builds a new system image from the current binaries.

Setting module-specific Build Parameters

Some code in Android system can is customized in the They is built (separate from the build variant and release vs. D Ebug options). You can set variables this control individual build options, either by setting them in the environment or by passing them directly to ' make ' (or the ' M ... ' functions which call ' make ')

For example, the ' init ' program can be built with the Bootchart logging by setting the Init_bootchart variable. (See Using the Bootchart on Android for why might want to does this.)

You can accomplish either with:

$ touch system/init/init.c$ Export init_bootchart=true$ make

Or

$ touch system/init/init.c$ m init_bootchart=true
Makefile Tricks

These is some tips for things your can use in your own android.mk files.

Build Helper Functions

A whole bunch of build helper functions is defined in the file build/core/definitions.mk

Try grep define BUILD/CORE/DEFINITIONS.MK for a exhaustive list.

Here is some possibly interesting functions:

    • Print-vars-shall all Makefile variables, for debugging
    • Emit-line-output a line during building, to a file
    • Dump-words-to-file-output a list of words to a file
    • Copy-one-file-copy a file from one place to another (dest on target?)

Add a file directly to the output area

You can copy a file directly to the output area, without building anything, using the Add-prebuilt-files function.

The following line, extracted from prebuilt/android-arm/gdbserver/android.mk copies a list of files to the executables dir Ectory in the output area:

$ (call Add-prebuilt-files, executables, $ (prebuilt_files))
Adding A new program to buildSteps for adding a new program to the Android source tree
    • Make a directory under ' external '
      • e.g. Android/external/myprogram
    • Create your c/cpp files.
    • Create Android.mk as clone of External/ping/android.mk
    • Change the names Ping.c and ping to match your c/cpp files and program name
    • Add the directory name in Android/build/core/main.mk after External/zlib as External/myprogram
    • Make from the root of the source tree
    • Your files would show up in the build output area, and in system images.
      • You can copy your file from the build output area, under out/target/product/..., if you want to copy it individually to th E Target (not do a whole install)

See Http://www.aton.com/android-native-development-using-the-android-open-source-project/for a lot more detail.

Building the kernel

The kernel is "outside" of the normal Android build system (indeed, the kernel are not included by default in the Android O Pen Source Project). However, there is tools in AOSP for building a kernel. If You is building the kernel, start on this page:http://source.android.com/source/building-kernels.html

If you were building the kernel for the emulator, you could also want to look at:http://stackoverflow.com/questions/1809774/ Android-kernel-compile-and-test-with-android-emulator

And, Ron M wrote (on the Android-kernel mailing list, in May 21, 2012):

This post was very old-but nothing had changes as far as AOSP was concerned, so in case anyone was interested and runs into This problem if building for QEMU:

There is actually a nice and shorter-to-build the kernel for your QEMU target provided by the AOSP:

1. CD to your kernel source dir (only goldfish 2.6.29 works out of the box for the emulator)

2. ${android_build_top}/external/qemu/distrib/build-kernel.sh-j=64--arch=x86--out= $YourOutDir

3. Emulator-kernel ${youroutdir}/kernel-qemu # Run Emulator:


Step #2 calls the toolbox.sh wrapper scripts which works around the SSE disabling gcc Warning-which happens for GCC < 4.5 (as in the AOSP prebuilt X86 toolchain).

This script adds the '-mfpmath=387-fno-pic ' in case it's an X86 and that in turn eliminates the compilation errors seen Above.

To has finer control over the build process, you can use the ' toolbox.sh ' wrapper and set some other stuff without modify ing the script files.

An example for building the same emulator is below:

# Set Archexport arch=x86# has make refer to the QEMU wrapper script for building Android over x86 (eliminates the errors Listed above) export cross_compile=${android_build_top}/external/qemu/distrib/kernel-toolchain/ android-kernel-toolchain-# Put your cross compiler here. I am using the AOSP prebuilt one in this exampleexport real_cross_compile=${android_build_top}/prebuilt/linux-x86/ toolchain/i686-android-linux-4.4.3/bin/i686-android-linux-# Configure your Kernel-here I am taking the default goldfish _defconfigmake goldfish_defconfig# buildmake-j64# Run emulator:emulator-kernel Arch/x86/boot/bzimage-show-kernel


This works for the 2.6.29 goldfish branch. If anyone is using the-the emulator with a kernel I would being like to hear about it.

Android Build System

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.