【转】基于Ubuntu 14.04 LTS编译Android4.4.2源代码
原文网址:http://blog.csdn.net/gobitan/article/details/24367439
基于Ubuntu 14.04 LTS编译Android4.4.2源代码
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文件中
- # 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>"
然后执行如下命令,将上述文件中的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中然后做后面的步骤。
- 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. 选择编译目标包
注:目标包的选择跟你的硬件有关。我这里选择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] 9
- ============================================
- 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文件。
5. 将img加载到物理机
(1) 将手机用USB数据线连接到机器
(2) 安装fastboot工具
- $ sudo apt-get install android-tools-fastboot
(3) 设置ANDROID_PRODUCT_OUT环境变量
将如下部分加入~/.bashrc文件中:
- export ANDROID_PRODUCT_OUT=/home/dennis/devenv/aosp/out/target/product/mako
然后保存执行". .bashrc"使其生效。
(4) 以fastboot模式重启手机
- $ adb reboot bootloader
此时手机会重启,呈现一个与往不同的fastboot界面
(5) 将编译的img刷到目标机
- dennis@dubuntu1404:~/devenv/aosp/out$ fastboot flashall -w
- --------------------------------------------
- Bootloader Version...: PRIMEMD04
- Baseband Version.....: I9250XXLJ1
- Serial Number........: 0146A0D00600900B
- --------------------------------------------
- checking product...
- FAILED
- Device product is 'tuna'.
- Update requires 'mako'.
- finished. total time: 0.035s
(注:由于我的物理机三星I9250在lunch里没有对应的型号,因此会加载失败,这里这是记录过程)
===================================
如果需要编译其他版本,可执行如下步骤,这里选择虚拟机用的通用版本aosp_arm-eng。
- $ cd aosp
- 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$ lunch aosp_arm-eng
- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=4.4.2
- TARGET_PRODUCT=aosp_arm
- TARGET_BUILD_VARIANT=eng
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- TARGET_ARCH_VARIANT=armv7-a
- TARGET_CPU_VARIANT=generic
- 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
- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=4.4.2
- TARGET_PRODUCT=aosp_arm
- TARGET_BUILD_VARIANT=eng
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- TARGET_ARCH_VARIANT=armv7-a
- TARGET_CPU_VARIANT=generic
- 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
- ============================================
- including ./abi/cpp/Android.mk ...
- including ./art/Android.mk ...
- ....中间过程省略
- Install system fs image: out/target/product/generic/system.img
- out/target/product/generic/system.img+ maxsize=588791808 blocksize=2112 total=576716800 reserve=5947392
编译完成,执行如下命令可以将您刚build好的ROM在模拟器里运行起来,第一次启动的时间可能比较长:
$ emulator &
注意:如果提示找不到emulator命令,可将如下环境变量重新初始化一遍(也就是每次重新进入系统运行的时候,都需要初始化一遍),lunch后面的参数要与你编译的ROM选项一致。
- $. build/envsetup.sh
- $lunch aosp_arm-eng
更新日志:
2014-4-25:增加自己编译的ROM在模拟器里的运行方法;
参考资料:
Stay hungry, stay foolish!