简体   繁体   English

使用Gradle和CMake将发布和调试子目录添加到Android版本

[英]Adding release and debug subdirectories to Android build with Gradle and CMake

I'm trying to migrate an Android NDK project from Ant/ndk-build to Gradle/CMake. 我正在尝试将Android NDK项目从Ant / ndk-build迁移到Gradle / CMake。 I want to build it from the command line on Windows. 我想从Windows上的命令行构建它。 I don't want to use Android Studio. 我不想使用Android Studio。 It's pretty much up and running now but there's one thing left to solve and that is the following: 它现在已经启动并运行了,但还有一件事需要解决,其中包括:

In my CMakeFiles.txt I'm importing several other directories that contain static linker libraries into my project like this: 在我的CMakeFiles.txt我将几个包含静态链接库的目录导入到我的项目中,如下所示:

add_subdirectory("../foobar/src" "../foobar/obj/${ANDROID_ABI}")

Now the problem is with specifying the directory for the binary files. 现在的问题是指定二进制文件的目录。 I'm already using ${ANDROID_ABI} to make sure the object and library files are separated by architecture. 我已经使用${ANDROID_ABI}来确保对象和库文件由体系结构分隔。 However, this isn't enough because Gradle will also do debug and release builds and with the line above, debug and release object and library files will be written to exactly the same directory and this will make Gradle rebuild each static library every time I say ./gradlew build . 但是,这还不够,因为Gradle也会进行调试和发布构建,并且使用上面的行,调试和发布对象和库文件将被写入完全相同的目录,这将使Gradle每次重建每个静态库./gradlew build

So how can I solve this problem? 那么我该如何解决这个问题呢? Is there a variable that I can use in my CMakeLists.txt that tells me if Gradle/CMake is currently building for debug or for release? 有没有我可以在我的CMakeLists.txt中使用的变量,它告诉我Gradle / CMake目前是否正在构建用于调试或发布? I've tried the following already, but it didn't work: 我已经尝试了以下,但它不起作用:

IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
    set(BUILDMODE "Debug")
ELSE()
    set(BUILDMODE "Release")
ENDIF()

CMAKE_BUILD_TYPE always seems to be != DEBUG when building with Gradle and the Android plugin so BUILDMODE will always be Release . 使用Gradle和Android插件构建时, CMAKE_BUILD_TYPE似乎总是!= DEBUG,因此BUILDMODE将永远是Release

I've been thinking about this for several hours now and I'm pretty much out of ideas... unfortunately, it also doesn't seem possible to leave out the binary files argument for CMake's add_subdirectory . 我已经考虑了好几个小时了,而且我几乎没有想法......不幸的是,似乎也没有可能省略CMake的add_subdirectory的二进制文件参数。

I really don't know what to do here so any help greatly appreciated. 我真的不知道该怎么做,所以任何帮助都非常感激。

According to @Andreas 's comment, here is how to use CMAKE_CURRENT_BINARY_DIR to detect Debug or Release variant in an Android NDK CMake build: 根据@Andreas的评论,这里是如何使用CMAKE_CURRENT_BINARY_DIR来检测Android NDK CMake构建中的DebugRelease变体:

if (CMAKE_CURRENT_BINARY_DIR MATCHES "debug")
    message(STATUS "Debug build detected")
    add_compile_options("-DANDROID_DEBUG") # <-- change this to have an effect on the build
else()
    message(STATUS "Release build detected")
endif()

You may want to set it if not explicitly set upstream. 如果没有明确设置上游,您可能想要设置它。

if (NOT CMAKE_BUILD_TYPE)
 SET(CMAKE_BUILD_TYPE Debug)
endif()

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

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