本文源文档地址:http://source.android.com/porting/build_system.htmlAndroid使用一个可定制的编译系统来生成工具、二进制文件和文档。本文档简单介绍了这个编译系统,并做一个简单编译的例子。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))下面演示了一个典型的makefileLOCAL_PATH := $(my-dir)include $(CLEAR_VARS)LOCAL_MODULE := <buil_name>LOCAL_SRC_FILES := main.cLOCAL_MODULE_TAGS := eng developmentLOCAL_SHARED_LIBRARIES := cutilsinclude $(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-eng2、清除执行% 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 withsignature ([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 alwaysandroid.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-aflag.

编译内核时,运行% make -j4五、编译变量

eng

This is thedefault flavor. A plainmakeisthe same asmakeeng.

  • Installsmodules tagged with:eng,debug,user,and/ordevelopment.

  • Installsnon-APK modules that have no tags specified.

  • InstallsAPKs according to the product definition files, in addition totagged APKs.

  • ro.secure=0

  • ro.debuggable=1

  • ro.kernel.android.checkjni=1

  • adbisenabled by default.

  • 这个是默认风格。只执行make相当于执行makeeng

  • 安装标记为eng,debug,user的模块和development

  • 安装没有标记的非APK模块。

  • 安装产品定义文件中描述的APK和标记过的APK

  • adb默认打开。

user

makeuser

This is theflavor intended to be the final release bits.

  • Installsmodules tagged withuser.

  • Installsnon-APK modules that have no tags specified.

  • InstallsAPKs according to the product definition files; tags are ignoredfor APK modules.

  • ro.secure=1

  • ro.debuggable=0

  • adbisdisabled by default.

  • 发布版使用的风格。

  • 安装标记为user的模块。

  • 安装没有标记的非APK模块。

  • 安装产品定义文件中定义的APK,对APK来说,标记被忽略

  • adb默认关闭。

userdebug

makeuserdebug

The same asuser,except:

  • Alsoinstalls modules tagged withdebug.

  • ro.debuggable=1

  • adbisenabled by default.

  • user基本相同,除了以下几点:

  • 安装标记为debug的模块。

  • adb默认打开。

如果编译完一种风格后,再编译另外一种的话,应该在两次make之间运行make installclean来保证不会冲突。也可以运行make clean,但再编译时需要的时间更长一些。

更多相关文章

  1. Android(安卓)数据操作(一) 自定义AttributeSet属性
  2. Android(安卓)项目中文件夹作用(res文件夹详细介绍)
  3. Android(安卓)自定义View -- 简约的折线图
  4. [置顶] android 自定义Toast,可设定显示时间
  5. Android自定义动画类——实现3D旋转动画
  6. Android中PreferenceFregment使用完全攻略
  7. android 自定义spinner的下拉弹出窗口样式
  8. Android笔记-自定义适配器
  9. android tabhost 自定义标题 you cannot combine custom titles

随机推荐

  1. android service
  2. android启动加速
  3. 如何在电脑上安装模拟器体验Android(安卓
  4. 错误ava.lang.RuntimeException: Unable
  5. Android(安卓)蓝牙开发
  6. android 常见面试题以及答案
  7. Android近场通信---NFC基础(五)
  8. Android(安卓)模拟器 GPU ON
  9. 关于使用 Android(安卓)MVVM + LiveData
  10. android lib 发布到jcenter