AOSP
最近因为想更深入的了解一下android系统,在我的电脑上进行了android源码编译和内核编译的操作,因为其中操作较复杂,为了防止以后再次编译的时候忘了,这里进行一下记录:
编译环境搭建
- 创建区分大小写的文件系统
//指定大小为160g,-size指定文件系统大小,最后的~/android.dmg为文件系统路径和名称
#hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 160g ~/android.dmg.sparseimage
//调整文件系统大小
# hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage
- 修改~/.bash_profile文件,添加如下代码:
# mount the android file image
function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }
# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }
命令行运行. ~/.bash_profile
使.bash_profile文件生效
之后在每次执行mountAndroid
命令和umountAndroid
命令时执行装载文件系统和卸载文件系统的操作。
3. 安装jdk
这个就不说了,下载安装就OK了
其中需要注意的一点时,编译需要jdk7,如果是更高版本(比如我的原来安装的是jdk1.8),需要同时安装两个版本,在编译时切换到1.7,具体切换方式参考MAC下安装多版本JDK和切换几种方式
4. 安装 Xcode 命令行工具
$ xcode-select --install
这个因为我的电脑上已经安装了xcode,这个就不用关了
5. 通过macports.org安装 MacPorts
6. 添加export PATH=/opt/local/bin:$PATH
到~/.bash_profile
文件中
7. 通过 MacPorts 获取 Make、Git 和 GPG 程序包
$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg
8. 设置文件描述符数量上限
在 Mac OS 中,可同时打开的文件描述符的默认数量上限太低,在高度并行的编译流程中,可能会超出此上限。
要提高此上限,请将下列行添加到 ~/.bash_profile 中:
# set the number of open files to be 1024
ulimit -S -n 1024
至此,android源代码编译环境已经配置好了。
下载源代码
- 安装 Repo
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo - 在之前创建的文件系统中创建工作目录。
$ mountAndroid
$ mkdir [WORKING_DIRECTORY]
$ cd [WORKING_DIRECTORY]
- 初始化repo仓库
//这样不指定版本号的还,默认版本为当前最新版本
$ repo init -u https://android.googlesource.com/platform/manifest
//指定版本分支
$ repo init -u https://android.googlesource.com/platform/manifest -b android-6.0.1_r1
//上述因为直接访问google,需要翻墙,且奇慢,可以将 https://android.googlesource.com/ 全部使用 https://aosp.tuna.tsinghua.edu.cn/ 代替。(使用清华的镜像)
上面的版本分支,可以在源代码标记和版本中查看。
4. 下载源代码
repo sync -j2
(-j2为指定2个线程的意思,这个值一般推荐为cpu核心书的1.5-2倍,不过个人不建议在下载时指定这个推荐数,个人觉得指定为2就好,毕竟这个不靠cpu,靠的是网卡)
由于同步需要很久(下载30-40g的东西),网络可能出现状况,可以通过写一个脚本,进行自动下载,脚本内容如下:
#!/bin/sh
repo sync -j2
while [ $? -ne 0 ]
do
repo sync -j2
done
直接运行该脚本即可(该脚本摘自repo sync失败后自动重试)
编译源码
- 清理
如果之前进行过编译,请使用以下命令删除所有以前编译操作的已有输出:
$ make clobber - 设置环境
$ source build/envsetup.sh
或
$ . build/envsetup.sh - 选择编译目标
所有编译目标都采用 BUILD-BUILDTYPE 形式,其中 BUILD 是表示特定功能组合的代号。
BUILDTYPE 是以下类型之一:
- user:权限受限;适用于生产环境
- userdebug:与“user”类似,但具有 root 权限和可调试性;是进行调试时的首选编译类型
- eng:具有额外调试工具的开发配置
$ lunch
You're building on Darwin
Lunch menu... pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_mips-eng
4. aosp_mips64-eng
5. aosp_x86-eng
6. aosp_x86_64-eng
7. aosp_deb-userdebug
8. aosp_flo-userdebug
9. full_fugu-userdebug
10. aosp_fugu-userdebug
11. mini_emulator_arm64-userdebug
12. m_e_arm-userdebug
13. mini_emulator_mips-userdebug
14. mini_emulator_x86-userdebug
15. mini_emulator_x86_64-userdebug
16. aosp_flounder-userdebug
17. aosp_angler-userdebug
18. aosp_bullhead-userdebug
19. aosp_hammerhead-userdebug
20. aosp_hammerhead_fp-userdebug
21. aosp_shamu-userdebug
Which would you like? [aosp_arm-eng]
上述版本中选择一个,输入对应数字选择
- 编译代码
make -j4
编译需要大约5个小时,有时编译时,MacBook风扇会很响,也不知道为什么 - 运行模拟器
编译完成后,就可以使用以编译的结果运行模拟器了
emulator
备注:每次在一个新终端中运行模拟器前,都需要设置环境. build/envsetup.sh
,之后可以通过which emulator
获取emulator命令是否在当前源码环境中,只有在时,才能运行模拟器成功
编译过程中错误解决
- 运行
emulator
时报
错误信息: emulator: WARNING: system partition size adjusted to match image file (1536 MB > 200 MB)
解决方法:emulator -partition-size 1536
- 在
. build/evnsetup.sh
后执行lunch
出现Can not find SDK 10.6 at /Developer/SDKs/MacOSX10.6.sdk
解决方式参考here - macOSX10.12.SDK
解决方式参考 解决macOSX10.12.SDK下编译Android Open Source Project出错的问题