(一)、搭建编译环境



1. 查看linux编译环境软件版本

1.1 python版本
$ python -V


1.2 make版本
$ make -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

这个程序创建为 x86_64-pc-linux-gnu


1.4 GIT

上面3个都是系统自带的,git没有随系统安装。安装命令 sudo apt-get intall git



2 安装JDK

安装与配置,详见


查看版本

$ java -version

java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01, mixed mode)




3. 安装需求包

$ sudo apt-get install git-core gnupg flex bison gperf build-essential \  zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \  libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \  libgl1-mesa-dev g++-multilib mingw32 openjdk-6-jdk tofrodos \  python-markdown libxml2-utils xsltproc zlib1g-dev:i386$ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
直接copy过来代码,在命令行中执行,总是不能按预期的那样当成一条命令执行,干脆直接把文档的布局和内容都copy过来

如果出现错误:

在处理时有错误发生:
libc6-dev:i386
E: Sub-process /usr/bin/dpkg returned an error code (1)

执行以下命令
sudo apt-get -f install


4. 配置USB访问 详见


5. 配置ccahe

用于提高编译速度,也可以不安装



(二)、下载4.1.2源码



创建所需目录

$ mkdir ~/bin$ PATH=~/bin:$PATH


一、下载repo工具,需使用代理(我使用的是goagent


$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo$ chmod a+x ~/bin/repo

白天不行,晚上尽然就可以正常下载 。。。。
curl https://dl-ssl.google.com/dl/googlesource/git-repo   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100 20774  100 20774    0     0  20765      0  0:00:01  0:00:01 --:--:-- 32870


二、 配置repo 1. 创建本地保存Android源码目录
$ mkdir WORKING_DIRECTORY$ cd WORKING_DIRECTORY

2. repo初始化 选择下载那个版本的源码,打开以下网址可以看到Branches 先测试以下,如果上面的不行,可以使用下面的(后续命令也得改) https://android.googlesource.com/platform/manifest
通过以下命令进行初始化,其中最后一个参数是从上一个网址中自己根据相应版本获取的Branches名称 $ repo init -u https://android.googlesource.com/platform/manifest -b android-4.1.2_r1
需要输入用户名 和 邮箱地址
$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.1.2_r1.repo/manifests/: discarding 10 commitsYour Name  [YourName]: YourNameYour Email [YourName@YourName]: YourEmailAddress@mail.comYour identity is: YourName <YourEmailAddress@mail.com>is this correct [y/N]? yTesting colorized output (for 'repo diff', 'repo status'):  black    red      green    yellow   blue     magenta   cyan     white   bold     dim      ul       reverse Enable color display in this user account (y/N)? yrepo initialized in /home/YourName/WORKING_DIRECTORY



三、 下载源码
可以先设置代理
$ repo sync

温馨提示,可能需要几个小时


* repo配置使用代理 $ export HTTP_PROXY=http://<proxy_user_id>:<proxy_password>@<proxy_server>:<proxy_port>
$ export HTTPS_PROXY=http://<proxy_user_id>:<proxy_password>@<proxy_server>:<proxy_port>
* 网络设置,通常不需要使用 $ sudo sysctl -w net.ipv4.tcp_window_scaling=0
$ repo sync -j1

* 使用本地镜像
* 检验Git Tags

参考资料 android 官网文档 : Downloading the Source Tree


(三)下载Android源码遇到问题



昨天晚上下载一夜,到今天早上还是没有完全下载完,而且还遇到了问题

From https://android.googlesource.com/platform/prebuilt * [new branch]      ics-plus-aosp -> aosp/ics-plus-aosp   f37f794..f6e5b79  jb-dev     -> aosp/jb-dev * [new branch]      jb-mr0-release -> aosp/jb-mr0-release   f787b1d..1e1589a  jb-release -> aosp/jb-release * [new tag]         android-4.1.1_r1 -> android-4.1.1_r1 * [new tag]         android-4.1.1_r1.1 -> android-4.1.1_r1.1 * [new tag]         android-4.1.1_r2 -> android-4.1.1_r2 * [new tag]         android-4.1.1_r3 -> android-4.1.1_r3 * [new tag]         android-4.1.1_r4 -> android-4.1.1_r4 * [new tag]         android-4.1.1_r5 -> android-4.1.1_r5 * [new tag]         android-4.1.1_r6 -> android-4.1.1_r6 * [new tag]         android-4.1.1_r6.1 -> android-4.1.1_r6.1 * [new tag]         android-4.1.2_r1 -> android-4.1.2_r1 * [new tag]         android-cts-4.1_r1 -> android-cts-4.1_r1Fetching projects:  98% (288/293)  error: Cannot fetch device/samsung/cresporemote: Counting objects: 30, doneremote: Finding sources: 100% (30/30)eceiving objects:  84% (4900/5821), 59.80 MiB | 389 KiB/s 

到98%卡住了


之后终端,再次执行repo sync


error: RPC failed; result=56, HTTP code = 0fatal: The remote end hung up unexpectedly

error: RPC failed; result=35, HTTP code = 0iB | 103 KiB/s   fatal: The remote end hung up unexpectedlyerror: RPC failed; result=7, HTTP code = 0fatal: The remote end hung up unexpectedlyerror: Failed to connect to 2404:6800:4008:c01::52: Network is unreachable while accessing https://android.googlesource.com/platform/frameworks/compile/libbcc/info/refsfatal: HTTP request failederror: Failed to connect to 2404:6800:4008:c01::52: Network is unreachable while accessing https://android.googlesource.com/platform/frameworks/compile/linkloader/info/refsfatal: HTTP request failed

在网上查到一个脚本,每次出错了,重复执行repo sync命令


#!/bin/bashecho "======start repo sync======"repo syncwhile [ $? == 1 ]; doecho "======sync failed, re-sync again======"sleep 3repo syncdone



看看明天是否下载完 奋斗



哈哈,早上一起床下载好了,看来以后很有必要学习下shell


2012-12-09 再次下载源码

From https://android.googlesource.com/platform/tools/motodev * [new tag]         android-4.2.1_r1 -> android-4.2.1_r1 * [new tag]         android-4.2_r1 -> android-4.2_r1Fetching projects: 100% (329/329), done.  Checking out files: 100% (9390/9390), done.out files:  16% (1546/9390)   Checking out files: 100% (6764/6764), done.out files:   1% (88/6764)   Checking out files: 100% (24607/24607), done.ut files:  50% (12308/24607)   Checking out files: 100% (18696/18696), done.ut files:   0% (20/18696)   Checking out files: 100% (395/395), done.ng out files:  21% (84/395)   Checking out files: 100% (943/943), done.ng out files:  42% (401/943)   Checking out files: 100% (431/431), done.ng out files:  19% (86/431)   Checking out files: 100% (175/175), done.ng out files:  41% (73/175)   Checking out files: 100% (378/378), done.Checking out files: 100% (433/433), done.Checking out files: 100% (2407/2407), done. out files:  46% (1115/2407)   Checking out files: 100% (2489/2489), done.Checking out files: 100% (2493/2493), done.Checking out files: 100% (177/177), done.ng out files:  49% (88/177)   Checking out files: 100% (40775/40775), done.ut files:  13% (5323/40775)   Checking out files: 100% (93/93), done.Checking out files: 100% (450/450), done.Syncing work tree: 100% (329/329), done.  


原文地址:http://blog.csdn.net/love_world_/article/details/8079124


(四)编译Android 源码



一、使用高速的C/C++编译工具 ccache ccache(“compiler cache”的缩写 官方网站 )是一个编译器缓存,该工具会高速缓存编译生成的信息,并在编译的特定部分使用高速缓存的信息, 比如头文件,这样就节省了通常使用 cpp 解析这些信息所需要的时间。如果您编译清单 2 中的文件,假定 foobar.h 中包含对其他头文件的引用,ccache 会用那个文件的 cpp-parsed 版本来 取代 include 声明。就那么简单。不是真正去读取、理解并解释其内容,ccache 只是 将最终的文本拷贝到文件中,使得它可以立即被编译。
如果不需要再次编译Android可以不配置ccache,初次编译使用ccache会减低些速度,但是要是重新编译的会会提升5~10倍速度。 如果想了解更多使用,可以参考 ccache使用教程
1 ccache安装
sudo apt-get install ccache

2 ccache配置
编辑.bashrc添加以下配置$ export USE_CCACHE=1$ export CCACHE_DIR=/<path_of_your_choice>/.ccache$ ccache -M 20G

3 可以观察ccache都做了什么
$ watch -n1 -d prebuilt/linux-x86/ccache/ccache -s



二、初始化系统编译环境
$ cd WORKING_DIRECTORY/$ source build/envsetup.shincluding device/asus/grouper/vendorsetup.shincluding device/generic/armv7-a-neon/vendorsetup.shincluding device/generic/armv7-a/vendorsetup.shincluding device/moto/wingray/vendorsetup.shincluding device/samsung/crespo/vendorsetup.shincluding device/samsung/maguro/vendorsetup.shincluding device/ti/panda/vendorsetup.shincluding sdk/bash_completion/adb.bash

envsetup.sh参数解释: http://blog.csdn.net/changjiang654/article/details/6752324
envsetup.sh文件shell源码分析 http://www.cnblogs.com/cnhome/archive/2010/07/24/1784382.html
或者 $ . build/envsetup.sh

三、选择目标平台
$ lunch full-eng============================================PLATFORM_VERSION_CODENAME=RELPLATFORM_VERSION=4.1.2TARGET_PRODUCT=fullTARGET_BUILD_VARIANT=engTARGET_BUILD_TYPE=releaseTARGET_BUILD_APPS=TARGET_ARCH=armTARGET_ARCH_VARIANT=armv7-aHOST_ARCH=x86HOST_OS=linuxHOST_OS_EXTRA=Linux-3.2.0-29-generic-x86_64-with-Ubuntu-12.04-preciseHOST_BUILD_TYPE=releaseBUILD_ID=JZO54KOUT_DIR=out============================================



参数解释 build name:
Build name Device Notes
full emulator fully configured with all languages, apps, input methods
full_maguro maguro fullbuild running on Galaxy Nexus GSM/HSPA+ ("maguro")
full_panda panda fullbuild running on PandaBoard ("panda")


build type:
Buildtype Use
user limited access; suited for production
userdebug like "user" but with root access and debuggability; preferred for debugging
eng development configuration with additional debugging tools



四、编译Android代码
白天上班之前使用make -j16 结果,编译过程中终端卡死,只能重新编译了
如果直接使用make命令编译会比较慢,可以通过参数 -j 来提高编译速度
make -j参数解释: “-j []”
“–jobs[=]”
指同时运行命令的个数。如果没有这个参数,make运行命令时能运行多少就运行多少。 同时运行命令的个数可以是cpu核数的1~2倍。例如我的是8核CPU, 执行
$ make -j 8


参考文章: http://blog.csdn.net/wangjingfei/article/details/4969942






参考资料 Android官方文档: Building the System
老罗资料: 在Ubuntu上下载、编译和安装Android最新源代码 http://blog.csdn.net/luoshengyang/article/details/6559955 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel) http://blog.csdn.net/luoshengyang/article/details/6564592 如何单独编译Android源代码中的模块 http://blog.csdn.net/luoshengyang/article/details/6566662
* make -j参数相关: http://blog.csdn.net/wangjingfei/article/details/4969942
* ccache相关: ccache使用教程 ccache官方网站



2014-03-13 整合

《编译Android源码 - 搭建编译环境》

《编译Android源码-下载源码》

《编译Andorid源码-下载遇到问题》

《编译Android源码》





一、使用高速的C/C++编译工具 ccache ccache(“compiler cache”的缩写 官方网站 )是一个编译器缓存,该工具会高速缓存编译生成的信息,并在编译的特定部分使用高速缓存的信息, 比如头文件,这样就节省了通常使用 cpp 解析这些信息所需要的时间。如果您编译清单 2 中的文件,假定 foobar.h 中包含对其他头文件的引用,ccache 会用那个文件的 cpp-parsed 版本来 取代 include 声明。就那么简单。不是真正去读取、理解并解释其内容,ccache 只是 将最终的文本拷贝到文件中,使得它可以立即被编译。
如果不需要再次编译Android可以不配置ccache,初次编译使用ccache会减低些速度,但是要是重新编译的会会提升5~10倍速度。 如果想了解更多使用,可以参考 ccache使用教程
1 ccache安装
sudo apt-get install ccache

2 ccache配置
编辑.bashrc添加以下配置$ export USE_CCACHE=1$ export CCACHE_DIR=/<path_of_your_choice>/.ccache$ ccache -M 20G

3 可以观察ccache都做了什么
$ watch -n1 -d prebuilt/linux-x86/ccache/ccache -s



二、初始化系统编译环境
$ cd WORKING_DIRECTORY/$ source build/envsetup.shincluding device/asus/grouper/vendorsetup.shincluding device/generic/armv7-a-neon/vendorsetup.shincluding device/generic/armv7-a/vendorsetup.shincluding device/moto/wingray/vendorsetup.shincluding device/samsung/crespo/vendorsetup.shincluding device/samsung/maguro/vendorsetup.shincluding device/ti/panda/vendorsetup.shincluding sdk/bash_completion/adb.bash

envsetup.sh参数解释: http://blog.csdn.net/changjiang654/article/details/6752324
envsetup.sh文件shell源码分析 http://www.cnblogs.com/cnhome/archive/2010/07/24/1784382.html
或者 $ . build/envsetup.sh

三、选择目标平台
$ lunch full-eng============================================PLATFORM_VERSION_CODENAME=RELPLATFORM_VERSION=4.1.2TARGET_PRODUCT=fullTARGET_BUILD_VARIANT=engTARGET_BUILD_TYPE=releaseTARGET_BUILD_APPS=TARGET_ARCH=armTARGET_ARCH_VARIANT=armv7-aHOST_ARCH=x86HOST_OS=linuxHOST_OS_EXTRA=Linux-3.2.0-29-generic-x86_64-with-Ubuntu-12.04-preciseHOST_BUILD_TYPE=releaseBUILD_ID=JZO54KOUT_DIR=out============================================



参数解释 build name:
Build name Device Notes
full emulator fully configured with all languages, apps, input methods
full_maguro maguro fullbuild running on Galaxy Nexus GSM/HSPA+ ("maguro")
full_panda panda fullbuild running on PandaBoard ("panda")


build type:
Buildtype Use
user limited access; suited for production
userdebug like "user" but with root access and debuggability; preferred for debugging
eng development configuration with additional debugging tools



四、编译Android代码
白天上班之前使用make -j16 结果,编译过程中终端卡死,只能重新编译了
如果直接使用make命令编译会比较慢,可以通过参数 -j 来提高编译速度
make -j参数解释: “-j []”
“–jobs[=]”
指同时运行命令的个数。如果没有这个参数,make运行命令时能运行多少就运行多少。 同时运行命令的个数可以是cpu核数的1~2倍。例如我的是8核CPU, 执行
$ make -j 8


参考文章: http://blog.csdn.net/wangjingfei/article/details/4969942

下载、编译官方Android 4.1.2 源码_第1张图片




参考资料 Android官方文档: Building the System
老罗资料: 在Ubuntu上下载、编译和安装Android最新源代码 http://blog.csdn.net/luoshengyang/article/details/6559955 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel) http://blog.csdn.net/luoshengyang/article/details/6564592 如何单独编译Android源代码中的模块 http://blog.csdn.net/luoshengyang/article/details/6566662
* make -j参数相关: http://blog.csdn.net/wangjingfei/article/details/4969942
* ccache相关: ccache使用教程 ccache官方网站

更多相关文章

  1. android 事件机制图文详解-从源码角度分析彻底理解事件传递机制(上
  2. Eclipse与Android源码中ProGuard工具的使用
  3. 从源码角度分析Android中的Binder机制的前因后果
  4. android精仿今日头条源码,饿了么点餐源码等
  5. 在android中通过java层程序调用命令行的一些备注

随机推荐

  1. Android Shape Drawable 静态使用和动态
  2. Android自学笔记1——基础知识
  3. Android系统资源
  4. Android 3 开发环境搭建
  5. Android 的系统属性(SystemProperties)分
  6. Android的BroadcastReceiver简介
  7. android中的计步问题及计步传感器分析
  8. Android开发的未来发展方向,难道android真
  9. Android日记之2012\01\13
  10. Android绘图之LinearGradient线性渐变(9)