gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:50:49 26 4
gpt4 key购买 nike

我通过 Android Studio 的 SDK 工具安装了以下 CMAKE 和 Ninja:

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

我在尝试构建我的项目时遇到“配置错误”。这是构建输出:

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 安装 ninja。

这是我的 build.gradle 中的 fragment :

 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

如果 ninja 存在,请从 sdk 根目录检查您的 CMake。 enter image description here


下面不好。

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 自动决定,参见下面来自how ANDROID_PLATFORM works 的官方文档:

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. 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 会根据你的 abiFilters 自动迭代所有的 ABIs。如果您只想构建 armeabi-v7a,您可以使用 abiFilter 指定它,例如

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

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

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 配置 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?

将以下参数添加到cmake配置:

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

关于Android Studio CMake/Ninja 未用于构建 NDK 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54470140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com