type
status
date
slug
summary
tags
category
icon
password
URL
In the realm of Android development, efficiency is paramount. A streamlined build process and effective code completion tools can significantly enhance productivity.
Introduction
Android's build system is powerful but can be complex, especially when working with large codebases. Developers often face challenges like lengthy build times and inadequate code completion in C/C++ projects. By optimizing the build environment and configuring tools like
clangd, developers can significantly improve their coding efficiency and overall development experience.Generating compile_commands.json
A
compile_commands.json file is essential for advanced code analysis and enabling intelligent code completion in editors and IDEs. Here's how to generate it in the Android build environment:- Set the Environment Variable:
Enable Soong's compilation database generation by setting the
SOONG_GEN_COMPDB environment variable:- Load the Build Environment:
Source the build environment setup script:
- Select the Build Target:
Choose the appropriate build target for your device or project:
- Start the Build Process:
Initiate the build:
Note: The initial build may take considerable time depending on your system's performance.
- Locate and Link
compile_commands.json:
After the build, find the generated
compile_commands.json:It is typically located at
out/soong/development/ide/compdb/compile_commands.json. Create a symbolic link in the project's root directory:Optimizing Soong's Compilation Database
By default, Soong generates a comprehensive
compile_commands.json, which can be excessively large and unwieldy. To optimize and reduce its size, consider the following methods:1. Generate Compilation Database for Specific Modules
Limit the compilation database to specific modules by setting
SOONG_GEN_COMPDB to the module name:Example:
Now, Soong will only generate compile commands for the
libart module, reducing the size of compile_commands.json.2. Use Path Filters
Filter the files included in the compilation database based on their paths using the
SOONG_COMPDB_FILTER environment variable:This command ensures that only files within the
frameworks/base directory are included.3. Exclude Specific Modules
Exclude unwanted modules from the compilation database using
SOONG_COMPDB_MODULES_EXCLUDE:Example:
4. Merge Multiple Compilation Databases
If you need to generate compile commands for multiple modules separately and then merge them:
- Generate
compile_commands.jsonfor each module:
- Merge them using
jq:
5. Utilize Soong's Filters in Build Configuration
Modify Soong's build configuration files to fine-tune which files and modules are included in the compilation database. This approach requires a deeper understanding of Soong's build scripts and is recommended for advanced users.
Enhancing C/C++ Code Completion with clangd
clangd is a language server protocol (LSP) server that provides IDE-like features for C/C++ code, such as code completion, navigation, and refactoring. Leveraging clangd can greatly enhance your coding efficiency.Installing clangd
Ensure that
clangd is installed on your system:- Ubuntu/Debian:
- macOS (using Homebrew):
Add
clangd to your PATH if necessary:- Other Platforms:
Download pre-built binaries from the official LLVM releases or build from source.
Configuring Your Code Editor
Visual Studio Code (VSCode)
- Install the
clangdExtension:
Go to the Extensions view (
Ctrl+Shift+X), search for clangd, and install the official extension.- Configure Workspace Settings:
-compile-commands-dir=.tellsclangdwhere to findcompile_commands.json.-background-indexenables indexing in the background.- Disabling the built-in IntelliSense avoids conflicts.
Create or edit
.vscode/settings.json in your project root:Vim/Neovim
- Install a Language Client Plugin:
Use a plugin like
coc.nvim:- Install the
coc-clangdExtension:
In Vim/Neovim, run:
- Configure
coc-settings.json:
Create or edit
~/.vim/coc-settings.json or ~/.config/nvim/coc-settings.json:Verifying Code Completion
Open a C/C++ source file and start typing.
clangd should provide accurate code completions, syntax highlighting, and allow navigation to definitions and references.Optimizing clangd Performance
Given the size of the Android codebase,
clangd may consume significant system resources. Optimize its performance with these strategies:1. Configure Cache Directory
Set a dedicated cache directory for
clangd to improve indexing speed:2. Limit Indexing Threads
Control the number of threads used for indexing to prevent excessive CPU usage:
Adjust
-j=4 according to your CPU's capabilities.3. Exclude Unnecessary Directories
Create a
.clangd configuration file in your project root to exclude irrelevant directories:This configuration tells
clangd to skip indexing the specified directories, conserving resources.Further Enhancements for Code Completion
1. Use Precompiled Headers (PCH)
Enabling precompiled headers can speed up both compilation and code analysis. Configure your build system to generate and use PCH files, which may involve modifying
Android.bp or Android.mk files.2. Customize Completion Behavior
Fine-tune how code completion behaves in your editor:
- Trigger Characters: Adjust settings to specify which characters trigger completion suggestions.
- Sorting and Filtering: Configure how completion items are sorted and filtered to prioritize relevant suggestions.
- Snippet Support: Enable or disable snippet completions based on your preference.
3. Automate Compilation Database Updates
Create scripts to regenerate
compile_commands.json when source files or build configurations change. This ensures clangd always has up-to-date information.References:
Β