Boost Android Native C++ Quality with Clang‑Tidy and the C++ Core Guidelines
This article explains how to integrate the C++ Core Guidelines checker via Clang‑Tidy into Android C++ projects, covering setup in ndk‑build and Android Studio, using quick‑fixes to automatically refactor code, generating a JSON compilation database, and highlighting the guidelines' impact on modern C++ talent development.
Background
The C++ Core Guidelines (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) were created by Bjarne Stroustrup to address safety and performance issues in modern C++. They are a comprehensive, authoritative set of best‑practice recommendations for C++14 and later, extending earlier works like Effective C++.
Documentation: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
Guideline Support Library: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#gsl-guidelines-support-library
Checker tools are now well supported in Microsoft Visual Studio.
How to Do It
Clang‑Tidy
Clang‑Tidy, built on clang/LLVM, is a mature C++ linter. It can be integrated into Android NDK build systems and Android Studio CMake workflows to enforce the Core Guidelines.
ndk‑build
APP_CLANG_TIDY := trueAndroid Studio 3.3+
Android Studio 3.3 and later support Clang‑Tidy. The steps are:
Enable Clang‑Tidy inspection (see image below).
Adjust the checks you want to apply in the inspection panel.
Run the inspection.
Apply quick‑fixes via the light‑bulb icon.
In the inspection panel you can select from a large number of checks.
Running the inspection produces many warnings (over 1,100 in the author's project) but no errors. Quick‑fixes allow automatic refactoring, reducing temporary objects and improving runtime efficiency.
After becoming familiar with the checks and their quick‑fixes, batch application is possible. Example commit summary:
commit db6625732e09156501f32f7be0f2e5515e44ea11 (HEAD -> cpp_core_guideline_warning_fix, origin/cpp_core_guideline_warning_fix)
Author: xide.wf
Date: Thu Aug 31 20:50:36 2023 +0800
Through Clang‑Tidy basic cpp core guideline validation, added corresponding modifications.
android/README.md | 11 ++++++++++-
core/filters/bloom_filter_define.cpp | 18 +++++++++---------
... (many files omitted) ...
179 files changed, 1153 insertions(+), 1230 deletions(-)JSON Compilation Database
The JSON compilation database is a universal format for sharing build parameters across tools. It is useful for IDEs like VS Code that rely on compile_commands.json.
CMake
Enable with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON. Android CMake builds enable this by default.
ndk‑build
Since NDK r18, ndk‑build can generate a JSON compilation database via ndk-build compile_commands.json (no build) or ndk-build GEN_COMPILE_COMMANDS_DB=true (build and generate).
Implications for C++ Talent Development
The author lists several C++ books and notes that modern C++ education must combine broad knowledge with practical experience. The Core Guidelines can serve as a catalyst for training modern C++ developers or for evaluating a migration to Rust.
Key benefits of following the C++ Core Guidelines include higher code quality, better maintainability, improved extensibility, increased performance, smoother team collaboration, and reduced defect risk.
Alipay Experience Technology
Exploring ultimate user experience and best engineering practices
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
