Ubuntu下获取安卓源码并编译

目录

背景

获取源码

安装repo

下载源码压缩包

同步

编译

安装lunch

设置环境

编译

使用AS查看源码

编译idegen部分

优化读取选项

查看源码

结语

背景

获取安卓源码建议在Linux或Mac下进行,硬盘空间起码250G,因为源码压缩包有119G,而且要保证网络通畅

获取源码

安装repo

root@rtlab-computer:/home/rtlab/szc# apt install repo

检查下repo版本

root@rtlab-computer:/home/rtlab/szc# repo
warning: Python 3 support is currently experimental. YMMV.
Please use Python 2.6 - 2.7 instead.


... A new version of repo (2.15) is available.
... You should upgrade soon:
    cp /home/rtlab/szc/AndroidSource/aosp/.repo/repo/repo /usr/bin/repo


usage: repo COMMAND [ARGS]
The most commonly used repo commands are:
  abandon        Permanently abandon a development branch
  branch         View current topic branches
  branches       View current topic branches
  checkout       Checkout a branch for development
  cherry-pick    Cherry-pick a change.
  diff           Show changes between commit and working tree
  diffmanifests  Manifest diff utility
  download       Download and checkout a change
  gitc-delete    Delete a GITC Client.
  gitc-init      Initialize a GITC Client.
  grep           Print lines matching a pattern
  info           Get info on the manifest branch, current branch or unmerged branches
  init           Initialize a repo client checkout in the current directory
  list           List projects and their associated directories
  overview       Display overview of unmerged project branches
  prune          Prune (delete) already merged topics
  rebase         Rebase local branches on upstream branch
  smartsync      Update working tree to the latest known good revision
  stage          Stage file(s) for commit
  start          Start a new branch for development
  status         Show the working tree status
  sync           Update working tree to the latest revision
  upload         Upload changes for code review
See 'repo help ' for more information on a specific command.
See 'repo help --all' for a complete list of recognized commands.
Bug reports: https://bugs.chromium.org/p/gerrit/issues/entry?template=Repo+tool+issue
root@rtlab-computer:/home/rtlab/szc#

目前我用的Python版本为3,上面的提示信息说最好使用Python2.6-2.7,因为repo对Python3的支持目前只是实验版,但是使用python2.7同步最新的aosp源码会报错,python3就不会,因此还是推荐使用Python3。

新建个工作目录,我们以后对AOSP的操作都在这个目录下进行

root@rtlab-computer:/home/rtlab/szc# mkdir AndroidSource && cd AndroidSource

下载源码压缩包

这里推荐中科大的镜像,清华的这两天下载速度非常不稳定

root@rtlab-computer:/home/rtlab/szc/AndroidSource# wget http://mirrors.ustc.edu.cn/aosp-monthly/aosp-latest.tar

压缩包总大小为119G,我这里的下载速度为10M,也下了四个小时。

下载完成后,解压,解压时间也比较长

root@rtlab-computer:/home/rtlab/szc/AndroidSource# tar -xf aosp-latest.tar
root@rtlab-computer:/home/rtlab/szc/AndroidSource# cd aosp/

同步

首先我们要修改同步地址为中科大的地址

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# vim .repo/manifests.git/config

把[remote "origin"]标签在的url改成中科大的就行

url =  git://mirrors.ustc.edu.cn/aosp/platform/manifest

然后设置git的文件模式

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# git config core.filemode false

最后同步

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# repo sync

如果中途有部分代码同步失败,别急着ctrl-c,待repo sync命令自行结束后,运行以下命令对同步失败的部分重新进行同步,直到完成

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# repo sync -j1 --fail-fast

结束后,我们可以得到下面的目录信息

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# repo sync -j1 --fail-fast
warning: Python 3 support is currently experimental. YMMV.
Please use Python 2.6 - 2.7 instead.


... A new version of repo (2.15) is available.
... You should upgrade soon:
    cp /home/rtlab/szc/AndroidSource/aosp/.repo/repo/repo /usr/bin/repo


Fetching: 100% (1022/1022), done in 21m15.123s
Garbage collecting: 100% (1022/1022), done in 11.650s
Checking out: 100% (1022/1022), done in 57.294s
repo sync has finished successfully.
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# ls
Android.bp  Makefile   art     bootable        build          cts     developers   device    frameworks  kernel   libnativehelper  pdk               prebuilts  system  toolchain
BUILD       WORKSPACE  bionic  bootstrap.bash  compatibility  dalvik  development  external  hardware    libcore  packages         platform_testing  sdk        test    tools
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp#

编译

安装lunch

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# apt install python-lunch

设置环境

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# source build/envsetup.sh

编译

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# lunch aosp_arm-eng && make -j4

要是编译一开始就报错Failed to create parent directories of empty ninja glob file 'XXX/aosp/.bootstrap/build-globs.ninja': mkdir XXX/aosp/.bootstrap permission denied,就手动创建这个目录、并设置aosp目录权限为777,再重新编译

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# mkdir -p /home/rtlab/szc/AndroidSource/aosp/.bootstrap && chmod -R 777 .
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# make -j4

贴一下编译成功的截图(其实编译了一上午,第一次编译编译到一半儿失败了,把整个aosp目录权限设为777,再进行编译才成功)

Ubuntu下获取安卓源码并编译_第1张图片

使用AS查看源码

编译idegen部分

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# make idegen -j4
root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# development/tools/idegen.sh

完成后的截图如下

Ubuntu下获取安卓源码并编译_第2张图片

优化读取选项

打开aosp根目录下的android.iml

root@rtlab-computer:/home/rtlab/szc/AndroidSource/aosp# vim android.iml

先把所有orderEntry标签删到只剩下下面三行

    
    
    

然后在上面,加入过滤目录,以便只打开framework源码

.……





























    
    
    
    
  

查看源码

打开Android Studio,选择android.ipr打开

Ubuntu下获取安卓源码并编译_第3张图片

最后得到的结果如下图所示:

Ubuntu下获取安卓源码并编译_第4张图片

虽然一片报红,但java层的代码都是能直接按住ctrl + 鼠标左键点击查看的,但是native层就不行了,所以看native层还是需要到jni目录下去查看(在此可以使用sublime-text,或者命令行中也可以):

Ubuntu下获取安卓源码并编译_第5张图片

结语

最后推荐一个在线查看Android源码(java层和C层)的网站:http://androidxref.com/

你可能感兴趣的:(安卓开发,android)