android.toolchain.cmake) that configures CMake for cross-compilation.
Toolchain configuration
To use the Android toolchain, specify it when invoking CMake:Platform variables
These variables configure the target Android platform and architecture.Target Application Binary Interface (ABI). Determines the instruction set and calling conventions.
Supported ABI values
| ABI | Description | Architecture |
|---|---|---|
arm64-v8a | ARMv8-A 64-bit | Modern ARM devices (recommended) |
armeabi-v7a | ARMv7-A 32-bit with NEON | Older ARM devices |
x86_64 | x86 64-bit | Intel/AMD 64-bit emulators and devices |
x86 | x86 32-bit | Intel/AMD 32-bit emulators |
For new applications, target
arm64-v8a and x86_64. The armeabi, mips, and mips64 ABIs are deprecated and removed.Minimum Android API level to target. Determines available APIs and system libraries.
Android 5.0 (API 21) is the minimum supported version for 64-bit ABIs. Use API 21 or higher for modern applications.
Path to the Android NDK. Automatically set by Android Studio, but can be specified manually.
C++ configuration
CMake provides variables to configure the C++ standard library and language features.C++ Standard Library implementation to use. Default is
c++_shared.STL options
| Option | Description | Use case |
|---|---|---|
c++_shared | LLVM libc++ shared library | Recommended for most apps |
c++_static | LLVM libc++ static library | Single shared library projects |
none | No C++ standard library | C-only projects |
system | System C++ runtime | Deprecated, minimal support |
If using
c++_static, ensure all shared libraries in your app use the same STL to avoid violations of the One Definition Rule (ODR).C++ language standard version. Common values:
11, 14, 17, 20.Additional C++ compiler flags.
Additional C compiler flags.
Compilation settings
ARM instruction set mode for 32-bit ARM builds:
arm (32-bit) or thumb (16-bit, default).Enable ARM NEON SIMD instructions for
armeabi-v7a. Default is TRUE.Disable compiler format string security checks. Default is
FALSE. Not recommended.Path to ccache executable for faster rebuilds.
Linking configuration
Linker flags for shared libraries.
Linker to use:
lld (default, recommended) or deprecated (old GNU linkers).The LLD linker is faster and produces smaller binaries. The deprecated GNU linkers (gold, bfd) are removed in newer NDK versions.
Library linking
Link Android system libraries usingtarget_link_libraries:
Common Android libraries
| Library | Description | Use case |
|---|---|---|
log | Android logging | Debug and diagnostic output |
android | Android native app glue | NativeActivity support |
EGL | EGL graphics | OpenGL ES context management |
GLESv2 | OpenGL ES 2.0 | 2D/3D graphics rendering |
GLESv3 | OpenGL ES 3.0+ | Advanced graphics features |
OpenSLES | OpenSL ES | Low-latency audio |
mediandk | Media APIs | Video/audio codec access |
camera2ndk | Camera2 NDK | Camera hardware access |
vulkan | Vulkan graphics | Modern graphics API |
jnigraphics | Bitmap access | Direct bitmap manipulation |
z | zlib compression | Data compression |
Complete CMakeLists.txt example
Build types
CMake supports different build configurations:Build configuration:
Debug, Release, RelWithDebInfo, or MinSizeRel.| Build Type | Optimization | Debug Info | Use case |
|---|---|---|---|
Debug | None (-O0) | Full (-g) | Development and debugging |
Release | Maximum (-O3) | None | Production builds |
RelWithDebInfo | Optimized (-O2) | Full (-g) | Profiling and stack traces |
MinSizeRel | Size (-Os) | None | Size-constrained environments |
Architecture-specific code
Use CMake variables to conditionally compile architecture-specific code:Useful CMake variables
| Variable | Description |
|---|---|
ANDROID | Always TRUE when using Android toolchain |
ANDROID_ABI | Target ABI (e.g., arm64-v8a) |
ANDROID_PLATFORM_LEVEL | Numeric API level (e.g., 21) |
CMAKE_ANDROID_ARCH_ABI | Same as ANDROID_ABI |
CMAKE_SYSTEM_NAME | Always Android |
CMAKE_SYSTEM_VERSION | Android API level |
Finding and using libraries
Using find_library
Find system libraries at runtime:Importing prebuilt libraries
Using external projects
Gradle integration
Android Studio uses Gradle to invoke CMake. Configure CMake inbuild.gradle:
Gradle CMake arguments
| Argument | Description |
|---|---|
cppFlags | Additional C++ compiler flags |
cFlags | Additional C compiler flags |
abiFilters | List of ABIs to build |
arguments | Additional CMake arguments |
targets | Specific CMake targets to build |
Advanced configuration
Custom toolchain configuration
Strip symbols in release builds
Multiple ABI builds
For production apps, build for at least
arm64-v8a and armeabi-v7a to support both modern and older devices. The Play Store requires 64-bit support.