<span style="font-family: Tahoma; background-color: rgb(255, 255, 255);">基于Ubuntu 14.04 LTS编译Android4.4.2源代码</span>
Dennis Hu 2014-4-21
环境准备:
基本环境:ubuntu-14.04-desktop-64bit LTS(裸机或者Windows下虚拟机安装均可,12.04也可以,但10.04目前不支持了)
其他要求:空闲磁盘空间100G以上,代码部分接近10G,其他为git和编译中间和目标文件准备
==========================================
第一部分:环境初始化
==========================================
1. 安装Oracle JDK
注意:如果编译Android4.4不能用OpenJDK,必须用Oracle JDK1.6,1.7目前是不支持的。
具体如何安装Oracle JDK,请参见我的另一篇单独的文章,[Ubuntu Linux下安装Oracle JDK]
http://blog.csdn.net/gobitan/article/details/24322561.
2. 安装必备工具软件
注意:这里与官方的有个包不同,官方的包会报错.
- $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-dri:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386 dpkg-dev
- $ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
3. 配置USB访问权限
将如下内容加入到/etc/udev/rules.d/51-android.rules文件中
然后执行如下命令,将上述文件中的username替换为你的用户名。
- # adb protocol on passion (Nexus One)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e12", MODE="0600", OWNER="<username>"
- # fastboot protocol on passion (Nexus One)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", ATTR{idProduct}=="0fff", MODE="0600", OWNER="<username>"
- # adb protocol on crespo/crespo4g (Nexus S)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", MODE="0600", OWNER="<username>"
- # fastboot protocol on crespo/crespo4g (Nexus S)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e20", MODE="0600", OWNER="<username>"
- # adb protocol on stingray/wingray (Xoom)
- SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="70a9", MODE="0600", OWNER="<username>"
- # fastboot protocol on stingray/wingray (Xoom)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="708c", MODE="0600", OWNER="<username>"
- # adb protocol on maguro/toro (Galaxy Nexus)
- SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", MODE="0600", OWNER="<username>"
- # fastboot protocol on maguro/toro (Galaxy Nexus)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e30", MODE="0600", OWNER="<username>"
- # adb protocol on panda (PandaBoard)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d101", MODE="0600", OWNER="<username>"
- # adb protocol on panda (PandaBoard ES)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="d002", MODE="0600", OWNER="<username>"
- # fastboot protocol on panda (PandaBoard)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d022", MODE="0600", OWNER="<username>"
- # usbboot protocol on panda (PandaBoard)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d00f", MODE="0600", OWNER="<username>"
- # usbboot protocol on panda (PandaBoard ES)
- SUBSYSTEM=="usb", ATTR{idVendor}=="0451", ATTR{idProduct}=="d010", MODE="0600", OWNER="<username>"
- # adb protocol on grouper/tilapia (Nexus 7)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e42", MODE="0600", OWNER="<username>"
- # fastboot protocol on grouper/tilapia (Nexus 7)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e40", MODE="0600", OWNER="<username>"
- # adb protocol on manta (Nexus 10)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee2", MODE="0600", OWNER="<username>"
- # fastboot protocol on manta (Nexus 10)
- SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee0", MODE="0600", OWNER="<username>"
- $sudo sed -i 's/username/dennis/g' /etc/udev/rules.d/51-android.rules
安装ccache并将export USE_CCACHE=1加入到~/.bashrc中。
- $sudo apt-get install ccache
- $source ~/.bashrc
第二部分:源代码下载
==========================================
1. 安装repo
(1) 创建目录
- $ mkdir ~/bin
- $ PATH=~/bin:$PATH
注:由于防火墙的原因,官方提供的链接无法使用,需替换为如下:
官方的为curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
可用的:
- curl http://git-repo.googlecode.com/files/repo-1.12 > ~/bin/repo
(3) 增加可执行权限
- $ chmod a+x ~/bin/repo
2. 初始化Repo客户端
(1)创建目录
- $mkdir aosp
- $cd aosp
(2) 初始化repo
注:后面可能出现网络错误,请先将如下内容加入到/etc/hosts中然后做后面的步骤。
(也可以上网去买一个VPN(
http://astrill-china.com/
),除了网速不是很快之外,上美帝的各种网站都没问题的。)
- 74.125.71.82 www.googlesource.com
- 74.125.31.82 android.googlesource.com
- 203.208.46.172 cache.pack.google.com
- 59.24.3.173 cache.pack.google.com
- $repo init -u https://android.googlesource.com/platform/manifest -b android-4.4.2_r2
注意:如果是在虚拟机中执行,需要采用email验证的方式,详细参见参考资料[2].
设置git的用户名和邮件地址,将您的真实email地址和name替换了:
- git config --global user.email "you@example.com"
- git config --global user.name "Your Name"
修改aosp/.resp/manifest.xml,将fetch=".."改为:
fetch="https://android.googlesource.com/"
3. 下载源代码
- $repo sync
注意:系统可能会提示你升级repo,最好不要升级,除非你有十足信息解决升级后的一系列问题。
好了,慢慢等吧,短则数小时,长则几天几夜!我花了5个小时
==========================================
第三部分:源代码编译
==========================================
1. 设置ccache
注:aosp为源代码根目录
- $ cd aosp
- $ prebuilts/misc/linux-x86/ccache/ccache -M 50G
略:由于我未分区,因此采用默认的out目录
3. 初始化编译环境
- dennis@dubuntu1404:~/devenv/aosp$ . build/envsetup.sh
- including device/samsung/manta/vendorsetup.sh
- including device/generic/mips/vendorsetup.sh
- including device/generic/armv7-a-neon/vendorsetup.sh
- including device/generic/x86/vendorsetup.sh
- including device/lge/mako/vendorsetup.sh
- including device/lge/hammerhead/vendorsetup.sh
- including device/asus/tilapia/vendorsetup.sh
- including device/asus/deb/vendorsetup.sh
- including device/asus/grouper/vendorsetup.sh
- including device/asus/flo/vendorsetup.sh
- including sdk/bash_completion/adb.bash
- dennis@dubuntu1404:~/devenv/aosp$
4.安装perl的switch库:
sudo apt-get install libswitch-perl
5. 选择编译目标包
注:目标包的选择跟你的硬件有关。我这里选择9,mako
- dennis@dubuntu1404:~/devenv/aosp$ lunch
- You're building on Linux
- Lunch menu... pick a combo:
- 1. aosp_arm-eng
- 2. aosp_x86-eng
- 3. aosp_mips-eng
- 4. vbox_x86-eng
- 5. aosp_manta-userdebug
- 6. mini_mips-userdebug
- 7. mini_armv7a_neon-userdebug
- 8. mini_x86-userdebug
- 9. aosp_mako-userdebug
- 10. aosp_hammerhead-userdebug
- 11. aosp_tilapia-userdebug
- 12. aosp_deb-userdebug
- 13. aosp_grouper-userdebug
- 14. aosp_flo-userdebug
- Which would you like? [aosp_arm-eng] 1
- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=4.4.2
- TARGET_PRODUCT=aosp_mako
- TARGET_BUILD_VARIANT=userdebug
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- TARGET_ARCH_VARIANT=armv7-a-neon
- TARGET_CPU_VARIANT=krait
- HOST_ARCH=x86
- HOST_OS=linux
- HOST_OS_EXTRA=Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty
- HOST_BUILD_TYPE=release
- BUILD_ID=KVT49L
- OUT_DIR=out
- ============================================
- dennis@dubuntu1404:~/devenv/aosp$ make -j4
整个编译过程,大约花了三个小时。编译成功后会在out目录下生成img文件。
6.运行模拟器
编译完成后,可以运行emulator来启动模拟器运行原版Android系统。
-
$ . build / envsetup . sh$ lunch 1$ make - j4
如果找不到emulator命令,需进行初始化操作:
-
$ source build / envsetup . sh$ lunch full - eng$ emulator