I have tried to give some explanation to identify the different between CMake and NDK-Build and setup:
Some initial notes:
- Android Studio's default build tool for native libraries is CMake.
- Android Studio also supports ndk-build due to the large number of existing projects that use the build toolkit to compile their native code.
- If you are creating a new native library, you should use CMake.
- Support for ndk-build is included due to the large number of legacy projects.
CMake:
An external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build. CMake require a build script to know how to build your native library. For new projects, Android Studio creates a CMake build script, CMakeLists.txt
, and places it in your module’s root directory.
If your native sources don’t already have a CMake build script, you need to create one yourself and include the appropriate CMake commands. A CMake build script is a plain text file that you must name CMakeLists.txt.
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies 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/file_name.cpp )
NDK-Build:
Android Studio also supports ndk-build due to the large number of existing/legacy projects that use the build toolkit to compile their native code. You need to create one yourself and include the appropriate Android.mk file for ndk-build and then need to configure gradle file for ndk-build same as CMake.
Configure Gradle both for CMake and ndk-build:
To manually configure Gradle to link to your native library, you need to add the externalNativeBuild
block to your module-level build.gradle
file and configure it with either the cmake or ndkBuild block:
android {
...
defaultConfig {
...
// This block is different from the one you use to link Gradle
// to your CMake or ndk-build script.
externalNativeBuild {
// For ndk-build, instead use the ndkBuild block.
cmake/ndkBuild {
// Passes optional arguments to CMake.
arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
// Sets optional flags for the C compiler.
cFlags "-fexceptions", "-frtti"
// Sets a flag to enable format macro constants for the C++ compiler.
cppFlags "-D__STDC_FORMAT_MACROS"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
}
}
buildTypes {...}
// Encapsulates your external native build configurations.
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "src/main/cpp/CMakeLists.txt"
}
// Encapsulates your ndkBuild build configurations.
ndkBuild {
// Provides a relative path to your ndkBuild Android.mk file.
path "src/main/cpp/Android.mk"
}
}
}
If you want to link Gradle to an existing ndk-build project, use the ndkBuild
block instead of the cmake block, and provide a relative path to your Android.mk
file.
cmake
is clearly the superior choice here.cmake
is a complete programming language it has for-loops, if-else, functions, variables, list, etc all waiting to be used for your most demanding dependency-related compilation needs. – Phyllode