Android Studio CMake/Ninja未用于构建NDK项目 [英] Android Studio CMake/Ninja Not Used for Building an NDK project

查看:526
本文介绍了Android Studio CMake/Ninja未用于构建NDK项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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文件的defaultConfigproductFlavors块中设置minSdkVersion属性,而不是直接更改此标志.这样可以确保您的库仅由运行适当版本的Android的设备上安装的应用程序使用.然后,CMake工具链会使用以下逻辑为正在构建的ABI选择最佳平台版本:

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. 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:

  1. 如果ABI的平台版本等于minSdkVersion,则CMake将使用该版本. 否则,
  2. 如果ABI的平台版本低于minSdkVersion,则CMake将使用这些平台中的最高版本.这是一个合理的选择,因为缺少平台版本通常意味着自从先前的可用版本以来,本机平台API一直没有更改.
  3. 否则,CMake使用比minSdkVersion高的下一个可用平台版本.
  1. If there exists a platform version for the ABI equal to minSdkVersion, CMake uses that version. Otherwise,
  2. 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.
  3. 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'
    }
}

此外,rttiexceptionscppFlags,下面应该是设置这两个标志的正确方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆