笔者编译的是Android 9.0源码,下载下来后将近40g,编译后153g,因此至少须要160g可用磁盘空间。php
看网上有别人下载编译7.1.1版本的源码,编译下来须要67g,则可用磁盘空间至少须要分配80g。html
注:在编译过程当中,若是因为磁盘空间不足致使编译失败,可从新调整磁盘大小,而后接着以前的进度继续编译,因此此处看我的编译的版本,但至少要保证可用磁盘不能过小。java
Mac OS默认会在不区分大小写的文件系统中运行,但因为Git并不支持此类文件系统,因此须要在Mac OS上创建一个区分大小写的磁盘分区。咱们这里使用命令行来建立,一是比较方便,二是后期可扩展。android
在终端输入如下命令来建立一个160g大小的磁盘映像:git
$ hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 160g ~/android.dmg
复制代码
会在当前用户目录下生成一个android.dmg.sparseimage
文件,可简单理解为该文件是编译源码所须要的驱动盘。github
建议在分区时size指定为160g,但若是后期不够,能够经过该命令来调整分区大小,例以下面命令调整为200g:shell
$ hdiutil resize -size 200g ~/android.dmg.sparseimage
复制代码
注意:调整分区大小的命令,须要在该分区磁盘映像已卸载的状况下才能生效。macos
为了方便装载和卸载该分区磁盘映像,咱们能够向~/.bash_profile
文件中添加以下函数:xcode
# mount the android file image
mountAndroid() { hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android; }
复制代码
须要装载分区磁盘映像时,执行以下命令便可,装载路径为/Volumes/android
:bash
$ mountAndroid
复制代码
一样,卸载的函数以下,咱们也将它添加到~/.bash_profile
文件中:
# unmount the android file image
umountAndroid() { hdiutil detach /Volumes/android; }
复制代码
执行以下命令,即可卸载该分区磁盘映像:
$ umountAndroid
复制代码
$ xcode-select --install
复制代码
安装Xcode,直接在AppStore里安装便可。
安装MacPorts或Homebrew,MacPorts和Homebrew是软件包管理工具,可用来直接在终端里安装、更新和卸载软件包。建议两者都安装,笔者在经过MacPorts安装gnupg时死活装不上,最后经过Homebrew安装成功:
在~/.bash_profile
文件中导入路径,就可使用port或brew命令来管理软件包了:
export PATH=/opt/local/bin:$PATH
export PATH=/usr/local/bin:$PATH
复制代码
$ POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg
复制代码
$ brew install gmake libsdl git gnupg2
复制代码
因为新版本的Mac OS更新(我这里是10.14.4 Mojave版本),致使Android源码在编译时可能报以下错误:
system/core/libcutils/threads.c:38:10: error: 'syscall' is deprecated: first deprecated in OS X 10.12 - syscall(2) is unsupported; please switch to a supported interface. For SYS_kdebug_trace use kdebug_signpost(). [-Werror,-Wdeprecated-declarations]
return syscall(SYS_thread_selfid);
复制代码
因此须要下载旧版本的Mac OS SDK,建议下载MacOSX10.11.sdk.tar.xz 版本,解压到自定义目录,而后创建软连接。例如我放在~/lib目录下,再给它建立一个软连接,能够避免下次Mac OS或Xcode升级的时候被删除:
$ sudo ln -s ~/lib/MacOSX10.11.sdk /Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
复制代码
在 Mac OS 中,可同时打开的文件描述符的默认数量上限过低,在高度并行的编译流程中,可能会超出此上限。要提升此上限,请将下列行添加到 ~/.bash_profile
中:
# set the number of open files to be 1024
ulimit -S -n 1024
复制代码
至此,一切准备工做就绪,接下来就是下载源码的过程了。
这里参考Google 教程。因为墙的缘由,即便咱们做为技术人员能翻过去,但使用起来可能仍是有各类各样的问题,因此这里咱们能够经过清华大学镜像站来下载。
因为Android源码庞大复杂,因此Google专门开发了Repo来管理Android源码库,这里很少做介绍,有兴趣可自行阅读repo工具介绍。
按照官方建议,首先在主目录下有一个 bin/
目录,而且将该目录包含在路径中:
$ mkdir ~/bin
$ PATH=~/bin:$PATH
复制代码
而后下载Repo工具,并确保它可执行:
$ curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
$ chmod a+x ~/bin/repo
复制代码
在2.2小节中,咱们经过mountAndroid
命令装载了咱们分区后的磁盘映像,其装载路径为/Volumes/android
,因此咱们须要在该目录下进行初始化:
切换到该目录下,而且建立一个aosp
的工做目录:
$ cd /Volumes/android/
$ mkdir aosp
$ cd aosp
复制代码
如今,咱们的源码根目录全路径为/Volumes/android/aosp
。
Repo的运行过程当中会尝试访问官方的git源更新本身,若是想使用清华的镜像源进行更新,能够将以下内容复制到你的~/.bashrc
(没有该文件则建立一个)里:
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
复制代码
而后配置git,须要姓名和邮箱:
$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"
复制代码
运行repo init
来初始化master
分支:
$ repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
复制代码
要对master
之外的分支进行校验,请使用 -b
来指定相应分支。要查看分支列表,请参阅源代码标记和版本,笔者这里指定的是android-9.0.0_r35
分支:
$ repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r35
复制代码
在终端输入如下命令来下载源码:
$ repo sync
复制代码
因为下载源码过程比较长,因此最好将你的Mac“系统偏好设置” -> “节能”里勾选上“防止进入睡眠”,以下图所示:
接下来就是漫长的等待过程了,30多g,带宽若是还能够的话,差很少不到两小时就下载完成了,会提示:
Syncing work tree: 100%(xxx/xxx), done.
复制代码
编译命令仍是在下载源码的目录下输入,即/Volumes/android/aosp
。
因为Android编译只能使用bash,因此你若是使用zsh等其它的shell,须要切换至bash,切换命令以下:
$ chsh -s /bin/bash
复制代码
而后重启终端才能生效。
使用envsetup.sh
脚本初始化环境:
$ source build/envsetup.sh
复制代码
输入lunch
,会提示以下:
$ 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_car_arm-userdebug 8. aosp_car_arm64-userdebug 9. aosp_car_x86-userdebug 10. aosp_car_x86_64-userdebug 11. mini_emulator_arm64-userdebug 12. m_e_arm-userdebug 13. m_e_mips-userdebug 14. m_e_mips64-eng 15. mini_emulator_x86-userdebug 16. mini_emulator_x86_64-userdebug 17. uml-userdebug 18. aosp_crosshatch-userdebug 19. aosp_blueline-userdebug 20. aosp_cf_x86_auto-userdebug 21. aosp_cf_x86_phone-userdebug 22. aosp_cf_x86_tablet-userdebug 23. aosp_cf_x86_tablet_3g-userdebug 24. aosp_cf_x86_tv-userdebug 25. aosp_cf_x86_wear-userdebug 26. aosp_cf_x86_64_auto-userdebug 27. aosp_cf_x86_64_phone-userdebug 28. aosp_cf_x86_64_tablet-userdebug 29. aosp_cf_x86_64_tablet_3g-userdebug 30. aosp_cf_x86_64_tv-userdebug 31. aosp_cf_x86_64_wear-userdebug 32. cf_x86_auto-userdebug 33. cf_x86_phone-userdebug 34. cf_x86_tablet-userdebug 35. cf_x86_tablet_3g-userdebug 36. cf_x86_tv-userdebug 37. cf_x86_wear-userdebug 38. cf_x86_64_phone-userdebug 39. cf_x86_64_tablet-userdebug 40. cf_x86_64_tablet_3g-userdebug 41. cf_x86_64_tv-userdebug 42. cf_x86_64_wear-userdebug 43. aosp_marlin-userdebug 44. aosp_marlin_svelte-userdebug 45. aosp_sailfish-userdebug 46. aosp_walleye-userdebug 47. aosp_walleye_test-userdebug 48. aosp_taimen-userdebug 49. hikey-userdebug 50. hikey64_only-userdebug 51. hikey960-userdebug Which would you like? [aosp_arm-eng] 复制代码
这里列出了全部的可编译的目标。全部编译目标都采用 BUILD-BUILDTYPE
形式,其中 BUILD
是表示特定功能组合的代号,BUILDTYPE
是如下类型之一,表示在什么环境下运行:
编译类型 | 使用状况 |
---|---|
user | 权限受限;适用于生产环境 |
userdebug | 与“user”相似,但具备 root 权限和可调试性;是进行调试时的首选编译类型 |
eng | 具备额外调试工具的开发配置 |
好比aosp_arm-eng
的BUILD
为aosp_arm
,BUILDTYPE
为eng
,aosp
表明Android开源项目,arm
表示系统是运行在arm架构的处理器上,更多见官方文档。
若是你有Pixel或Nexus真机,选择对应的编译目标便可,不然咱们就选模拟器,即5.aosp_x86-eng
。在上面的Which would you like? [aosp_arm-eng]
后面输入5
便可。也直接选择:
$ lunch 5
复制代码
或
$ lunch aosp_x86-eng
复制代码
在编译以前,经过输入如下命令,能够查看你的Mac的CPU核数:
$ sysctl -n machdep.cpu.core_count
4
复制代码
上面输出的CPU核数为4,则能够启动4个线程来编译源码:
$ make -j4
复制代码
接下来就是漫长的编译过程,通常须要2~3个小时,若是看到下面的提示,则表示已经编译完成:
#### build completed successfully (02:15:52 (hh:mm:ss)) ####
复制代码
在编译完成后,终端里输入如下命令,即可使用模拟器来运行编译的版本:
$ emulator
复制代码
编译完成以后,运行如下命令,编译idegen模块:
$ mmm development/tools/idegen/
复制代码
idegen模块编译完成后会有以下提示:
#### make completed successfully (46 seconds) ####
复制代码
此时继续运行下面的命令,会在aosp根目录生成对应的android.ipr
、android.iml
IDEA工程配置文件
$ development/tools/idegen/idegen.sh
Read excludes: 17ms
Traversed tree: 218013ms
复制代码
启动Android Studio,选择一个已存在的Android Studio项目,而后选中aosp根目录下的android.ipr
文件,若是提示须要你convert,确认convert就行。首次导入大概须要20~30分钟:
导入成功后,在Project Structure中,选择Modules,删除全部的依赖,只保留下图所示。这样在查看源码跳转的时候,不会进入android.jar,会直接跳转至本项目的文件中。
如今你能够自由地阅读源码了,enjoy it!
Google官方Android源码编译教程 清华大学开源软件镜像站 - Android 镜像使用帮助 macOS(Sierra 10.12)上Android源码(AOSP)的下载、编译与导入到Android Studio Android AOSP基础(二)AOSP源码下载 Android AOSP基础(三)Android系统源码的整编和单编 使用Android Studio导入源码