背景
上一篇文章已经介绍了VSCode + clangd搭建aosp c++源码环境,
https://mp.weixin.qq.com/s/Pw5NYfnkDFDSEiekoRAm-g
不过文章中介绍的是对过大的compile_commands.json进行精简的方式。
本文采用另一种不对compile_commands.json进行任何的精简,而是在.vscode/settings.json中进行一些模块排除方式,这种方式相对来说更加方便一些,毕竟不需要每次对json进行精简。
为什么要用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
后续也可以直接使用这个json,这里采用不做任何精简json的方法,那么接下来就需要创建或者修改.vscode/settings.json。
手动创建.vscode/settings.json
源码根目录
mkdir .vscode
然后创建
具体详细内容如下:
大家要根据自己的clangd的path进行修改
{
// ==================== clangd 核心配置 ====================
// ⚠️ 必须显式指定 clangd 可执行文件路径!
// 否则 VSCode 可能使用内置下载的旧版本或找不到 clangd
"clangd.path": "/home/test/aosp15/prebuilts/clang/host/linux-x86/clang-r522817/bin/clangd",
// clangd 启动参数
"clangd.arguments": [
// 指定编译数据库目录(AOSP 全量 compile_commands.json 所在目录)
"--compile-commands-dir=${workspaceFolder}/out/soong/development/ide/compdb",
// 后台建立索引,避免打开工程时全量索引导致卡死
"--background-index",
// 详细补全样式(可选:detailed / bundled)
"--completion-style=detailed",
// 禁止自动插入头文件,减少干扰
"--header-insertion=never",
// 授权 clangd 查询系统编译器,解决部分编译标志无法识别的问题
"--query-driver=/usr/bin/gcc,/usr/bin/g++",
// 限制补全/引用结果数量,防止卡顿
"--limit-results=100",
// 日志级别(调试时可改为 verbose)
"--log=error",
// 使用所有可用 CPU 核心索引(根据机器性能调整)
// "-j=8"
],
// clangd 索引线程数(根据 CPU 核心数调整)
"clangd.indexingThreads": 4,
// clangd 崩溃后自动重启
"clangd.restartAfterCrash": true,
// clangd 检查更新(建议关闭,避免频繁弹窗)
"clangd.checkUpdates": false,
// clangd 完成时是否展示文档
"clangd.onConfigChanged": "restart",
// ==================== 禁用微软 C/C++ 扩展的 IntelliSense ====================
// 如果安装了微软 C/C++ 扩展,必须禁用其智能感知,否则与 clangd 冲突
"C_Cpp.intelliSenseEngine": "disabled",
"C_Cpp.autocomplete": "disabled",
// ==================== 文件监视排除 ====================
// 排除大目录,减少 VSCode 自身扫描负担,间接提升 clangd 响应速度
"files.watcherExclude": {
"**/out/**": true,
"**/.repo/**": true,
"**/prebuilts/**": true,
"**/external/**": true,
"**/art/**": true,
"**/cts/**": true,
"**/test/**": true,
"**/.git/**": true,
"**/__pycache__/**": true,
"**/node_modules/**": true
},
// ==================== 搜索排除 ====================
"search.exclude": {
"**/out/**": true,
"**/.repo/**": true,
"**/prebuilts/**": true,
"**/external/**": true,
"**/art/**": true,
"**/cts/**": true,
"**/test/**": true,
"**/.git/**": true
},
// ==================== 编辑器通用配置(可选) ====================
// 关闭保存时自动格式化,避免大文件卡顿
"editor.formatOnSave": false,
// 设置文件关联,让 VSCode 识别更多 AOSP 文件类型
"files.associations": {
"*.bp": "json",
"*.mk": "makefile",
"Android.bp": "json",
"*.rc": "properties"
},
// 关闭小地图,减少渲染开销
"editor.minimap.enabled": false,
// ==================== 针对 Java 文件的额外优化 ====================
// AOSP 包含大量 Java 代码,clangd 主要处理 C/C++,Java 可交给其他扩展
// 如果你不需要 Java 索引,可以在此排除
// "[java]": {
// "editor.wordWrap": "on"
// }
}
VSCode打开aosp源码目录
上面的 compile_commands.json本身在aosp源码的根目录,VSCode直接源码根目录就可以对代码进行浏览了

但是发现点击跳转有些问题,经过追踪发现属于clangd版本要与aosp编译时候的clangd版本要一致,所以要进行配置相关的clangd对应bin文件路径问aosp下面的,修改如下:
在 .vscode/settings.json 中把 clangd 指向与代码库匹配的预编译 clangd 18:
总结
确实使用clangd发现代码的跳转更加准确了,而且在编写代码时候发现提示的也是很准确的,和写java代码一样准确,效率确实是大大提升。

6177



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



