The clang compilation database (compile_commands.json) is a json file that has instructions on how to compile your file. If your project uses soong, you can follow the instructions here

However, if your project does not yet build with soong, you can use bear to generate it from make files on Android.

  1. Install Bear
  2. Create an incremental_build.sh script with the following contents and put it somwhere, like in ~/bin
1
2
3
4
5
6
cd $ANDROID_BUILD_TOP
. build/envsetup.sh
lunch $TARGET_PRODUCT-userdebug
cd $1
find . | xargs -I {} touch {}
mm -j32
  1. Run the following on the target you’d like the compile_commands.json for, for example - you can do the following for surfaceflinger
1
2
bear bash ~/bin/incremental_build.sh \
    frameworks/native/services/surfaceflinger