Android源码的下载及编译是一件相当耗时的工作,经过两天的努力,终于完成。在此略记下安装过程,以咨后人。
1. 安装环境
(1)Android4.2
(2)Ubuntu12 64位,这一点很重要,必须是64位,32位无法编译
(3)Vmware8 Ubuntu安装于虚拟机之上
(4)JDK必须为1.6
2. 事先准备
(1)虚拟机内存至少2G
(2)虚拟机硬盘最好设为50G,我从20G到30G,全都失败,最后升到45G。成功之后发现硬盘被用去91%,所以最好设为50G左右。
3. 下载及编译
这里只是列出所使用的命令,因为Linux的很多资源会不断变更,所以有些资源的链接应以安装时为准。
(1) JDK安装
(1.a) sudo apt-get install git-core gnupg
(1.b)sudo add-apt-repository ppa:webupd8team/java
(1.c)sudo apt-get update
(1.d)sudo apt-get install oracle-java6-installer
(2) 一些库的安装
(2.a)sudo apt-get install flex bison gperf libsdl-dev libesd0-dev build-essential zip curl
(2.b)sudo apt-get install valgrind
(2.c)sudo apt-get install xsltproc
(2.d)sudo apt-get install libc6-dev:i386
(2.e)sudo apt-get install build-essential
(2.f)sudo apt-get install gcc-multilib
(2.g)sudo apt-get install libxml2-utils
(2.h)sudo apt-get install lzma
(2.i)sudo apt-get install lib32stdc++6
(2.j)sudo apt-get install lib32z1
(3)下载
(3.a)wget https://dl-ssl.google.com/dl/googlesource/git-repo/repo (下载repo)
如果上面的链接无效,尝试以下链接: wget http://php.webtutor.pl/en/wp-content/uploads/2011/09/repo
(3.b)chmod 777 repo
(3.c)cp repo /bin/
(3.d)mkdir Android (建立存入下载代码目录)
(3.e)cd Android
(3.f)repo init -u https://android.googlesource.com/platform/manifest
(3.g)repo init -u https://android.googlesource.com/platform/manifest -b android-4.2_r1
(3.h)repo sync (下载开始)
如果repo sync时出现以下错误:
fatal: '../platform/abi/cpp.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
error: Cannot fetch platform/abi/cpp
(3.i) 在.repo目录下打开manifest.xml文件,找到fetch,然后将其值改为:fetch="git://Android.git.linaro.org/",之后再repo sync
如果下载时出现以下错误:
error: Exited sync due to fetch errors...
(3.j) 点击获取用户名和密码
(3.k) 拷贝machine开头的两行字到用户根下的.netrc文件,若没有则新建
(3.l)repo init -u https://android.googlesource.com/a/platform/manifest
(3.m)repo sync
4.编译
(4.a)增大swap
(4.a.1) mkdir swap
(4.a.2) cd swap
(4.a.3) sudo dd if=/dev/zero of=swapfile bs=1024 count=1000000 (如此是增加1G,变更count即可)
(4.a.4) sudo mkswap swapfile
(4.a.5) sudo swapon swapfile (至此,增大swap完成)
若要去除增长的swap,执行 sudo swapoff swapfile
若要显示空间的大小,执行free -m
(4.b) make -j4
我的虚拟机内存2G,swap空间2G,编译耗时接近6个小时。
至此,编译完成。可执行emulator命令启动模拟器(在out/host/linux-x86/bin目录下)。
虽然在编译完成之后立即启动emulator,可以正常启动,但之后却无法启动,需要设置以下环境变量:
export ANDROID_PRODUCT_OUT=~/Android/out/target/product/generic
export ANDROID_BUILD_TOP=~/Android