背景
近期有学员在vip群中问关于c++代码的查看软件vscode相关一些跳转问题,其实我们原来在课程中也给大家有分享过相关的vscode加载调试aosp的c++部分代码。
可以看如下链接:
https://www.bilibili.com/video/BV1xa411h7CL/
我们当时使用主要还是微软的这个c++插件

在使用这个插件查看aosp的c++代码,发现是大部分可以跳,但是也有小部分不可以,所以也不是很影响,但我们vip群学员有提出有更好的方案,那就是使用vscode + clangd的方案。
所以今天就带大家来实战搭建一下vscode + clangd的环境。
为什么要用clangd?
Android Open Source Project (AOSP) 是 Android 系统的开源项目,包含了 Android 系统的源代码、构建脚本、文档等。在进行 AOSP 的开发时,需要使用到 C++ 编程语言,因此需要一个好的开发环境来提高开发效率。
安装clangd的插件、禁用微软的IntelliSense
下载clangd插件,并且进行enable

因为会和微软的插件冲突,所以要进disable微软的。

准备compile_commands.json文件
clangd插件需要借助compile_commands.json文件,这个文件需要我们在aosp源码中进行编译生成。
编译方式参考aosp源码的md文件
build/soong/docs/compdb.md
# Compdb (compile\_commands.json) Generator
Soong can generate compdb files. This is intended for use with editing tools
such as YouCompleteMe and other libclang based completers.
compdb file generation is enabled via environment variable:
```bash
$ export SOONG_GEN_COMPDB=1
$ export SOONG_GEN_COMPDB_DEBUG=1
One can make soong generate a symlink to the compdb file using an environment
variable:
$ export SOONG_LINK_COMPDB_TO=$ANDROID_HOST_OUT
You can then trigger an empty build:
$ make nothing
Note that if you build using mm or other limited makes with these environment
variables set the compdb will only include files in included modules.
执行后就会在out目录生成对应的文件,路径为:out/soong/development/ide/compdb/compile_commands.json
看看大小
```bash
test@test:~/aosp15$ ls -l out/soong/development/ide/compdb/compile_commands.json
-rw-rw-r-- 1 test test 561370807 Sep 8 2025 out/soong/development/ide/compdb/compile_commands.json
可以看出明显太大了,500多m,直接vscode等无法打开,打开就报错。
也不建议直接用这个json文件,所以需要对这个文件进行精简。
精简compile_commands.json
精简 compile_commands.json
1 数据库格式
每条记录结构(soong 生成,单行 JSON 数组):
{
"directory": "/media/test/.../home/test/aosp15",
"arguments": ["prebuilts/clang/host/linux-x86/clang-r522817/bin/clang++", "-I...", "..."],
"file": "frameworks/base/libs/hwui/jni/pdf/PdfEditor.cpp"
}
directory:绝对路径(AOSP 根目录),全部 76246 条记录相同file:相对路径arguments:编译参数,其中-I、--sysroot、--gcc-toolchain、clang 路径等全是相对路径,没有绝对路径
2 过滤脚本
文件:filter_compile_commands.py,核心逻辑是流式扫描顶层对象(带字符串感知的括号匹配),按 "file" 前缀过滤:
SRC = "out/soong/development/ide/compdb/compile_commands.json"
DST = "compile_commands.filtered.json"
# 自定义要包含的目录(相对 "file" 路径的前缀)
include_dirs = [
"frameworks/base/",
"system/core/",
"bionic/",
"external/libcxx/",
]
markers = [('"file":"' + p).encode() for p in include_dirs]
(完整脚本 filter_compile_commands.py请在vip学员群中或者私聊马哥获取)
运行:
python3 filter_compile_commands.py
3 过滤结果
done: total=76246 kept=2979
output: compile_commands.filtered.json (22.3 MB)
frameworks/base 1103
system/core 559
bionic/* 1317 (libc 509, tests 346, libm 215, benchmarks 199, linker 43, ...)
external/libcxx 0 ← 无条目
说明:
external/libcxx没有任何编译条目 —— AOSP 实际使用 toolchain 预编译的 libc++(prebuilts/clang/...),external/libcxx源码在完整构建里不会被编译,所以过滤不到,属正常现象。
4 切换到精简版
# 备份原软链(原 536MB 文件仍在 out/ 下,不受影响)
mv compile_commands.json compile_commands.full.json
# 用精简版替换
mv compile_commands.filtered.json compile_commands.json
结果:
compile_commands.json 23M ← clangd 实际加载的精简版
compile_commands.full.json 软链 → out/soong/.../compile_commands.json(备份)
恢复方法:mv compile_commands.full.json compile_commands.json
VSCode打开aosp源码目录
上面的 compile_commands.json本身在aosp源码的根目录,VSCode直接源码根目录就可以对代码进行浏览了

但是发现点击跳转有些问题,经过追踪发现属于clangd版本要与aosp编译时候的clangd版本要一致,所以要进行配置相关的clangd对应bin文件路径问aosp下面的,修改如下:
在 .vscode/settings.json 中把 clangd 指向与代码库匹配的预编译 clangd 18:
{
"clangd.path": "/home/test/aosp15/prebuilts/clang/host/linux-x86/clang-r522817/bin/clangd",
"clangd.arguments": [
"--background-index",
"--log=error"
],
"files.associations": { "..." }
}

总结
确实使用clangd发现代码的跳转更加准确了,而且在编写代码时候发现提示的也是很准确的,和写java代码一样准确,效率确实是大大提升。

原文地址:
https://mp.weixin.qq.com/s/Pw5NYfnkDFDSEiekoRAm-g?token=697945935&lang=zh_CN
448



被折叠的 条评论
为什么被折叠?



