本文源文档地址:http://source.android.com/porting/build_system.html Android使用一个可定制的编译系统来生成工具、二进制文件和文档。本文档简单介绍了这个编译系统,并做一个简单编译的例子。 Android的编译系统基于MAKE,并需要一个较新版本的GNU MAKE,你可以通过make -v来检查你机器上的MAKE程序的版本号,确保它高于或等于3.80。 一、理解makefile 一个makefile定义了怎么样编译一个特定的应用程序,一般包含下面几个元素: 1、名字:你要编译的模块的名字(LOCAL_MODULE := <build_name>) 2、本地变量:清除本地变量(include $(CLEAR_VARS)) 3、文件:列出你的程序依赖的文件(LOCAL_SRC_FILES := main.c) 4、标记:如果必要的话,定义标记(LOCAL_MODULE_TAGS := eng development) 5、库:定义你的程序依赖的库(LOCAL_SHARED_LIBRARIES := cutils) 6、模板文件:调用一个模板文件来编译特定目标(include $(BUILD_EXECUTABLE)) 下面演示了一个典型的makefile LOCAL_PATH := $(my-dir) include $(CLEAR_VARS) LOCAL_MODULE := <buil_name> LOCAL_SRC_FILES := main.c LOCAL_MODULE_TAGS := eng development LOCAL_SHARED_LIBRARIES := cutils include $(BUILD_EXECUTABLE) (HOST_)EXECUTABLE, (HOST_)JAVA_LIBRARY, (HOST_)PREBUILT, (HOST_)SHARED_LIBRARY, (HOST_)STATIC_LIBRARY, PACKAGE, JAVADOC, RAW_EXECUTABLE, RAW_STATIC_LIBRARY, COPY_HEADERS, KEY_CHAR_MAP 二、抽象层次 下面的表格描述了编译系统中的抽象层次。 每一层与它的上一层都是一对多的关系。如,一个arch可以有多个board,一个board可以有多个device。每一层中,你可以定义多个不同特征的元素,可以使用一样的代码支持多个不同的需求。
Layer | Example | Description |
---|---|---|
Product | myProduct,myProduct_eu, myProduct_eu_fr, j2, sdk | The product layerdefines a complete specification of a shipping product, definingwhich modules to build and how to configure them. You might offera device in several different versions based on locale, forexample, or on features such as a camera. 产品层定义了一个商业产品的完整特征,比如编译哪个模块,怎么配置它们。 |
Device | myDevice,myDevice_eu, myDevice_eu_lite | The device layerrepresents the physical layer of plastic on the device. Forexample, North American devices probably include QWERTY keyboardswhereas devices sold in France probably include AZERTY keyboards.Peripherals typically connect to the device layer. 设备层代表设备的物理层。比如,北美的设备包含一个QWERTY键盘,而法国的设备有一个AZERTY键盘。 |
Board | sardine, trout,goldfish | The board layerrepresents the bare schematics of a product. You may still connectperipherals to the board layer. |
Arch | arm (arm5te)(arm6), x86, 68k | The arch layerdescribes the processor running on your board. 这个层次描述了运行在board上的处理器。 |
三、编译Android平台 本节描述了如何编译Android的默认版本。你如果学会怎么编译通用版本,就很容易知道怎么修改自己的定制版本了。 1、设备代码 首先,运行一下build/envsetup.sh这个脚本,这里面定义了一些必要的变量和函数。 % cd $TOP % . build/envsetup.sh # pick a configuration using choosecombo % choosecombo % make -j4 PRODUCT-generic-user 你也可以用eng来代替user。 % make -j4 PRODUCT-generic-eng 2、清除 执行% m clean
清除你创建的二进制文件,你也可以执行% m clobber
来清除所有的二进制文件。后面这个命令相当于删除out目录。 3、加速重编译 有时,你需要重新编译整个系统,这时,应该定义USE_CCACHE环境变量: % export USE_CCACHE=1 这样做,会强制编译系统ccache编译缓存工具,这个工具可以减少重编译的时间。ccache这个二进制文件在//prebuilt/...下,你自己的系统中不需要安装。 4、常见问题 下面的错误很可能是由于JAVA版本过时引起的。 device Dex: core UNEXPECTED TOP-LEVEL ERROR: java.lang.NoSuchMethodError: method java.util.Arrays.hashCode with signature ([Ljava.lang.Object;)I was not found. at com.google.util.FixedSizeList.hashCode(FixedSizeList.java:66) at com.google.rop.code.Rop.hashCode(Rop.java:245) at java.util.HashMap.hash(libgcj.so.7) [...] dx是一个JAVA程序,它使用JAVA1.5编译的,确保你的JAVA版本高于1.5。 四、编译Android的内核 本节介绍了怎么编译Android的默认内核。你如果学会编译通用的内核,自然也就会定制了。 先进入到设备目录(/home/joe/android/device)来建立变量: % . build/envsetup.sh % partner_setup generic 再进入到内核目录/home/joe/android/kernel
.
CheckingOut a Branch
Thedefault 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"
Tosimplify code management, give your local branch the same name as theremote branch it is tracking (as illustrated in the snippet above).Switch between branches by executing %git checkout <branchname>
.
VerifyingLocation
Findout which branches exist (both locally and remotely) and which one isactive (marked with an asterisk) by executing the following:
% git branch -a android * android-mydevice origin/HEAD origin/android origin/android-mydevice origin/android-mychipset
Toonly see local branches, omit the -a
flag.
编译内核时,运行% make -j4 五、编译变量
| This is thedefault flavor. A plain
|
|
This is theflavor intended to be the final release bits.
|
|
The same as
|
如果编译完一种风格后,再编译另外一种的话,应该在两次make之间运行 make installclean
来保证不会冲突。也可以运行make clean,但再编译时需要的时间更长一些。