Android Studio CMake reports “CMake Error: CMake was unable to find a build program corresponding Ninja”

Two days ago, I didn’t modify anything, I opened Android Studio to compile so, and suddenly it failed.

The error message is as follows:

Support for CMake 3.7 and higher is a preview feature. To report a bug, see https://developer.android.com/studio/report-bugs.html
Support for CMake 3.7 and higher is a preview feature. To report a bug, see https://developer.android.com/studio/report-bugs.html
External native generate JSON debug: building json with force flag false
External native generate JSON debug: starting JSON generation
External native generate JSON debug: using platform version 16 for ABI ARMEABI_V7A and min SDK version 14
External native generate JSON debug: rebuilding JSON /Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a/android_gradle_build.json due to:
External native generate JSON debug: - expected json /Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a/android_gradle_build.json file is not present, will remove stale json folder
External native generate JSON debug: - missing previous command file /Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a/cmake_build_command.txt, will remove stale json folder
External native generate JSON debug: removing stale contents from '/Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a'
External native generate JSON debug: created folder '/Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a'
External native generate JSON debug: executing cmake Executable : /Users/along/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake
arguments:
-H/Users/oncealong/Android-Project/
-B/Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a
-DANDROID_ABI=armeabi-v7a
-DANDROID_PLATFORM=android-16
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/oncealong/Android-Project/build/intermediates/cmake/debug/obj/armeabi-v7a
-DCMAKE_BUILD_TYPE=Debug
-DANDROID_NDK=/Users/along/Library/Android/android-ndk-r18b
-DCMAKE_CXX_FLAGS=-frtti -fexceptions -D__cdecl= -D__stdcall=
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a
-DCMAKE_SYSTEM_VERSION=16
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_ANDROID_NDK=/Users/along/Library/Android/android-ndk-r18b
-DCMAKE_TOOLCHAIN_FILE=/Users/along/Library/Android/android-ndk-r18b/build/cmake/android.toolchain.cmake
-G Ninja
-DANDROID_TOOLCHAIN=clang
-DANDROID_STL=c++_static
jvmArgs:


[== "CMake Server" ==[

{"supportedProtocolVersions":[{"isExperimental":true,"major":1,"minor":1}],"type":"hello"}

]== "CMake Server" ==]

[== "CMake Server" ==[

{
  "type": "handshake",
  "cookie": "gradle-cmake-cookie",
  "protocolVersion": {
    "isExperimental": true,
    "major": 1,
    "minor": 1
  },
  "sourceDirectory": "/Users/oncealong/Android-Project/",
  "buildDirectory": "/Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a",
  "generator": "Ninja"
}

]== "CMake Server" ==]

[== "CMake Server" ==[

{"cookie":"gradle-cmake-cookie","inReplyTo":"handshake","type":"reply"}

]== "CMake Server" ==]

[== "CMake Server" ==[

{
  "type": "configure",
  "cacheArguments": [
    "",
    "-DANDROID_ABI\\=armeabi-v7a",
    "-DANDROID_PLATFORM\\=android-16",
    "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY\\=/Users/oncealong/Android-Project/build/intermediates/cmake/debug/obj/armeabi-v7a",
    "-DCMAKE_BUILD_TYPE\\=Debug",
    "-DANDROID_NDK\\=/Users/along/Library/Android/android-ndk-r18b",
    "-DCMAKE_CXX_FLAGS\\=-frtti -fexceptions -D__cdecl\\= -D__stdcall\\=",
    "-DCMAKE_SYSTEM_NAME\\=Android",
    "-DCMAKE_ANDROID_ARCH_ABI\\=armeabi-v7a",
    "-DCMAKE_SYSTEM_VERSION\\=16",
    "-DCMAKE_EXPORT_COMPILE_COMMANDS\\=ON",
    "-DCMAKE_ANDROID_NDK\\=/Users/along/Library/Android/android-ndk-r18b",
    "-DCMAKE_TOOLCHAIN_FILE\\=/Users/along/Library/Android/android-ndk-r18b/build/cmake/android.toolchain.cmake",
    "-G Ninja",
    "-DANDROID_TOOLCHAIN\\=clang",
    "-DANDROID_STL\\=c++ _static"
  ]
}

]== "CMake Server" ==]

[== "CMake Server" ==[

{"cookie":"","inReplyTo":"configure","message":"CMake Error: CMake was unable to find a build program corresponding to "Ninja ". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.","title":"Error","type":"message"}

]== "CMake Server" ==]

CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.


[== "CMake Server" ==[

{"cookie":"","inReplyTo":"configure","progressCurrent":1000,"progressMaximum":1000,"progressMessage":"Configuring\ ","progressMinimum":0,"type":"progress"}

]== "CMake Server" ==]

[== "CMake Server" ==[

{"cookie":"","inReplyTo":"configure","message":"Configuring incomplete, errors occurred!\\
See also "/Users/oncealong /Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".","type":"message"}

]== "CMake Server" ==]

Configuring incomplete, errors occurred!
See also "/Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".
Configuring incomplete, errors occurred!
See also "/Users/oncealong/Android-Project/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".


[== "CMake Server" ==[

{"cookie":"","errorMessage":"Configuration failed.","inReplyTo":"configure","type":"error"}

]== "CMake Server" ==]

Configuration failed.
External native generate JSON debug: JSON generation completed with problems

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':armplayer-lib'.
> executing external native build for cmake /Users/oncealong/Android-Project/CMakeLists.txt

*Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

After looking for the information, there is only how to fix this problem, but not why. Finally, I found the official website:

https://developer.android.com/studio/projects/add-native-code#vanilla_cmake

The official website indicates that CMake 3.6 and 3.10 are now supported, but gradle chooses 3.6 by default.
If it is configured as 3.10, I failed to configure it according to the 3.10 configuration on the official website, but successfully configured it according to “Use a custom CMake version” on the official website.
This should be caused by a mismatch between the lower version of the gradle plugin and the default settings of CMake 3.10, which requires additional manual intervention.

It is recommended to solve it directly according to “Use a custom CMake version” on the official website.

Use CMake 3.6.0 or 3.10.2

The SDK Manager includes the 3.6.0 forked version of CMake and version 3.10.2. Projects that don’t set a specific CMake version in build.gradle are built with CMake 3.6.0. To use the later included version, specify CMake version 3.10.2 in your module’s build.gradle file:

android {
    ...
    externalNativeBuild {
        cmake {
            ...
            version "3.10.2"
        }
    }
}

While not a best practice, you may specify the CMake version as the minimum version by adding a + to the end of the build.gradle entry, such as 3.10 + or 3 + .

Use a custom CMake version

If you want to use a CMake version 3.7 or higher that is not included by the SDK Manager, follow these steps:

  1. Download and install CMake 3.7 or higher from the official CMake website.
  2. Specify the CMake version you want Gradle to use in your module’s build.gradle file:
    android {
        ...
        externalNativeBuild {
            cmake {
                ...
                version "cmake-version"
            }
        }
    }
    
  3. Either add the path to the CMake installation to your PATH environment variable or include it in your project’s local.properties file, as shown below. If Gradle is unable to find the version of CMake you specified in your build.gradle file, you get a build error.
    # If you set this property, Gradle no longer uses PATH to find CMake.
    cmake.dir="path-to-cmake"
    
  4. If you don’t already have the Ninja build system installed on your workstation, go to the official Ninja website, and download and install the latest version of Ninja available for your OS. Make sure to also add the path to the Ninja installation to your PATH environment variable.