我需要定义一些自定义宏,例如“DEBUG”、“RELEASE”、“DEMO_VER”和“FULL_VER” ”在 Android Studio build.gradle 文件中,以便我的 C/C++ 代码可以检测到它们,如下所示:
#ifdef DEBUG
...
#else //RELEASE
...
#endif
或
#ifdef DEMO_VER
...
#else //FULL_VER
...
#endif
我的理解是,这些宏应该在构建变体 block 中定义为 g++ 编译器选项,如下代码所示:
buildTypes
{
release
{
cmake <<====== Error!!!!!: could not find method cmake() for ...BuildType
{
cppFlags += "-DRELEASE"
}
}
debug
{
cmake <<====== Error!!!!!: could not find method cmake() for ...BuildType
{
cppFlags += "-DDEBUG"
}
}
}
flavorDimensions "version"
productFlavors
{
demo
{
cmake <<====== Error!!!!!: could not find method cmake() for ...ProductFlavor
{
cppFlags += "-DEMO_VER"
}
}
full
{
cmake <<====== Error!!!!!: could not find method cmake() for ...ProductFlavor
{
cppFlags += "-DFULL_VER"
}
}
}
问题是我无法在“BuildType”或“ProductFlavor”中使用“cmake”,该方法不能成立。 那么针对不同的产品风格/构建类型传递编译器宏的正确方法是什么?
最佳答案
我自己找到了答案:“cmake”方法属于“externalNativeBuild”类,因此需要将其嵌入到“externalNativeBuild” block 中,如下所示:
release
{
externalNativeBuild
{
cmake
{
cppFlags += "-DRELEASE"
}
}
...
}
现在,build.gradle中定义的所有预处理器都被传递到 C/C++ 编译器中。
关于Android Studio : How to define custom macros in build. gradle(针对不同的构建变体)并让 native C/C++ 代码检测它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52860467/