自己动手编译AOSP源码

本文使用的系统是Ubuntu16.04,编译一个android系统

安装工具

  • 安装git
    1
    2
    3
    sudo apt-get install git
    git config --global user.email "yourmail"
    git config --global user.name "yourname"

yourmail是你的邮箱地址,yourname是你的用户名

  • 安装repo工具
    1
    2
    3
    4
    mkdir ~/bin
    PATH=~/bin:$PATH
    curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    chmod a+x ~/bin/repo

同步源码

  • 方式一:从google的服务器同步(国内不可用)
  • 创建存放源码的文件夹

    1
    2
    mkdir android
    cd android
  • 初始化仓库

    1
    repo init -u https://android.googlesource.com/platform/manifest

也可以指定同步某个特定的版本

1
repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1

-b 后面的参数是需要同步分支,在这里可以获取分支代码

  • 同步源码树

    1
    repo sync
  • 方式二:直接从清华大学镜像站现在AOSP初始包(推荐使用)

    由于首次同步需要下载 24GB 数据,过程中任何网络故障都可能造成同步失败,我们强烈建议您使用初始化包进行初始化。
    下载 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,下载完成后记得根据 checksum.txt 的内容校验一下。
    由于所有代码都是从隐藏的 .repo 目录中 checkout 出来的,所以我们只保留了 .repo 目录,下载后解压 再 repo sync 一遍即可得到完整的目录。

    1
    2
    3
    4
    5
    6
    wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下载初始化包
    tar xf aosp-latest.tar
    cd AOSP # 解压得到的 AOSP 工程目录
    # 这时 ls 的话什么也看不到,因为只有一个隐藏的 .repo 目录
    repo sync # 正常同步一遍即可得到完整目录
    # 或 repo sync -l 仅checkout代码

此后,每次只需运行 repo sync 即可保持同步

  • 方式三:从清华大学开元镜像获取
  • 创建存放源码的文件夹

    1
    2
    mkdir android
    cd android
  • 初始化仓库

    1
    repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest

指定同步某个特定的版本

1
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-4.0.1_r1

-b 后面的参数是需要同步分支,在这里可以获取分支代码

  • 同步源码树
    1
    repo sync

然后耐心等待源码同步完成 20多G~

编译AOSP源码

  • 编译前的准备工作

    Ubuntu 16.04 编译master源码需要安装OpenJDK 8,注意不是SUN JDK1.8,否则编译会报错
    安装OpenJDK 8
    1
    sudo apt-get install openjdk-8-jdk

Ubuntu 16.04要安装的依赖

1
2
3
4
5
6
7
8
9
10
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt-get install git-core gnupg flex bison gperf build-essential
sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
sudo apt-get install lib32z-dev ccache

  • 初始化编译环境
    在aosp源码目录下执行

    1
    source build/envsetup.sh
  • 开始编译AOSP源码
    通过lunch命令选择需要编译的目标设备

    1
    lunch

选择需要编译的版本,我选择的是1.aosp_arm-eng,开始编译(强烈建议不要编译这个版本,非常卡。。。,建议编译aosp_x86-eng或aosp_x86_64-eng,你打开aosp_arm-eng编译的镜像会有一句英语提示你编译x86的版本的)

1
make -j8

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进去
    1
    2
    3
    source build/envsetup.sh
    lunch
    emulator

lunch 之后选择1


模拟器成功的跑起来了!

错误和解决

编译过程遇到的一些错误

  • /bin/bash: xmllint: command not found
    解决
    1
    sudo apt-get install libxml2-utils