Search code examples
androidc++cmakeandroid-ndkninja

How to build an Android project with Ninja?


I have an Android project with a app/src/CMakeLists.txt file like this

cmake_minimum_required(VERSION 3.4.1)

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).
             main/cpp/native-lib.cpp )

I also have a app/src/main/cpp/native-lib.cpp file in my project.

When I run ninja in a terminal I get this error

ninja: error: loading 'build.ninja': No such file or directory

I'm not familiar with the NDK and I'm trying to run the project with ninja but I'm not able to find any clear documentation or example.


Solution

  • Android NDK uses CMake to build projects, but instead of run ninja separately, you should run below gradle command to trigger your NDK project build.

    ./gradlew externalNativeBuild
    

    Internally, CMake will use the ninja build system to compile and link the C/C++ sources for your apps.