一、ubuntu 12.04 64位 android 编译环境安装

在闲鱼花300多买了个Nexus 5 手机准备学习android4.4.4源码。Nexus 5 刷入从第三方下载的ROM后,总是推送各种第三方APP,于是自己打算下载编译一次官方的源码。

一、编译环境搭建

1.官方推荐环境介绍

关于Android各个版本的编译环境的搭建,大家可以参考我去年翻译的Android 官方文档http://blog.csdn.net/u012417380/article/details/51472860。我目前打算用VM虚拟机编译Android 4.4.4 版本,它官方要求的具体环境是:
硬件环境

  • 64 位系统环境编译(2.3及2.3系统上都是64位环境)
  • 至少100GB的磁盘空间用于校验。单个版本编译需要150GB磁盘空间,多版本编译需要200GB以上成磁盘空间
  • 如果在虚拟机上运行 Lunix,至少需要 16GB的RAM/swap空间。

注:上述环境是官方以最新版本为要求的环境。我打算用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

2.本地编译环境搭建

1.用VMare 12.0 创建Linux虚拟机。

虚拟机:动态分配硬盘大小为150G,内存分配 4GB,交换空间分配8GB。
操作系统:ubuntu-12.04.5-desktop-amd64.iso 
VMare下载: https://www.cr173.com/soft/177242.html
ubuntu下载: 进入ubuntu官网:http://releases.ubuntu.com/后,找到 Ubuntu 12.04.5 LTS (Precise Pangolin) 或者已经归档的12.04文件夹
这里写图片描述
安装成功后安装VM Tools,这里不介绍

2.安装java JDK 1.6

jdk版本:jdk-6u45-linux-x64.bin

下载:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html
详细安装及环境变量配置参考我这篇博文: Ubuntu 下JDK的安装与环境配置

3.安装依赖的其它包

用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

出现如下问题:

一、ubuntu 12.04 64位 android 编译环境安装_第1张图片

参考文章:    

Kernel LTSEnablementStack - Ubuntu Wiki.html https://wiki.ubuntu.com/Kernel/LTSEnablementStack

LTS Enablement Stacks

In an effort to support a wider variety of hardware on an existing LTS release, the 12.04.2 and newer point releases will ship with an updated kernel and X stack by default. These newer hardware enablement stacks will be comprised of the newer kernel and X stacks from 12.10 (Quantal), 13.04 (Raring), 13.10 (Saucy), and 14.04 (Trusty). These enablement stacks are only intended for use on x86 hardware at this time. Those running virtual or cloud images should not need these newer enablement stacks and are thus recommended to remain on the original Precise stack. To remain on the original Precise stack there are a few options:

  • Install from a previous 12.04.0 or 12.04.1 point release and update. The previous 12.04.0 and 12.04.1 releases are archived at http://old-releases.ubuntu.com/

  • Perform an update or upgrade to Precise from a previous release. Only those installing from the 12.04.2 or newer point release media will automatically receive a newer enablement stack by default.
  • Perform a network install using the netboot images rather than the new <release>-netboot images, eg. quantal-netboot images.

Anyone wishing to opt into the hardware enablement stack for Precise may do so by installing the linux-generic-lts-<release> and xserver-xorg-lts-<release> packages. For example to install the Quantal hardware enablement packages in Precise, please run the following command:

将libgl1-mesa-glx:i386替换为libgl1-mesa-glx-lts-:i386

例如:我的ubuntu版本为12.04.5就使用如下命令:
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-lts-trusty:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
一、ubuntu 12.04 64位 android 编译环境安装_第2张图片
一、ubuntu 12.04 64位 android 编译环境安装_第3张图片
安装 成功
4.配置 USB 使用权限

如果在乌班图系统下编译后,并用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计算机连接方式设置为相机,可以看到乌班图能够识别设备了。
这里写图片描述
这里写图片描述

5.配置单独的输出目录。

默认情况下,每次编译的输出都会存储在相应源代码树的 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。这个过程后续会验证

3 下载系统源码

安卓系统源码放在git仓库中

1.安装Repo

repo 是安卓环境下的命令行工具,对Git部分命令进行封装,将 百多个git库进行更新。
按照如下步骤进行安装Repo.

1.确定你的主目录(home)下有bin目录,并且bin目录变量在你的path环境变量中:
$ mkdir ~/bin
$ PATH=~/bin:$PATH
2.下载Repo tool,并且确保它是可执行的。
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

初始化 Repo 客户端

当你安装完Repo后,设置安卓源码仓库的客户端:

1.创建一个存放安卓源码的空目录
$ 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$ 

2.进入Android444_r1 目录,运行 repo init 来下载相应版本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
3.配置你的用户邮箱

repo init 后,会提示你输入名字和邮箱, 两个我都是直接按的回车键,这时它默认将系统用户名和主机名 作为用户名和邮箱,接着我直接按了y,提示repo初始完。

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

这里写图片描述

4.然后同步源码树
$ 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镜像和内核的刷机教程。小白的学习记录过程分享,大神不要喷呀。

转自:http://blog.csdn.net/u012417380/article/details/72809141

你可能感兴趣的:(android)