Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让本机C/C ++代码检测到它们? [英] Android Studio: How to define custom macros in build.gradle (for different build variants) and let native C/C++ code detect them?

查看:1126
本文介绍了Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让本机C/C ++代码检测到它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一些自定义宏,例如" DEBUG "," RELEASE "," DEMO_VER "和" FULL_VER" "在Android Studio build.gradle 文件中,以便我的C/C ++代码可以检测到它们,例如:

I need to define some custom macros such as "DEBUG", "RELEASE", "DEMO_VER" and "FULL_VER" in Android Studio build.gradle file so that my C/C++ code can detect them like:

#ifdef DEBUG
   ...
#else //RELEASE
   ...
#endif

#ifdef DEMO_VER
   ...
#else //FULL_VER
   ...
#endif

我的理解是,应在构建变体块中将这些宏定义为g ++编译器选项,例如以下代码:

My understanding is that these macros should be defined as g++ compiler options in the build variant blocks like the following code:

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 ",该方法不能成立. 那么,为不同的产品类型/构建类型传递编译器宏的正确方法是什么?

The problem is that I can not use "cmake" inside either "BuildType" nor "ProductFlavor", the method can not be found. So what is the correct way to pass in compiler macros for different product flavors / build types?

推荐答案

我自己找到答案:"cmake"方法属于"externalNativeBuild"类,因此需要将其嵌入"externalNativeBuild"块中,如下所示:

Found answer myself: The "cmake" method belongs to class "externalNativeBuild" class, therefore it needs to be embedded inside "externalNativeBuild" block, like this:

release
{
        externalNativeBuild
        {
            cmake
            {
                cppFlags += "-DRELEASE"
            }
        }
        ...
}

现在,在 build.gradle 中定义的所有预处理器都将传递到C/C ++编译器中.

Now all the preprocessors defined in build.gradle are passed into the C/C++ compiler.

这篇关于Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让本机C/C ++代码检测到它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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