• Configure CMake
    • Create a CMake build script
    • Add NDK APIs
    • Add other prebuilt libraries
    • Include other CMake projects

    Configure CMake

    A CMake build script is a plain text file that you must name CMakeLists.txt and includes commands CMake uses to build your C/C++ libraries. If your native sources don’t already have a CMake build script, you need to create one yourself and include the appropriate CMake commands.

    This section covers some basic commands you should include in your build script in order to tell CMake which sources to use when creating your native library. To learn more, read the official documentation about CMake commands.

    After you configure a new CMake build script, you need toconfigure Gradleto include your CMake project as a build dependency, so that Gradle builds andpackages your native library with your app's APK.

    Note: If your project uses ndk-build, you don’t need to create a CMake build script. You can simply configure Gradle to include your existing native library project by providing a path to your Android.mk file.

    Create a CMake build script

    To create a plain text file that you can use as your CMake build script, proceed as follows:

    • Open the Project pane from the left side of the IDE and select the Project view from the drop-down menu.
    • Right-click on the root directory of your-module and select New > File.
      Note: You can create the build script in any location you want. However, when configuring the build script, paths to your native source files and libraries are relative to the location of the build script.

    • Enter "CMakeLists.txt" as the filename and click OK.
      You can now configure your build script by adding CMake commands. To instruct CMake to create a native library from native source code, add the cmake_minimum_required() and add_library() commands to your build script:

    1. # Sets the minimum version of CMake required to build your native library.
    2. # This ensures that a certain set of CMake features is available to
    3. # your build.
    4.  
    5. cmake_minimum_required(VERSION 3.4.1)
    6.  
    7. # Specifies a library name, specifies whether the library is STATIC or
    8. # SHARED, and provides relative paths to the source code. You can
    9. # define multiple libraries by adding multiple add_library() commands,
    10. # and CMake builds them for you. When you build your app, Gradle
    11. # automatically packages shared libraries with your APK.
    12.  
    13. add_library( # Specifies the name of the library.
    14. native-lib
    15.  
    16. # Sets the library as a shared library.
    17. SHARED
    18.  
    19. # Provides a relative path to your source file(s).
    20. src/main/cpp/native-lib.cpp )

    Tip: Similar to how you can tell CMake to create a native library from source files, you can use the add_executable() command to tell CMake to instead create an executable from those source files. However, building executables from your native sources is optional, and building native libraries to package into your APK satisfies most project requirements.

    When you add a source file or library to your CMake build script using add_library(), Android Studio also shows associated header files in the Project view after you sync your project. However, in order for CMake to locate your header files during compile time, you need to add the include_directories() command to your CMake build script and specify the path to your headers:

    1. add_library(...)
    2.  
    3. # Specifies a path to native header files.
    4. include_directories(src/main/cpp/include/)

    The convention CMake uses to name the file of your library is as follows:

    liblibrary-name.so

    For example, if you specify "native-lib" as the name of your shared library in the build script, CMake creates a file named libnative-lib.so. However, when loading this library in your Java or Kotlin code, use the name you specified in the CMake build script:

    1. static {
    2. System.loadLibrary("native-lib");
    3. }

    Note: If you rename or remove a library in your CMake build script, you need to clean your project before Gradle applies the changes or removes the older version of the library from your APK. To clean your project, select Build > Clean Project from the menu bar.

    Android Studio automatically adds the source files and headers to the cpp group in the Project pane. By using multiple add_library() commands, you can define additional libraries for CMake to build from other source files.

    Add NDK APIs

    The Android NDK provides a set of native APIs and libraries that you may find useful. You can use any of these APIs by including the NDK libraries in your project’s CMakeLists.txt script file.

    Prebuilt NDK libraries already exist on the Android platform, so you don’t need to build them or package them into your APK. Because the NDK libraries are already a part of CMake’s search path, you don’t even need to specify the location of the library in your local NDK installation—you only need to provide CMake with the name of the library you want to use and link it against your own native library.

    Add the find_library() command to your CMake build script to locate an NDK library and store its path as a variable. You use this variable to refer to the NDK library in other parts of the build script. The following sample locates the Android-specific log support library and stores its path in log-lib:

    1. find_library( # Defines the name of the path variable that stores the
    2. # location of the NDK library.
    3. log-lib
    4.  
    5. # Specifies the name of the NDK library that
    6. # CMake needs to locate.
    7. log )

    In order for your native library to call functions in the log library, you need to link the libraries using the target_link_libraries() command in your CMake build script:

    1. find_library(...)
    2.  
    3. # Links your native library against one or more other native libraries.
    4. target_link_libraries( # Specifies the target library.
    5. native-lib
    6.  
    7. # Links the log library to the target library.
    8. ${log-lib} )

    The NDK also includes some libraries as source code that you need to build and link to your native library. You can compile the source code into a native library by using the add_library() command in your CMake build script. To provide a path to your local NDK library, you can use the ANDROID_NDK path variable, which Android Studio automatically defines for you.

    The following command tells CMake to build android_native_app_glue.c, which manages NativeActivity lifecycle events and touch input, into a static library and links it to native-lib:

    1. add_library( app-glue
    2. STATIC
    3. ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
    4.  
    5. # You need to link static libraries against your shared native library.
    6. target_link_libraries( native-lib app-glue ${log-lib} )

    Add other prebuilt libraries

    Adding a prebuilt library is similar to specifying another native library for CMake to build. However, because the library is already built, you need to use the IMPORTED flag to tell CMake that you only want to import the library into your project:

    1. add_library( imported-lib
    2. SHARED
    3. IMPORTED )

    You then need to specify the path to the library using the set_target_properties() command as shown below.

    Some libraries provide separate packages for specific CPU architectures, or Application Binary Interfaces (ABI), and organize them into separate directories. This approach helps libraries take advantage of certain CPU architectures while allowing you to use only the versions of the library you want. To add multiple ABI versions of a library to your CMake build script, without having to write multiple commands for each version of the library, you can use the ANDROID_ABI path variable. This variable uses a list of the default ABIs that the NDK supports, or a filtered list of ABIs you manually configure Gradle to use. For example:

    1. add_library(...)
    2. set_target_properties( # Specifies the target library.
    3. imported-lib
    4.  
    5. # Specifies the parameter you want to define.
    6. PROPERTIES IMPORTED_LOCATION
    7.  
    8. # Provides the path to the library you want to import.
    9. imported-lib/src/${ANDROID_ABI}/libimported-lib.so )

    For CMake to locate your header files during compile time, you need to use the include_directories() command and include the path to your header files:

    1. include_directories( imported-lib/include/ )

    Note: If you want to package a prebuilt library that is not a build-time dependency—for example, when adding a prebuilt library that is a dependency of imported-lib, you do not need perform the following instructions to link the library.

    To link the prebuilt library to your own native library, add it to the target_link_libraries() command in your CMake build script:

    1. target_link_libraries( native-lib imported-lib app-glue ${log-lib} )

    To package the prebuilt library into your APK, you need to manually configure Gradle with the sourceSets block to include the path to your .so file. After building your APK, you can verify which libraries Gradle packages into your APK by using the APK Analyzer.

    Include other CMake projects

    If you want to build multiple CMake projects and include their outputs in your Android project, you can use one CMakeLists.txt file as the top-level CMake build script (which is the one you link to Gradle) and add additional CMake projects as dependencies of that build script. The following top-level CMake build script uses the add_subdirectory() command to specify another CMakeLists.txt file as a build dependency and then links against its output just as it would with any other prebuilt library.

    1. # Sets lib_src_DIR to the path of the target CMake project.
    2. set( lib_src_DIR ../gmath )
    3.  
    4. # Sets lib_build_DIR to the path of the desired output directory.
    5. set( lib_build_DIR ../gmath/outputs )
    6. file(MAKE_DIRECTORY ${lib_build_DIR})
    7.  
    8. # Adds the CMakeLists.txt file located in the specified directory
    9. # as a build dependency.
    10. add_subdirectory( # Specifies the directory of the CMakeLists.txt file.
    11. ${lib_src_DIR}
    12.  
    13. # Specifies the directory for the build outputs.
    14. ${lib_build_DIR} )
    15.  
    16. # Adds the output of the additional CMake build as a prebuilt static
    17. # library and names it lib_gmath.
    18. add_library( lib_gmath STATIC IMPORTED )
    19. set_target_properties( lib_gmath PROPERTIES IMPORTED_LOCATION
    20. ${lib_build_DIR}/${ANDROID_ABI}/lib_gmath.a )
    21. include_directories( ${lib_src_DIR}/include )
    22.  
    23. # Links the top-level CMake build output against lib_gmath.
    24. target_link_libraries( native-lib ... lib_gmath )