本文介绍了从Flavor和BuildType添加cppflags,然后在我的CmakeList中访问它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这招:

android {
    ...
    buildTypes {
        debug {
            externalNativeBuild {
                cmake {
                    cppFlags "-DGENDEV"
                }
            }
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
    ...
}

如果条件在我的CMakeList文件中正常运行,则此操作:

This if condition works fine in my CMakeList file:

if("${CMAKE_CXX_FLAGS}" MATCHES "GENDEV$")
    // true
endif()

我最近在我的构建中添加了风味 :

I recently added flavors in my build like so:

flavorDimensions "version"
productFlavors {
    free {
        dimension "version"
        externalNativeBuild.cmake {
            cppFlags "-DFLAVOR_FREE"
        }
    }
    full {
        dimension "version"
        externalNativeBuild.cmake {
            cppFlags "-DFLAVOR_FULL"
        }
    }
}

现在,我在CMakeList文件中进行了以下检查:

Now, I have these checks in my CMakeList file:

if("${CMAKE_CXX_FLAGS}" MATCHES "FLAVOR_FULL$")
    // full version
else()
    // free version
endif()

if("${CMAKE_CXX_FLAGS}" MATCHES "GENDEV$")
    // true
endif()

第一次检查始终为假,因此每个版本都是免费版本!我在做什么错了?

The first check is always false, thus every build is a FREE version! What am I doing wrong?

推荐答案

您的代码还可以,但是Gradle插件太旧了,无法正确处理.

Your code is alright, but the Gradle plugin was too old to handle it correctly.

对我来说,相同的代码适用于

For me, same code worked with

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
    }
}

此插件需要Gradle 5.5.

This plugin requires Gradle 5.5.

请注意,更简单的CMake集成方法可以是指定参数:

Note that an easier approach to CMake integration could be to specify arguments:

flavorDimensions "version"
productFlavors {
    free {
        dimension "version"
        externalNativeBuild.cmake {
            arguments "-DFLAVOR=FREE"
        }
    }
    full {
        dimension "version"
        externalNativeBuild.cmake {
            arguments "-DFLAVOR=FULL"
        }
    }
}

这样,您的CMakeLists.txt将更具可读性:

This way, your CMakeLists.txt will be more readable:

if(${FLAVOR} STREQUAL "FULL")
    // full version
else()
    // free version
endif()

或者,您可以选择使用arguments "-DFLAVOR_FULL"if(FLAVOR_FULL)

Or, you can choose to use arguments "-DFLAVOR_FULL" and if(FLAVOR_FULL)

这篇关于从Flavor和BuildType添加cppflags,然后在我的CmakeList中访问它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 19:54