Ubuntu14.04下搭建android5.1编译环境
官网上教程:http://source.android.com/source/building.html,基本是按照这个一步一步来的。
一、初始化配置
1.安装jdk7(5.1需要openjdk7,其他版本看文档)
$ sudo apt-get update
$ sudo apt-get install openjdk-7-jdk
更新默认jdk版本
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
2.1404需要下载的依赖
sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip
我的出现如下错误
下列软件包有未满足的依赖关系:
g++-multilib : 依赖: gcc-multilib (>= 4:4.8.2-1ubuntu6) 但是它将不会被安装
依赖: g++ (>= 4:4.8.2-1ubuntu6) 但是它将不会被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
分别安装gcc-multilib和g++即可:
sudo apt-get install g++
sudo apt-get install gcc-multilib
3.配置USB访问权限
新建/etc/udev/rules.d/51-android.rules文件,可以使用 sudo gedit /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>替换为系统的用户名。
4.配置输出目录
export OUT_DIR_COMMON_BASE=<path-to-your-out-directory>
5.设置ccache
export USE_CCACHE=1
默认存放在~/.ccache,如果是NFS文件系统还需特别指定:
export CCACHE_DIR=<path-to-your-cache-directory>
建议cache大小50-100G,源码下载完后执行:
prebuilts/misc/linux-x86/ccache/ccache -M 50G
环境配置完成,接下来下载源码。
二、源码下载
1.安装Repo
新建bin目录并添加到环境变量中:
$ mkdir ~/bin
$ PATH=~/bin:$PATH
下载Repo
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
由于国内被墙,google可能无法下载,推荐使用tsinghua的aosp源下载:
git clone git://aosp.tuna.tsinghua.edu.cn/android/git-repo.git/
拷贝到bin目录并添加权限:
cp git-repo/repo ~/bin/
chmod a+x ~/bin/repo
修改repo文件,设置REPO_URL如下:
REPO_URL = 'git://aosp.tuna.tsinghua.edu.cn/android/git-repo'
2.初始化repo
创建源码下载目录:
$ mkdir ~/source
$ cd ~/srouce
初始化repo
$ repo init -u https://android.googlesource.com/platform/manifest
google连不上的可以用这个:
repo init -u git://aosp.tuna.tsinghua.edu.cn/android/platform/manifest
此命令默认下载master分支的最新版本,此时即5.1,如果要指定版本,需要加-b,如下:
$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
Build Branch Version Supported devices
LMY47Z android-5.1.1_r4 Lollipop Nexus 6 (For Sprint, USC ONLY)
LMY48B android-5.1.1_r3 Lollipop Nexus 5
LMY47X android-5.1.1_r2 Lollipop Nexus 9 (volantis)
LMY47V android-5.1.1_r1 Lollipop Nexus 7 (flo/grouper), Nexus 10, Nexus Player
LMY47O android-5.1.0_r5 Lollipop Nexus 4, Nexus 7 (flo/deb)
LMY47M android-5.1.0_r4 Lollipop Nexus 6 (For T-Mobile ONLY)
LMY47I android-5.1.0_r3 Lollipop Nexus 5, Nexus 6
LMY47E android-5.1.0_r2 Lollipop Nexus 6
LMY47D android-5.1.0_r1 Lollipop Nexus 5, Nexus 6, Nexus 7 (grouper/tilapia), Nexus 10, Nexus Player
LRX22L android-5.0.2_r3 Lollipop Nexus 9 (volantis/volantisg)
LRX22G android-5.0.2_r1 Lollipop Nexus 7 (flo/deb/grouper/tilapia), Nexus 10
LRX22C android-5.0.1_r1 Lollipop Nexus 4, Nexus 5, Nexus 6 (shamu), Nexus 7 (flo), Nexus 9 (volantis/volantisg), Nexus 10
LRX21V android-5.0.0_r7.0.1 Lollipop Nexus Player (fugu)
LRX21T android-5.0.0_r6.0.1 Lollipop Nexus 4
LRX21R android-5.0.0_r5.1.0.1 Lollipop Nexus 9 (volantis)
LRX21Q android-5.0.0_r5.0.1 Lollipop Nexus 9 (volantis)
LRX21P android-5.0.0_r4.0.1 Lollipop Nexus 7 (flo/grouper), Nexus 10
LRX21O android-5.0.0_r3.0.1 Lollipop Nexus 5 (hammerhead), Nexus 6 (shamu)
LRX21M android-5.0.0_r2.0.1 Lollipop Nexus Player (fugu)
LRX21L android-5.0.0_r1.0.1 Lollipop Nexus 9 (volantis)
KTU84Q android-4.4.4_r2 KitKat Nexus 5 (hammerhead) (For 2Degrees/NZ, Telstra/AUS and India ONLY)
KTU84P android-4.4.4_r1 KitKat Nexus 5, Nexus 7 (flo/deb/grouper/tilapia), Nexus 4, Nexus 10
KTU84M android-4.4.3_r1.1 KitKat Nexus 5 (hammerhead)
KTU84L android-4.4.3_r1 KitKat Nexus 7 (flo/deb/grouper/tilapia), Nexus 4, Nexus 10
KVT49L android-4.4.2_r2 KitKat Nexus 7 (deb Verizon)
KOT49H android-4.4.2_r1 KitKat Nexus 5, Nexus 7 (flo/deb/grouper/tilapia), Nexus 4, Nexus 10
KOT49E android-4.4.1_r1 KitKat Nexus 5, Nexus 7 (flo/deb/grouper/tilapia), Nexus 4, Nexus 10
KRT16S android-4.4_r1.2 KitKat Nexus 7 (flo/deb/grouper/tilapia), Nexus 4, Nexus 10
KRT16M android-4.4_r1 KitKat Nexus 5 (hammerhead)
JLS36I android-4.3.1_r1 Jelly Bean Nexus 7 (deb)
JLS36C android-4.3_r3 Jelly Bean Nexus 7 (deb)
JSS15R android-4.3_r2.3 Jelly Bean Nexus 7 (flo)
JSS15Q android-4.3_r2.2 Jelly Bean Nexus 7 (flo)
JSS15J android-4.3_r2.1 Jelly Bean Nexus 7 (flo/deb)
JSR78D android-4.3_r2 Jelly Bean Nexus 7 (deb)
JWR66Y android-4.3_r1.1 Jelly Bean Galaxy Nexus, Nexus 7 (grouper/tilapia), Nexus 4, Nexus 10
JWR66V android-4.3_r1 Jelly Bean Galaxy Nexus, Nexus 7 (grouper/tilapia), Nexus 4, Nexus 10
JWR66N android-4.3_r0.9.1 Jelly Bean Galaxy Nexus, Nexus 7 (grouper/tilapia/flo), Nexus 4, Nexus 10
JWR66L android-4.3_r0.9 Jelly Bean Nexus 7
JDQ39E android-4.2.2_r1.2 Jelly Bean Nexus 4
JDQ39B android-4.2.2_r1.1 Jelly Bean Nexus 7
JDQ39 android-4.2.2_r1 Jelly Bean Galaxy Nexus, Nexus 7, Nexus 4, Nexus 10
JOP40G android-4.2.1_r1.2 Jelly Bean Nexus 4
JOP40F android-4.2.1_r1.1 Jelly Bean Nexus 10
JOP40D android-4.2.1_r1 Jelly Bean Galaxy Nexus, Nexus 7, Nexus 4, Nexus 10
JOP40C android-4.2_r1 Jelly Bean Galaxy Nexus, Nexus 7, Nexus 4, Nexus 10
JZO54M android-4.1.2_r2.1 Jelly Bean
JZO54L android-4.1.2_r2 Jelly Bean
JZO54K android-4.1.2_r1 Jelly Bean Nexus S, Galaxy Nexus, Nexus 7
JRO03S android-4.1.1_r6.1 Jelly Bean Nexus 7
JRO03R android-4.1.1_r6 Jelly Bean Nexus S 4G
JRO03O android-4.1.1_r5 Jelly Bean Galaxy Nexus
JRO03L android-4.1.1_r4 Jelly Bean Nexus S
JRO03H android-4.1.1_r3 Jelly Bean
JRO03E android-4.1.1_r2 Jelly Bean Nexus S
JRO03D android-4.1.1_r1.1 Jelly Bean Nexus 7
JRO03C android-4.1.1_r1 Jelly Bean Galaxy Nexus
IMM76L android-4.0.4_r2.1 Ice Cream Sandwich
IMM76K android-4.0.4_r2 Ice Cream Sandwich Galaxy Nexus
IMM76I android-4.0.4_r1.2 Ice Cream Sandwich Galaxy Nexus
IMM76D android-4.0.4_r1.1 Ice Cream Sandwich Nexus S, Nexus S 4G, Galaxy Nexus
IMM76 android-4.0.4_r1 Ice Cream Sandwich
IML77 android-4.0.3_r1.1 Ice Cream Sandwich
IML74K android-4.0.3_r1 Ice Cream Sandwich Nexus S
ICL53F android-4.0.2_r1 Ice Cream Sandwich Galaxy Nexus
ITL41F android-4.0.1_r1.2 Ice Cream Sandwich Galaxy Nexus
ITL41D android-4.0.1_r1.1 Ice Cream Sandwich Galaxy Nexus
ITL41D android-4.0.1_r1 Ice Cream Sandwich Galaxy Nexus
GWK74 android-2.3.7_r1 Gingerbread Nexus S 4G
GRK39F android-2.3.6_r1 Gingerbread Nexus One, Nexus S
GRK39C android-2.3.6_r0.9 Gingerbread Nexus S
GRJ90 android-2.3.5_r1 Gingerbread Nexus S 4G
GRJ22 android-2.3.4_r1 Gingerbread Nexus One, Nexus S, Nexus S 4G
GRJ06D android-2.3.4_r0.9 Gingerbread Nexus S 4G
GRI54 android-2.3.3_r1.1 Gingerbread Nexus S
GRI40 android-2.3.3_r1 Gingerbread Nexus One, Nexus S
GRH78C android-2.3.2_r1 Gingerbread Nexus S
GRH78 android-2.3.1_r1 Gingerbread Nexus S
GRH55 android-2.3_r1 Gingerbread earliest Gingerbread version, Nexus S
FRK76C android-2.2.3_r2 Froyo
FRK76 android-2.2.3_r1 Froyo
FRG83G android-2.2.2_r1 Froyo Nexus One
FRG83D android-2.2.1_r2 Froyo Nexus One
FRG83 android-2.2.1_r1 Froyo Nexus One
FRG22D android-2.2_r1.3 Froyo
FRG01B android-2.2_r1.2 Froyo
FRF91 android-2.2_r1.1 Froyo Nexus One
FRF85B android-2.2_r1 Froyo Nexus One
EPF21B android-2.1_r2.1p2 Eclair
ESE81 android-2.1_r2.1s Eclair
EPE54B android-2.1_r2.1p Eclair Nexus One
ERE27 android-2.1_r2 Eclair Nexus One
ERD79 android-2.1_r1 Eclair Nexus One
ESD56 android-2.0.1_r1 Eclair
ESD20 android-2.0_r1 Eclair
DMD64 android-1.6_r1.5 Donut
DRD20 android-1.6_r1.4
DRD08 android-1.6_r1.3
DRC92 android-1.6_r1.2
如果是首次使用git会提示email验证,输入如下命令:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
3.替换已有的AOSP源代码的remote
如果你之前已经通过某种途径获得了AOSP的源码,但是你希望以后通过TUNA同步,只需要将.repo/manifest.xml中的
<remote name="aosp" fetch=".." review="https://android-review.googlesource.com/" />
改为
<remote name="aosp" fetch="git://aosp.tuna.tsinghua.edu.cn/android/" review="https://android-review.googlesource.com/" />
4.下载源码
$ repo sync
这一步要等好久..好久...好久....repo可以看做git的升级版,有断点续传的功能,所以慢慢下吧,5.1大概下了30G
三、源码编译
1.初始化
执行 $ source build/envsetup.sh 或 $ . build/envsetup.sh
2.选择平台编译选项
$ lunch aosp_arm-eng
可以仅输入lunch来选择可选的编译目标。其中各个参数含义为,eng: 工程机,user:最终用户机,userdebug:调试测试机,tests:测试机
3.编译
make
如果开发机器配置较好,例如双核四线程,可以使用
make -j4
当然也可以使用 make -j16、
make -j32,make失败或停止后,可以使用make -k 继续编译。
接下来又是漫长的等待过程。等。。。等。。。等。。。。
若干小时候,出现错误:
build/core/shared_library_internal.mk:68 recipe for target 'out/target/product/generic/obj/SHARED_LIBRARIES/libwebviewchromium_intermediates/LINKED/libwebviewchromium.so' failed
make: ** [out/target/product/generic/obj/SHARED_LIBRARIES/libwebviewchromium_intermediates/LINKED/libwebviewchromium.so] Error 1
make: ** Deleting file 'out/target/product/generic/obj/SHARED_LIBRARIES/libwebviewchromium_intermediates/
按照http://jileniao.net/ubunt-add-swap-to-build-libwebviewchromium.html的方法解决。
1.#dd if=/dev/zero of=/opt/swap1 bs=1024 count=1024000
(count的值等于1024 x 你想要的文件大小, 4096000是4G,1024000是1G)
2.#mkswap /opt/swap1
(把这个文件变成swap文件)
3.#swapon /opt/swap1
(启用这个swap文件)
4.为了使每次开机时都能正常使用swap文件,所以这里需要把swap文件增加到fstab文件中
#sudo vi /etc/fstab
在最后一行增加如下内容
/opt/swap1 swap swap defaults 0 0
重复上述4步,再次增加一个/opt/swap2的文件。
据说增加内存也可以,我也没试过。
4.结果展示
启动模拟器: emulator &