Android Studio CMake 报错 "CMake Error: CMake was unable to find a build program corresponding Ninja"

前两天, 没修改什么, 打开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 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\u003darmeabi-v7a",
    "-DANDROID_PLATFORM\u003dandroid-16",
    "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY\u003d/Users/oncealong/Android-Project/build/intermediates/cmake/debug/obj/armeabi-v7a",
    "-DCMAKE_BUILD_TYPE\u003dDebug",
    "-DANDROID_NDK\u003d/Users/along/Library/Android/android-ndk-r18b",
    "-DCMAKE_CXX_FLAGS\u003d-frtti -fexceptions -D__cdecl\u003d -D__stdcall\u003d",
    "-DCMAKE_SYSTEM_NAME\u003dAndroid",
    "-DCMAKE_ANDROID_ARCH_ABI\u003darmeabi-v7a",
    "-DCMAKE_SYSTEM_VERSION\u003d16",
    "-DCMAKE_EXPORT_COMPILE_COMMANDS\u003dON",
    "-DCMAKE_ANDROID_NDK\u003d/Users/along/Library/Android/android-ndk-r18b",
    "-DCMAKE_TOOLCHAIN_FILE\u003d/Users/along/Library/Android/android-ndk-r18b/build/cmake/android.toolchain.cmake",
    "-G Ninja",
    "-DANDROID_TOOLCHAIN\u003dclang",
    "-DANDROID_STL\u003dc++_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.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券