自己动手编译AOSP源码
本文使用的系统是Ubuntu16.04,编译一个android系统
安装工具
- 安装git123sudo apt-get install gitgit config --global user.email "yourmail"git config --global user.name "yourname"
yourmail是你的邮箱地址,yourname是你的用户名
- 安装repo工具1234mkdir ~/binPATH=~/bin:$PATHcurl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repochmod a+x ~/bin/repo
同步源码
- 方式一:从google的服务器同步(国内不可用)
创建存放源码的文件夹
12mkdir androidcd android初始化仓库
1repo init -u https://android.googlesource.com/platform/manifest
也可以指定同步某个特定的版本
-b 后面的参数是需要同步分支,在这里可以获取分支代码
同步源码树
1repo sync方式二:直接从清华大学镜像站现在AOSP初始包(推荐使用)
由于首次同步需要下载 24GB 数据,过程中任何网络故障都可能造成同步失败,我们强烈建议您使用初始化包进行初始化。
下载 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,下载完成后记得根据 checksum.txt 的内容校验一下。
由于所有代码都是从隐藏的 .repo 目录中 checkout 出来的,所以我们只保留了 .repo 目录,下载后解压 再 repo sync 一遍即可得到完整的目录。123456wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包tar xf aosp-latest.tarcd AOSP # 解压得到的 AOSP 工程目录# 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录repo sync # 正常同步一遍即可得到完整目录# 或 repo sync -l 仅checkout代码
此后,每次只需运行 repo sync 即可保持同步
- 方式三:从清华大学开元镜像获取
创建存放源码的文件夹
12mkdir androidcd android初始化仓库
1repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
指定同步某个特定的版本
-b 后面的参数是需要同步分支,在这里可以获取分支代码
- 同步源码树1repo sync
然后耐心等待源码同步完成 20多G~
编译AOSP源码
- 编译前的准备工作
Ubuntu 16.04 编译master源码需要安装OpenJDK 8,注意不是SUN JDK1.8,否则编译会报错
安装OpenJDK 81sudo apt-get install openjdk-8-jdk
Ubuntu 16.04要安装的依赖
初始化编译环境
在aosp源码目录下执行1source build/envsetup.sh开始编译AOSP源码
通过lunch命令选择需要编译的目标设备1lunch

选择需要编译的版本,我选择的是1.aosp_arm-eng,开始编译(强烈建议不要编译这个版本,非常卡。。。,建议编译aosp_x86-eng或aosp_x86_64-eng,你打开aosp_arm-eng编译的镜像会有一句英语提示你编译x86的版本的)
Build everything with make. GNU make can handle parallel tasks with a -jN argument, and it’s common to use a number of tasks N that’s between 1 and 2 times the number of hardware threads on the computer being used for the build. For example, on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between make -j16 and make -j32.
等待几个小时之后编译完成。。。
运行编译后的镜像
- 由于我编译的是虚拟机镜像,所以运行虚拟机,真机的话把生成的镜像flash进去123source build/envsetup.shlunchemulator
lunch 之后选择1
模拟器成功的跑起来了!
错误和解决
编译过程遇到的一些错误
- /bin/bash: xmllint: command not found
解决1sudo apt-get install libxml2-utils