Android Studio CMake/Ninja未用于构建NDK项目 [英] Android Studio CMake/Ninja Not Used for Building an NDK project
问题描述
我有以下CMAKE&通过Android Studio的SDK工具安装的忍者:
I have the following CMAKE & Ninja installed through Android Studio's SDK Tools:
~/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 :
它不见了:
-DCMAKE_MAKE_PROGRAM=/Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja
错误:
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版本时说3.6.3155560才有效.否则,我必须从brew或macports安装忍者.
Only if I switch to CMake version say 3.6.3155560 it works. Otherwise, I have to install ninja from brew or macports.
这是我的build.gradle中的代码段:
Here is the snippet from my 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"
}
}
如何解决?
推荐答案
从Android Studio SDK管理器安装/更新CMake
Install/Update CMake From Android Studio SDK Manager
如果忍者存在,请从sdk根目录检查您的CMake.
Check your CMake from sdk root directory if ninja exists.
以下内容不好.
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"
}
因为,ANDROID_PLATFORM
应该由Android外部本机构建系统根据minSdkVersion
自动确定,请参见下面来自
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:
应该直接在模块级
build.gradle
文件的defaultConfig
或productFlavors
块中设置minSdkVersion
属性,而不是直接更改此标志.这样可以确保您的库仅由运行适当版本的Android的设备上安装的应用程序使用.然后,CMake工具链会使用以下逻辑为正在构建的ABI选择最佳平台版本:
Instead of changing this flag directly, you should set the
minSdkVersion
property in thedefaultConfig
orproductFlavors
blocks of your module-levelbuild.gradle
file. This makes sure your library is used only by apps installed on devices running an adequate version of Android. The CMake toolchain then chooses the best platform version for the ABI you're building using the following logic:
- 如果ABI的平台版本等于
minSdkVersion
,则CMake将使用该版本. 否则, - 如果ABI的平台版本低于
minSdkVersion
,则CMake将使用这些平台中的最高版本.这是一个合理的选择,因为缺少平台版本通常意味着自从先前的可用版本以来,本机平台API一直没有更改. - 否则,CMake使用比
minSdkVersion
高的下一个可用平台版本.
- If there exists a platform version for the ABI equal to
minSdkVersion
, CMake uses that version. Otherwise, - if there exists platform versions lower than
minSdkVersion
for the ABI, CMake uses the highest of those platform versions. 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. - Otherwise, CMake uses the next available platform version higher than
minSdkVersion
.
而且,-DANDROID_ABI=armeabi-v7a
也不是很好.您不应在此处定义此参数. CMake将根据您的abiFilter自动迭代所有ABI .如果您只想构建armeabi-v7a
,则可以使用abiFilter
进行指定,例如
And, -DANDROID_ABI=armeabi-v7a
is not good as well. You should not define this parameter here. CMake will iterate all your ABIs according to your abiFilters automatically. If you just want to build armeabi-v7a
, you can specify this using abiFilter
, e.g.
externalNativeBuild {
cmake {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
此外,rtti
和exceptions
是cppFlags
,下面应该是设置这两个标志的正确方法.
Also, rtti
and exceptions
are cppFlags
, below should be the proper way to set these two flags.
cppFlags "-std=c++11 -frtti -fexceptions"
请确保已正确配置ANDROID_NDK
路径,因为根据您的问题,您设置了两个版本的NDK,一个是-DANDROID_NDK=/Users/ssk/android-ndk-r17c/
,另一个是-DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
.从local.properties
:
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
. Config NDK path from local.properties
:
ndk.dir=/Users/ssk/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/ssk/Library/Android/sdk
-GAndroid Gradle-Ninja的修复程序是什么?
what is the fix for -GAndroid Gradle - Ninja?
在arguments
以下添加到cmake
配置:
externalNativeBuild {
cmake {
...
version "3.10.2"
arguments "-GAndroid Gradle - Ninja"
}
}
这篇关于Android Studio CMake/Ninja未用于构建NDK项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!