编译环境:Ubuntu 16.04
镜像文件:清华大学AOSP镜像 Android7.0
一、源码下载
1 镜像地址
- 1
- 2
- 1
- 2
2 过程摘录
下载Repo 工具
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
使用每月更新的初始化包
由于首次同步需要下载 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
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
此后,每次只需运行 repo sync
即可保持同步。 我们强烈建议您保持每天同步,并尽量选择凌晨等低峰时间
3 解决repo sync下载代码时Failed connect错误的办法:
下载Android代码时错误提示:error: Failed connect to android.googlesource.com:443
解决方法:编辑/etc/hosts文件
- 1
- 1
增加下面内容,保存
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
4 其他
- 初始化包大小21.4G,我的网速5-6M/s,下载时间基本一个小时左右。
- Android7.0,aosp目录编译完之后的大小是54.6G,安装Ubuntu时要注意留够足够的空间,推荐100G+以上。
二、源码编译
1 搭建编译环境
- openJdk is needed !我自己平时开发用的是JDK1.8,这里就要安装一下openjdk,并且更改环境变量。
- 1
- 2
- 1
- 2
但是如果你是刚安装完Ubuntu,那么就可以跳过此步骤,Ubuntu自带openjdk
- required package
- 1
- 1
- 因为源码包含C 和 C++代码,gcc为必须
- 1
- 1
- Setting up ccache , 使用ccache
ccache是一个编译器ccache用于C和C++。可以让构建更加快速。在源代码的根目录处,做如下操作:
- 1
- 2
- 3
- 1
- 2
- 3
建议的cache大小为50-100G
可以使用如下操作来查看使用的ccache大小
- 1
- 1
若使用Ice Cream Sandwich(4.0.x)或更老的版本,需要用prebuilts来代替prebuilts/misc
2 开始编译 Building the System
- 1.Set up environment
- 1
- 1
or
- 1
- 1
- 2.Choose a Target
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 3.build
使用make构建,GNU make可以通过使用-jN参数来处理并行任务。此处的N,根据所用的计算机的硬件的核数来指定,一般N指定为CPU核心的1到2倍。例如我的CPU是i5-6500(CPU4个核,每个核2个线程),那么最优的构建是使用make -j8。
- 1
- 1
3 build successfully
- 1
- 1
- 目录结构如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
4 编译中的问题解决
- 第一次编译,在编译到82%的时候失败,报错 : Increase Java heap size
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
stackoverflow上关于此问题的解决方法([详见这里](http://stackoverflow.com/questions/34940793/increasing-heap-size-while-building-the-android-source-code-on-ubuntu-15-10)),在make之前进行heap size配置:
- 1
- 2
- 3
- 1
- 2
- 3
三、运行
1 推送设备(真机运行)Flash device
- To flash a device, you will need to use fastboot, which should be included in your path after a successful build. Place the device in fastboot mode either manually by holding the appropriate key combination at boot, or from the shell with
- 1
- 1
- Once the device is in fastboot mode, run
- 1
- 1
- The -w option wipes the /data partition on the device; this is useful for your first time flashing a particular device but is otherwise unnecessary.
2 模拟器运行 Flash emulator
- 1.build generic img
lunch 1即可
- 2.启动模拟器
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
3 注意事项
-
1 因为上面的环境变量配置的是临时的,所以终端关闭后,直接运行emulator是不行的,需要重新运行下/build/envsetup.sh 和lunch 选择之前编译的版本
-
2 安装kvm,开启硬件加速
无论是在Windows平台还是Linux平台,或者还是Mac OSX平台,虚拟机的硬件加速全靠这个。
- 1
- 1
直接执行,完成安装即可。
开启之后我们可以使用下面的指令来验证
- 1
- 1
执行的结果不为0
表示开启成功(我的是4
)。
更好的阅读体验,请访问http://timeriver.com.cn/