(一)、搭建编译环境
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
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 Speed
100 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
先测试以下,如果上面的不行,可以使用下面的(后续命令也得改)
通过以下命令进行初始化,其中最后一个参数是从上一个网址中自己根据相应版本获取的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 commits
Your Name [YourName]: YourName
Your Email [YourName@YourName]: YourEmailAddress@mail.com
Your identity is: YourName <YourEmailAddress@mail.com>
is this correct [y/N]? y
Testing 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)? y
repo 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>
$ 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
$ 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_r1
Fetching projects: 98% (288/293)
error: Cannot fetch device/samsung/crespo
remote: Counting objects: 30, done
remote: 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 = 0
fatal: The remote end hung up unexpectedly
error: RPC failed; result=35, HTTP code = 0iB | 103 KiB/s
fatal: The remote end hung up unexpectedly
error: RPC failed; result=7, HTTP code = 0
fatal: The remote end hung up unexpectedly
error: Failed to connect to 2404:6800:4008:c01::52: Network is unreachable while accessing https://android.googlesource.com/platform/frameworks/compile/libbcc/info/refs
fatal: HTTP request failed
error: Failed to connect to 2404:6800:4008:c01::52: Network is unreachable while accessing https://android.googlesource.com/platform/frameworks/compile/linkloader/info/refs
fatal: HTTP request failed
在网上查到一个脚本,每次出错了,重复执行repo sync命令
#!/bin/bash
echo "======start repo sync======"
repo sync
while [ $? == 1 ]; do
echo "======sync failed, re-sync again======"
sleep 3
repo sync
done
哈哈,早上一起床下载好了,看来以后很有必要学习下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_r1
Fetching 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.sh
including device/asus/grouper/vendorsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/armv7-a/vendorsetup.sh
including device/moto/wingray/vendorsetup.sh
including device/samsung/crespo/vendorsetup.sh
including device/samsung/maguro/vendorsetup.sh
including device/ti/panda/vendorsetup.sh
including sdk/bash_completion/adb.bash
envsetup.sh参数解释:
envsetup.sh文件shell源码分析
或者
$ . build/envsetup.sh
三、选择目标平台
$ lunch full-eng
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.1.2
TARGET_PRODUCT=full
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.2.0-29-generic-x86_64-with-Ubuntu-12.04-precise
HOST_BUILD_TYPE=release
BUILD_ID=JZO54K
OUT_DIR=out
============================================
参数解释
build name:
| Build name | Device | Notes |
|---|---|---|
| full | emulator | fully configured with all languages, apps, input methods |
| full_maguro | maguro | full build running on Galaxy Nexus GSM/HSPA+ ("maguro") |
| full_panda | panda | full build 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运行命令时能运行多少就运行多少。
“–jobs[=]”
指同时运行命令的个数。如果没有这个参数,make运行命令时能运行多少就运行多少。
同时运行命令的个数可以是cpu核数的1~2倍。例如我的是8核CPU, 执行
$ make -j 8
参考文章:
参考资料
Android官方文档:
Building the System
老罗资料:
在Ubuntu上下载、编译和安装Android最新源代码
在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
如何单独编译Android源代码中的模块
* make -j参数相关:
* 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.sh
including device/asus/grouper/vendorsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/armv7-a/vendorsetup.sh
including device/moto/wingray/vendorsetup.sh
including device/samsung/crespo/vendorsetup.sh
including device/samsung/maguro/vendorsetup.sh
including device/ti/panda/vendorsetup.sh
including sdk/bash_completion/adb.bash
envsetup.sh参数解释:
envsetup.sh文件shell源码分析
或者
$ . build/envsetup.sh
三、选择目标平台
$ lunch full-eng
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.1.2
TARGET_PRODUCT=full
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.2.0-29-generic-x86_64-with-Ubuntu-12.04-precise
HOST_BUILD_TYPE=release
BUILD_ID=JZO54K
OUT_DIR=out
============================================
参数解释
build name:
| Build name | Device | Notes |
|---|---|---|
| full | emulator | fully configured with all languages, apps, input methods |
| full_maguro | maguro | full build running on Galaxy Nexus GSM/HSPA+ ("maguro") |
| full_panda | panda | full build 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运行命令时能运行多少就运行多少。
“–jobs[=]”
指同时运行命令的个数。如果没有这个参数,make运行命令时能运行多少就运行多少。
同时运行命令的个数可以是cpu核数的1~2倍。例如我的是8核CPU, 执行
$ make -j 8
参考文章:
参考资料
Android官方文档:
Building the System
老罗资料:
在Ubuntu上下载、编译和安装Android最新源代码
在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
如何单独编译Android源代码中的模块
* make -j参数相关:
* ccache相关:
本文详细介绍了如何搭建编译环境,包括查看和安装必要的软件版本,解决下载源码时遇到的问题,以及如何配置ccache以提升编译速度。在下载源码过程中,作者分享了遇到的挑战和解决方案,最终成功编译Android 4.1.2源码的经验。

1355

被折叠的 条评论
为什么被折叠?



