Migrating from NDK-Build to CMake in Android Studio: Step-by-Step
For many Android developers, transitioning from ndk-build to CMake offers a more flexible and powerful way to manage native code compilation. While both systems serve the purpose of building native libraries for Android apps, CMake provides cross-platform support and better integration with modern Android Studio workflows . If you’re looking to make the switch, here’s a step-by-step guide to help you through the migration process.
Why Migrate to CMake?
CMake is an open-source, cross-platform build system that allows you to manage the build process using a single configuration file—CMakeLists.txt
. Unlike ndk-build, which relies on Android.mk
and Application.mk
, CMake uses a higher-level abstraction that makes it easier to handle complex builds across different platforms .
Additionally, CMake supports Ninja, a small build system designed to minimize build times, making your development cycle faster and more efficient . With built-in support in Android Studio, CMake has become the preferred tool for integrating native code into Android applications.
Step 1: Install the NDK and CMake
Before beginning the migration, ensure that both the Android NDK and CMake are installed via the SDK Manager:
- Open your project in Android Studio.
- Navigate to Tools > SDK Manager.
- Click on the SDK Tools tab.
- Check the boxes for NDK (Side by side) and CMake, then click Apply to install them .
These tools will enable you to compile and integrate native code using CMake without leaving the Android Studio environment.
Step 2: Configure Gradle to Use CMake
Once the necessary tools are installed, update your module-level build.gradle
file to point to your CMakeLists.txt
file:
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.22.1" // Optional: specify CMake version
}
}
}
This configuration tells Gradle to use CMake to build your native code located in the cpp
directory. Make sure the path
matches the location of your CMakeLists.txt
file .
Step 3: Create or Update Your CMakeLists.txt File
If you already have an Android.mk
file, you can use it as a reference when creating your CMakeLists.txt
. Here’s a basic example:
cmake_minimum_required(VERSION 3.22.1)
project(MyApplication)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
This script defines how CMake should compile your native source files and link them with any required NDK libraries .
Step 4: Migrate Source Files and Dependencies
Ensure all source files referenced in your Android.mk
are present in your project structure and correctly listed in the add_library()
command within your CMakeLists.txt
.
You may also need to manually add any third-party or prebuilt libraries that were previously referenced in your Android.mk
file. CMake allows you to include other CMake projects or link directly to binaries using find_library()
or add_library()
commands .
Step 5: Build and Test
After configuring everything, sync your Gradle files and try building your project. If there are any issues, Gradle will output detailed logs that can help identify misconfigurations or missing dependencies.
If you encounter errors related to the build system not recognizing changes, you can force a clean rebuild using the Build > Clean Project and Build > Rebuild Project options in Android Studio .
Conclusion
Migrating from ndk-build to CMake in Android Studio streamlines the management of native code and enhances compatibility across platforms. By following these steps, you can transition smoothly while leveraging the power of CMake’s advanced features like Ninja builds and modular configuration .
Whether you’re starting fresh or updating an existing project, adopting CMake is a smart move toward a more maintainable and scalable build process. As Android Studio continues to evolve, so too should your development practices—making CMake a key part of your toolkit.