"CMake Error: CMake was unable to find a build program corresponding Ninja"

  • 2019 年 10 月 7 日
  • 筆記

前兩天, 沒修改什麼, 打開Android Studio編so, 忽然就不成功了.

報錯資訊如下:

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 folde    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 folde    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_ABIu003darmeabi-v7a",        "-DANDROID_PLATFORMu003dandroid-16",        "-DCMAKE_LIBRARY_OUTPUT_DIRECTORYu003d/Users/oncealong/Android-Project/build/intermediates/cmake/debug/obj/armeabi-v7a",        "-DCMAKE_BUILD_TYPEu003dDebug",        "-DANDROID_NDKu003d/Users/along/Library/Android/android-ndk-r18b",        "-DCMAKE_CXX_FLAGSu003d-frtti -fexceptions -D__cdeclu003d -D__stdcallu003d",        "-DCMAKE_SYSTEM_NAMEu003dAndroid",        "-DCMAKE_ANDROID_ARCH_ABIu003darmeabi-v7a",        "-DCMAKE_SYSTEM_VERSIONu003d16",        "-DCMAKE_EXPORT_COMPILE_COMMANDSu003dON",        "-DCMAKE_ANDROID_NDKu003d/Users/along/Library/Android/android-ndk-r18b",        "-DCMAKE_TOOLCHAIN_FILEu003d/Users/along/Library/Android/android-ndk-r18b/build/cmake/android.toolchain.cmake",        "-G Ninja",        "-DANDROID_TOOLCHAINu003dclang",        "-DANDROID_STLu003dc++_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!nSee 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.

找了下資料, 只有如何修復這個問題, 沒有為什麼這麼修復. 最終找到了官網:

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

官網表示現在支援CMake 3.6和3.10, 但是gradle默認選擇3.6.

如果配置成3.10, 我按照官網的3.10配置沒成功, 按照官網的"Use a custom CMake version"配置成功.

這裡應該是gradle plugin低版本和CMake 3.10默認設置不匹配導致, 需要額外手動介入解決.

推薦的是直接按照官網的"Use a custom CMake version"來解決.

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.