Android Build System(转)

Android Build System

Android uses a custom build system to generate tools, binaries, and documentation. This document provides an overview of Android’s build system and instructions for doing a simple build.

Android’s build system is make based and requires a recent version of GNU Make (note that Android uses advanced features of GNU Make that may not yet appear on the GNU Make web site). Before continuing, check your version of make by running % make -v. If you don’t have version 3.80 or greater, you need to upgrade your version of make.

Understanding the makefile

A makefile defines how to build a particular application. Makefiles typically include all of the following elements:

  1. Name: Give your build a name (LOCAL_MODULE := <build_name>).
  2. Local Variables: Clear local variables with CLEAR_VARS (include $(CLEAR_VARS)).
  3. Files: Determine which files your application depends upon (LOCAL_SRC_FILES := main.c).
  4. Tags: Define tags, as necessary (LOCAL_MODULE_TAGS := eng development).
  5. Libraries: Define whether your application links with other libraries (LOCAL_SHARED_LIBRARIES := cutils).
  6. Template file: Include a template file to define underlining make tools for a particular target (include $(BUILD_EXECUTABLE)).

The following snippet illustrates a typical makefile.

LOCAL_PATH := $(my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := <buil_name>
LOCAL_MODULE_TAGS := eng development

The snippet above includes artificial line breaks to maintain a print-friendly document.


The build hierarchy includes the abstraction layers described in the table below.

Each layer relates to the one above it in a one-to-many relationship. For example, an arch can have more than one board and each board can have more than one device. You may define an element in a given layer as a specialization of an element in the same layer, thus eliminating copying and simplifying maintenance.

Layer Example Description
Product myProduct, myProduct_eu, myProduct_eu_fr, j2, sdk The product layer defines a complete specification of a shipping product, defining which modules to build and how to configure them. You might offer a device in several different versions based on locale, for example, or on features such as a camera.
Device myDevice, myDevice_eu, myDevice_eu_lite The device layer represents the physical layer of plastic on the device. For example, North American devices probably include QWERTY keyboards whereas devices sold in France probably include AZERTY keyboards. Peripherals typically connect to the device layer.
Board sardine, trout, goldfish The board layer represents the bare schematics of a product. You may still connect peripherals to the board layer.
Arch arm (arm5te) (arm6), x86, 68k The arch layer describes the processor running on your board.

Building the Android Platform

This section describes how to build the default version of Android. Once you are comfortable with a generic build, then you can begin to modify Android for your own target device.

Device Code

To do a generic build of android, source build/, which contains necessary variable and function definitions, as described below.

% cd $TOP

% . build/

# pick a configuration using choosecombo
% choosecombo

% make -j4 PRODUCT-generic-user

You can also replace user with eng for a debug engineering build:

% make -j4 PRODUCT-generic-eng

These Build Variants differ in terms of debug options and packages installed.

Cleaning Up

Execute % m clean to clean up the binaries you just created. You can also execute % m clobber to get rid of the binaries of all combos. % m clobber is equivalent to removing the //out/ directory where all generated files are stored.

Speeding Up Rebuilds

The binaries of each combo are stored as distinct sub-directories of //out/, making it possible to quickly switch between combos without having to recompile all sources each time.

However, performing a clean rebuild is necessary if the build system doesn’t catch changes to environment variables or makefiles. If this happens often, you should define the USE_CCACHE environment variable as shown below:

% export USE_CCACHE=1

Doing so will force the build system to use the ccache compiler cache tool, which reduces recompiling all sources.

ccache binaries are provided in //prebuilt/... and don’t need to get installed on your system.

Building the Android Kernel

This section describes how to build Android’s default kernel. Once you are comfortable with a generic build, then you can begin to modify Android drivers for your own target device.

To build the kernel base, switch to the device directory (/home/joe/android/device) in order to establish variables and run:

% . build/
% partner_setup generic

Then switch to the kernel directory /home/joe/android/kernel.

Checking Out a Branch

The default branch is always android. To check out a different branch, execute the following:

% git checkout --track -b android-mydevice origin/android-mydevice
  //Branch android-mydevice set up to track remote branch
% refs/remotes/origin/android-mydevice.
  //Switched to a new branch "android-mydevice"

To simplify code management, give your local branch the same name as the remote branch it is tracking (as illustrated in the snippet above). Switch between branches by executing % git checkout <branchname>.

Verifying Location

Find out which branches exist (both locally and remotely) and which one is active (marked with an asterisk) by executing the following:

% git branch -a
* android-mydevice

To only see local branches, omit the -a flag.

Building the Kernel

To build the kernel, execute:

% make -j4

Build Variants

When building for a particular product, it’s often useful to have minor variations on what is ultimately the final release build. These are the currently-defined build variants:

eng This is the default flavor. A plain make is thesame as make eng.Installs modules tagged with: eng, debug,user, and/or development.Installs non-APK modules that have no tags specified.Installs APKs according to the product definition files, inaddition to tagged is enabled by default.
user make userThis is the flavor intended to be the final release bits.Installs modules tagged with user.Installs non-APK modules that have no tags specified.Installs APKs according to the product definition files; tagsare ignored for APK is disabled by default.
userdebug make userdebugThe same as user, except:Also installs modules tagged with is enabled by default.

If you build one flavor and then want to build another, you should run make installclean between the two makes to guarantee that you don’t pick up files installed by the previous flavor. makeclean will also suffice, but it takes a lot longer.


PHP 动态生成图片


putenv("GDFONTPATH=" . realpath('.'));
$font = "AnkeCalligraph.ttf";
$size = isset($_GET['size']) ? $_GET['size'] : 20;
$text = isset($_GET['text']) ? $_GET['text'] : "";

$image = imagecreatefrompng("2.png");
if(!$image) {
    $image = imagecreatetruecolor(150, 150);
    $bgc = imagecolorallocate($image, 255, 255, 255);
    $tc = imagecolorallocate($image, 0, 0, 0);
    imagefilledrectangle($image, 0, 0, 150, 30, $bgc);
    imagestring($image, 1, 5, 5, "Error loading", $tc);
$red = imagecolorallocate($image, 0xFF, 0, 0);
if($text) {
    $tsize = imagettfbbox($size, 0, $font, $text);
    $dx = abs($tsize[2] - $tsize[0]);
    $dy = abs($tsize[5] - $tsize[3]);
    $x = (imagesx($image) - $dx) / 2;
    $y = (imagesy($image) - $dy) / 2 + $dy;
    //绘制文字,这里使用的是AnkeCalligraph TTF字体
    imagettftext($image, $size, 0, $x, $y, $red, $font, $text);


Request the Image

<img src="dynamicImage.php?text=<?= urldecode("Hello PHP")?>" alt="dynamicImage"/>

Substitution failure is not an error

SFINAE(Substitution failure is not an error)是C++范型编程中的概念。 对SFINAE简单的理解是,当编译器发现最佳模版匹配失败的时候,不报错,而是继续寻找第二匹配。 在Java中我们可以通过反射机制来判断某个类中是否包含了某个方法,但是C++没有反射,但可以通过SFINAE来达到同样的目的。 WIKI中提供了一个非常简洁的代码片段来演示SFINAE:

struct Test {
    typedef int foo;
template <typename T> 
void f(typename T::foo) {} // Definition #1
template <typename T> 
void f(T) {}                // Definition #2
int main() {
    f<Test>(10); // Call #1.
    f<int>(10);  // Call #2. Without error (even though there is no int::foo) thanks to SFINAE.

我们看到第2个调用:f(10)中的模板参数为int,编译器为它匹配了Definition #2。


#include <iostream>
template <typename T>
struct has_typedef_foobar {
    // Types "yes" and "no" are guaranteed to have different sizes,
    // specifically sizeof(yes) == 1 and sizeof(no) == 2.
    typedef char yes[1];
    typedef char no[2];
    template <typename C>
    static yes& test(typename C::foobar*);
    template <typename>
    static no& test(...);
    // If the "sizeof" of the result of calling test<T>(0) would be equal to sizeof(yes),
    // the first overload worked and T has a nested type named foobar.
    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
struct foo {    
    typedef float foobar;
int main() {
    std::cout << std::boolalpha;
    std::cout << has_typedef_foobar<int>::value << std::endl;
    std::cout << has_typedef_foobar<foo>::value << std::endl;


VirtualBox 使用摄像头

OS: OSX 10.10.1

VirtualBOX: 4.3.20

近期在OSX上使用VirtualBox安装了一个Win7用来测试一个摄像头程序,但是发现VirtualBox本身是不支持使用HOST机的摄像头设备的,需要VirtualBox Extension Pack的支持才可以。 可以在这里下载对应ViretualBox版本的ExtensionPack,点击安装成功后,可以看到如下界面: Alt none

进入系统后即可在Devices -> Webcams中找到你的视频设备

PHP Trait & Resolve Conflict


        trait Logger {

            public function log($logString) {
                $className = __CLASS__;
                echo date("Y-m-d:h:i:s", time()) . ": [{$className}] {$logString} <br />";


        class User {

            use Logger;

            function __construct($name = '') {
                $this->name = $name;
                $this->log("Create user '{$this->name}'");

            function __toString() {
                return $this->name;


        Class UserGroup {

            use Logger;

            private $users = array();

            public function addUser(User $user) {
                if(!$this->includesUser($user)) {
                    $this->users[] = $user;
                    $this->log("Added user '{$user}' to group");

            private function includesUser(User $user) {
                return array_search($user, $this->users, true);
            public function showAll() {
                foreach($this->users as $name) {
                    echo "{$name} <br />";
        $group = new UserGroup;
        $group->addUser(new User("lnmcc"));
        $group->addUser(new User("wangsijie"));


2014-11-29:04:49:23: [User] Create user 'lnmcc' 
2014-11-29:04:49:23: [UserGroup] Added user 'lnmcc' to group 
2014-11-29:04:49:23: [User] Create user 'wangsijie' 
2014-11-29:04:49:23: [UserGroup] Added user 'wangsijie' to group 

Resolve Conflicts

Use the as keyword to alias a trait’s method name.

        trait Command1 {
            function run() {
                echo "executing command1 ... <br />";
        trait Command2 {
            function run() {
                echo "executing command2 ... <br />";
        class TestCommand {
            use Command1, Command2 {
                Command1::run as Command1Run;
                Command2::run insteadof Command1;
        $test = new TestCommand();


executing command2 ... 
executing command1 ...