最近买了个Nexus 5 手机准备学习android4.4.4源码。Nexus 5 刷入从第三方下载的ROM后,总是推送各种第三方APP,于是自己打算下载编译一次官方的源码。
关于Android各个版本的编译环境的搭建,大家可以参考我去年翻译的Android 官方文档http://blog.csdn.net/u012417380/article/details/51472860。我目前打算用VM虚拟机编译Android 4.4.4 版本,它官方要求的具体环境是:
硬件环境:
注:上述环境是官方以最新版本为要求的环境。我打算用VM 虚拟机运行Lunix编译,目前的方案是 动态分配硬盘大小为150G,内存分配 4GB,交换空间分配8GB。
软件环境
1.操作系统
Linux:
Android 2.3.x (Gingerbread) - Android 5.x (Lollipop): Ubuntu 12.04 (Precise)
2. Java Development Kit (JDK)
Android 2.3.x (Gingerbread) - Android 4.4.x (KitKat): Ubuntu - Java JDK 6, Mac OS - Java JDK 6
3. Key packages
Python 2.6 – 2.7 下载官网 python.org
GNU Make 3.81 – 3.82 下载官网:gnu.org ; Android 4.0.x (Ice Cream Sandwich) 以及之前的版本需要 使用 maker 3.81 来避免编译错误。
Git 1.7 + 下载官网 git-scm.com
虚拟机:动态分配硬盘大小为150G,内存分配 4GB,交换空间分配8GB。
操作系统:ubuntu-12.04.5-desktop-amd64.iso
安装成功后安装VM Tools,这里不介绍
jdk版本:jdk-6u45-linux-x64.bin
详细安装及环境变量配置参考我这篇博文: Ubuntu 下JDK的安装与环境配置
用Ubuntu12.04来编译安卓更旧的版本时,安装下面的安装包:
$ sudo apt-get install git 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 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
如果在乌班图系统下编译后,并用USB数据线进行对NEXUS 5 刷机,那么默认情况下普通用户无法直接使用 USB 设备。需要以root用户身份在‘/etc/udev/rules.d/51-android.rules’下创建一个文件。
为此,请运行以下命令来下载本网站附带的 51-android.txt 文件,对其进行修改以包含您的用户名,然后将其放到正确位置
//51-android.txt
# adb protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER=""
# fastboot protocol on passion (Nexus One)
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER=""
# adb protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER=""
# fastboot protocol on crespo/crespo4g (Nexus S)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER=""
# adb protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="70a9", MODE="0600", OWNER=""
# fastboot protocol on stingray/wingray (Xoom)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="708c", MODE="0600", OWNER=""
# adb protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER=""
# fastboot protocol on maguro/toro (Galaxy Nexus)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER=""
# adb protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d101", MODE="0600", OWNER=""
# adb protocol on panda (PandaBoard ES)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="d002", MODE="0600", OWNER=""
# fastboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d022", MODE="0600", OWNER=""
# usbboot protocol on panda (PandaBoard)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d00f", MODE="0600", OWNER=""
# usbboot protocol on panda (PandaBoard ES)
SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d010", MODE="0600", OWNER=""
# adb protocol on grouper/tilapia (Nexus 7)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e42", MODE="0600", OWNER=""
# fastboot protocol on grouper/tilapia (Nexus 7)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e40", MODE="0600", OWNER=""
# adb protocol on manta (Nexus 10)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee2", MODE="0600", OWNER=""
# fastboot protocol on manta (Nexus 10)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee0", MODE="0600", OWNER=""
# adb protocol on hammerhead (Nexus 5)
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee1", MODE="0600", OWNER=""
下面的的命令会自动下载51-android.txt
文件,对文件中
字段替换成你的用户名,并将文件放在/etc/udev/rules.d/51-android.rules
文件文件中
$ wget -S -O - http://source.android.com/source/51-android.txt | sed "s//$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules
$sudo udevadm control --reload-rules
如果显示拒绝连接无法下载的话,将51-android.txt文件放在本地目录任意一目录,在该目录中执行下面的命令。
$ cat 51-android.txt | sed "s//$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules
$ sudo udevadm control --reload-rules
执行完后,为了验证NEXUS 5 与乌班图是否连接,将nexus 5的USB计算机连接方式设置为相机,可以看到乌班图能够识别设备了。
默认情况下,每次编译的输出都会存储在相应源代码树的 out/ 子目录下。如果将源文件和输出存储在单独的存储卷中,编译速度会更快。
要进行这项设置,请导出 OUT_DIR_COMMON_BASE 变量,使其指向将存储输出目录的位置。
$ export OUT_DIR_COMMON_BASE=<path-to-your-out-directory>
对于每个单独的源代码树,其输出目录都将以其存放目录命名。
例如,如果您有源代码树 /source/master1 和 /source/master2,并且 OUT_DIR_COMMON_BASE 设为了 /output,那么输出目录将为 /output/master1 和 /output/master2。这个过程后续会验证
安卓系统源码放在git仓库中
repo 是安卓环境下的命令行工具,对Git部分命令进行封装,将 百多个git库进行更新。
按照如下步骤进行安装Repo.
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
当你安装完Repo后,设置安卓源码仓库的客户端:
$ mkdir WORKING_DIRECTORY
$ cd WORKING_DIRECTORY
由于我下载的版本是 Android4.4.4_r1所以将WORKING_DIRECTORY替换成Android444_r1:
即:
aosp444@aosp444-virtual-machine:~$ mkdir Android444_r1
aosp444@aosp444-virtual-machine:~$ cd Android444_r1
aosp444@aosp444-virtual-machine:~/Android444_r1$
Android4.4.4_r1
的源码:$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.4.4_r1
由于google站无法访问,所以我选择了清华的下载源,用下面的命令下载
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-4.4.4_r1
repo init 后,会提示你输入名字和邮箱, 两个我都是直接按的回车键,这时它默认将系统用户名和主机名 作为用户名和邮箱,接着我直接按了y,提示repo初始完。
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
$ repo sync
接着就是漫长的等待,如果中间出现中断,只要重复该命令即可。
下载成功后提示:
Fetching projects: 100% (413/413), done.
Checking out files: 100% (1179/1179), done.
Checking out files: 100% (2776/2776), done.ut files: 50% (1390/2776)
Checking out files: 100% (44267/44267), done. files: 0% (441/44267)
Checking out files: 100% (3628/3628), done.ut files: 35% (1305/3628)
Checking out files: 100% (6683/6683), done.out files: 8% (568/6683)
Checking out files: 100% (2438/2438), done.out files: 40% (976/2438)
Checking out files: 100% (9389/9389), done.out files: 29% (2742/9389)
Checking out files: 100% (50822/50822), done.t files: 4% (2060/50822)
Checking out files: 100% (140/140), done.
Checking out files: 100% (10083/10083), done.t files: 13% (1340/10083)
Checking out files: 100% (4307/4307), done.out files: 1% (64/4307)
Checking out files: 100% (3494/3494), done.out files: 12% (422/3494)
Checking out files: 100% (7954/7954), done.
Checking out files: 100% (580/580), done.g out files: 25% (145/580)
Checking out files: 100% (1056/1056), done. out files: 48% (510/1056)
Checking out files: 100% (11685/11685), done.ut files: 4% (578/11685)
Checking out files: 100% (4093/4093), done. out files: 13% (546/4093)
Checking out files: 100% (2501/2501), done. out files: 13% (336/2501)
Checking out files: 100% (2619/2619), done. out files: 42% (1107/2619)
Checking out files: 100% (1654/1654), done. out files: 10% (176/1654)
Checking out files: 100% (3924/3924), done.
Checking out files: 100% (2570/2570), done. out files: 30% (790/2570)
Checking out files: 100% (21973/21973), done.
Checking out files: 100% (8597/8597), done. out files: 44% (3802/8597)
Checking out files: 100% (1532/1532), done. out files: 40% (620/1532)
Checking out files: 100% (1690/1690), done. out files: 32% (557/1690)
Checking out files: 100% (376/376), done.ng out files: 9% (37/376)
Checking out files: 100% (108/108), done.
Checking out files: 100% (171/171), done.
Checking out files: 100% (175/175), done.ng out files: 43% (76/175)
Checking out files: 100% (181/181), done.
Checking out files: 100% (135/135), done.
Checking out files: 100% (378/378), done.ng out files: 2% (8/378)
Checking out files: 100% (109/109), done.
Checking out files: 100% (181/181), done.ng out files: 49% (90/181)
Checking out files: 100% (2407/2407), done.
Checking out files: 100% (2482/2482), done.
Checking out files: 100% (2484/2484), done. out files: 0% (21/2484)
Checking out files: 100% (182/182), done.
Checking out files: 100% (132/132), done.ng out files: 47% (63/132)
Checking out files: 100% (50299/50299), done.
Checking out files: 100% (4864/4864), done.
Checking out files: 100% (4383/4383), done.
Checking out files: 100% (28/28), done.king out files: 46% (13/28)
Checking out files: 100% (182/182), done.
Checking out files: 100% (1256/1256), done.
Checking out files: 100% (2951/2951), done. out files: 20% (606/2951)
Syncing work tree: 100% (412/412), done.
这时可看到Android444r1文件夹下checkout出的代码:
checkout出的代码有9.4G
进入隐藏的 .repo目录,查看当前分支大小,有64G呢
如果想check out 其它分支代码,将工作目录下check out出的代码删除,保留隐藏的.repo 目录,重新初始化分支执行上述流程就行,这时会快很多。
总结起来就是太耗时间。进入考试月了,等考完试,计划再实战出 android 4.4.4.r1 系统编译篇、内核编译篇、内核反调试修改编译篇、以及ROM镜像和内核的刷机教程。小白的学习记录过程分享,大神不要喷呀。