背景

市面上大部分 Android Framework 相关书籍刚开始都是搭建开发环境,下载相关源码。但是相关下载内容一般都是过时的,这一步往往劝退了很多人,曾经我也是。所以这里给出了一种新的阅读源码的方式:VSCode + Ctags。

Android 源码下载:https://android.googlesource.com/platform/frameworks/base/

配置

配置 Ctags

ctags 仓库地址:https://github.com/universal-ctags/ctags

配置步骤:

1
2
3
4
5
6
$ git clone https://github.com/universal-ctags/ctags.git
$ cd ctags
$ ./autogen.sh
$ ./configure --prefix=/where/you/want # defaults to /usr/local
$ make
$ make install # may require extra privileges depending on where to install

存在的问题:

我遇到的 pkg-config not found 具体情况如下:

1
2
configure: error: Either a previously installed pkg-config or "glib-2.0 >= 2.16" could not be found. 
Please set GLIB_CFLAGS and GLIB_LIBS to the correct values or pass --with-internal-glib to configure to use the bundled copy.

最终是参考:https://github.com/Automattic/node-canvas/wiki/Installation%3A-Mac-OS-X 中的解决方案。

配置 VSCode

  • 打开VSCode,下载 ctagsx 插件。
  • 进入 framework 所在目录,执行如下所示的官网命令,之后用 VSCode 打开framework 所在目录即可。
1
2
# 这一步主要是为了生成一些索引文件,这样就可以在 VSCode 中直接跳转
ctags --tag-relative --extra=f -R .

如何使用

  1. 快速找到指定类文件:Command + Shift + F
  2. 如何查看一个文件在项目中的位置:点击该文件即可
  3. 如何跳转:按住 Command,鼠标点击想要跳转的代码

如果跳转的可能性只有一个,点击后就会直接跳转。否则就如下图所示一样,需要选择跳转到指定处。