简体   繁体   English

Android Studio CMake / Ninja不用于构建NDK项目

[英]Android Studio CMake/Ninja Not Used for Building an NDK project

I have the following CMAKE & Ninja installed through Android Studio's SDK Tools: 我通过Android Studio的SDK工具安装了以下CMAKE和Ninja:

~/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja --version
1.8.2

I run into "Error Configuring" while trying to build my project. 我在尝试构建项目时遇到“错误配置”。 Here is the build output: 这是构建输出:

Executable : /Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake
arguments : 
-H/Users/ssk/MyProject
-B/Users/ssk/MyProject/.externalNativeBuild/cmake/debug/armeabi-v7a
-DANDROID_ABI=armeabi-v7a
-DANDROID_PLATFORM=android-16
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/ssk/MyProject/build/intermediates/cmake/debug/obj/armeabi-v7a
-DCMAKE_BUILD_TYPE=Debug
-DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
-DCMAKE_CXX_FLAGS=-std=c++11
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a
-DCMAKE_SYSTEM_VERSION=16
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_ANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
-DCMAKE_TOOLCHAIN_FILE=/Users/ssk/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake
-G Ninja
-DANDROID_STL=gnustl_statics
-DANDROID_CPP_FEATURES=rtti exception
-DANDROID_TOOLCHAIN=gcc
-DANDROID_NDK=/Users/ssk/android-ndk-r17c/
jvmArgs : 

It's missing: 它缺失了:

-DCMAKE_MAKE_PROGRAM=/Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja

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

Only if I switch to CMake version say 3.6.3155560 it works. 只有当我切换到CMake版本说3.6.3155560它才有效。 Otherwise, I have to install ninja from brew or macports. 否则,我必须从brew或macports安装忍者。

Here is the snippet from my build.gradle: 这是我的build.gradle的片段:

 externalNativeBuild {
        cmake {
            // Linker flags and Visibility options keeps the size of the library small
            cppFlags "-std=c++11"
            arguments "-DANDROID_STL=gnustl_static",
                      "-DANDROID_CPP_FEATURES=rtti exceptions",
                      "-DANDROID_TOOLCHAIN=gcc"
        }
    }

How to fix it? 怎么解决?

Install/Update CMake From Android Studio SDK Manager 从Android Studio SDK Manager安装/更新CMake

从Android Studio SDK Manager安装/更新CMake

Check your CMake from sdk root directory if ninja exists. 如果ninja存在,请从sdk根目录检查您的CMake。 在此输入图像描述


Below is not good. 下面不好。

cmake {
    cppFlags "-std=c++11"
    arguments "-DANDROID_ABI=armeabi-v7a",
                "-DANDROID_PLATFORM=android-16",
                "-DANDROID_STL=gnustl_static",
                "-DANDROID_CPP_FEATURES=rtti exceptions",
                "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=libs"
}

Because, ANDROID_PLATFORM should be automatically decided by Android external native build system according to minSdkVersion , see below official document from how ANDROID_PLATFORM works : 因为,根据minSdkVersion ,Android外部本机构建系统应自动决定ANDROID_PLATFORM ,请参阅以下官方文档, 了解ANDROID_PLATFORM工作原理

Instead of changing this flag directly, you should set the minSdkVersion property in the defaultConfig or productFlavors blocks of your module-level build.gradle file. 您应该在模块级build.gradle文件的defaultConfigproductFlavors块中设置minSdkVersion属性,而不是直接更改此标志。 This makes sure your library is used only by apps installed on devices running an adequate version of Android. 这样可确保您的库仅供运行适当版本Android的设备上安装的应用使用。 The CMake toolchain then chooses the best platform version for the ABI you're building using the following logic: 然后,CMake工具链使用以下逻辑为您正在构建的ABI选择最佳平台版本:

  1. If there exists a platform version for the ABI equal to minSdkVersion , CMake uses that version. 如果ABI的平台版本等于minSdkVersion ,则CMake使用该版本。 Otherwise, 除此以外,
  2. if there exists platform versions lower than minSdkVersion for the ABI, CMake uses the highest of those platform versions. 如果存在比ABI的minSdkVersion低的平台版本,则CMake使用这些平台版本中最高的版本。 This is a reasonable choice because a missing platform version typically means that there were no changes to the native platform APIs since the previous available version. 这是一个合理的选择,因为缺少平台版本通常意味着自上一个可用版本以来没有对本机平台API进行任何更改。
  3. Otherwise, CMake uses the next available platform version higher than minSdkVersion . 否则,CMake使用高于minSdkVersion的下一个可用平台版本。

And, -DANDROID_ABI=armeabi-v7a is not good as well. 并且, -DANDROID_ABI=armeabi-v7a也不好。 You should not define this parameter here. 您不应在此处定义此参数。 CMake will iterate all your ABIs according to your abiFilters automatically. CMake将根据您的abiFilters自动迭代您的所有ABI If you just want to build armeabi-v7a , you can specify this using abiFilter , eg 如果您只想构建armeabi-v7a ,可以使用abiFilter指定它,例如

externalNativeBuild {
    cmake {
        abiFilters 'armeabi-v7a', 'arm64-v8a'
    }
}

Also, rtti and exceptions are cppFlags , below should be the proper way to set these two flags. 另外, rttiexceptionscppFlags ,下面应该是设置这两个标志的正确方法。

cppFlags "-std=c++11 -frtti -fexceptions"

Ensure that your have properly configured ANDROID_NDK path, because according to your question, you have TWO version of NDK set, one is -DANDROID_NDK=/Users/ssk/android-ndk-r17c/ , the other one is -DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle . 确保您已正确配置ANDROID_NDK路径,因为根据您的问题,您有两个版本的NDK设置,一个是-DANDROID_NDK=/Users/ssk/android-ndk-r17c/ ,另一个是-DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle Config NDK path from local.properties : local.properties配置NDK路径:

ndk.dir=/Users/ssk/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/ssk/Library/Android/sdk

what is the fix for -GAndroid Gradle - Ninja? 什么是-GAndroid Gradle - Ninja的修复?

Add below arguments to the cmake config: 将以下arguments添加到cmake配置:

externalNativeBuild { 
    cmake { 
        ...
        version "3.10.2"
        arguments "-GAndroid Gradle - Ninja"
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM