Difference between CMake and NDK-build in android studio project
android studio项目中
Android Native Development Kit(NDK):一个工具集,可让您
在Android上使用C和C ++代码,并提供平台库
允许您管理本地活动并访问物理设备
组件,例如传感器和触摸输入。
CMake:与Gradle一起使用的外部构建工具
您的本机库。 如果您仅计划,则不需要此组件
使用ndk-build。
当我们需要使用什么东西时,有人可以通过示例更好地解释吗?
这里要消除一些混乱:ndk-build是NDK中包含的构建系统。它使用
What is the actual difference between CMake and NDK build in android studio project.
他们使用另一种语言(自定义makefile与cmake)来描述构建。理想情况下,相同描述的构建在输出上没有区别,但这并不意味着没有任何错误。
Can anyone has a better explanation with an example when we need to use what?
通常,使用您喜欢的任何系统。
CMake的主要优点是您可以为所有目标(Android,Linux,Windows,iOS等)使用一组构建文件。如果您的项目是跨平台的,则CMake应该使您最轻松。它在Android开发人员之外也广为人知,因此,刚接触Android的人将有更多的机会了解它。
如果要构建已经使用
如果您正在编写新代码,请使用任何您喜欢的东西。如果您都不熟悉,则cmake可能是更好的选择,因为如果您选择这样做,它将使将来的跨平台工作变得更容易。
我试图给出一些解释,以识别CMake和NDK-Build与setup之间的区别:
一些初步的注意事项:
- Android Studio用于本机库的默认构建工具是CMake。
- 由于存在大量使用构建工具包来编译其本机代码的现有项目,因此Android Studio还支持ndk-build。
- 如果要创建新的本机库,则应使用CMake。
- 由于存在大量旧项目,因此包括对ndk-build的支持。
CMake的:
一个与Gradle一起使用的外部构建工具,以构建您的本机库。如果仅计划使用ndk-build,则不需要此组件。 CMake需要一个构建脚本来知道如何构建您的本机库。对于新项目,Android Studio将创建CMake构建脚本
如果您的本地源还没有CMake构建脚本,则需要自己创建一个并包含适当的CMake命令。 CMake构建脚本是纯文本文件,您必须将其命名为CMakeLists.txt。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # Sets the minimum version of CMake required to build your native library. # This ensures that a certain set of CMake features is available to # your build. cmake_minimum_required(VERSION 3.4.1) # Specifies a library name, specifies whether the library is STATIC or # SHARED, and provides relative paths to the source code. You can # define multiple libraries by adding multiple add.library() commands, # and CMake builds them for you. When you build your app, Gradle # automatically packages shared libraries with your APK. add_library( # Specifies the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/file_name.cpp ) |
NDK版本:
由于存在大量使用构建工具包来编译其本机代码的现有/旧项目,因此Android Studio还支持ndk-build。您需要自己创建一个文件,并为ndk-build包含适当的Android.mk文件,然后需要为ndk-build配置gradle文件(与CMake相同)。
为CMake和ndk-build配置Gradle:
要手动配置Gradle链接到您的本机库,您需要将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | android { ... defaultConfig { ... // This block is different from the one you use to link Gradle // to your CMake or ndk-build script. externalNativeBuild { // For ndk-build, instead use the ndkBuild block. cmake/ndkBuild { // Passes optional arguments to CMake. arguments"-DANDROID_ARM_NEON=TRUE","-DANDROID_TOOLCHAIN=clang" // Sets optional flags for the C compiler. cFlags"-fexceptions","-frtti" // Sets a flag to enable format macro constants for the C++ compiler. cppFlags"-D__STDC_FORMAT_MACROS" } } ndk { // Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a' } } buildTypes {...} // Encapsulates your external native build configurations. externalNativeBuild { // Encapsulates your CMake build configurations. cmake { // Provides a relative path to your CMake build script. path"src/main/cpp/CMakeLists.txt" } // Encapsulates your ndkBuild build configurations. ndkBuild { // Provides a relative path to your ndkBuild Android.mk file. path"src/main/cpp/Android.mk" } } } |
如果要将Gradle链接到现有的ndk-build项目,请使用
明确的答案在这里https://android-developers.blogspot.ru/2016/11/make-and-ndk-build-support-in-android.html。总结-按顺序选择:
-
将Gradle实验性插件用于C ++受限的项目
-
cmake是新项目,旨在提高稳定性
-
ndk-build适用于旧项目,请尝试迁移到cmake或新插件
Android.mk是一个包含NDK-build的文件,如果您使用Cmake build,则您的应用程序不需要Android.mk而是CmakeList.txt