'how can I add cppFlag in gradle for specific build type

I have the following buildType block in my build.gradle. As you can see I would like to add the DEBUG_BUILD flag to the "debug" build type ONLY!

    buildTypes {
        release {
            sourceSets
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            externalNativeBuild {
                cmake {
                    cppFlags += "-DDEBUG_BUILD"
                }
            }
        }
    }

unfortunately, no matter what build type variant I choose in android studio both blocks are executed. I have seen other posts here about this issue, but non of them worked for me.

I also have this task that supposed to check the build type and it is invoked twice for debug and release

task checkbuildtype {
                android.libraryVariants.all { variant ->
                    if (variant.buildType.name == "release") {
                        variant.assemble.doLast { // Can also use doFirst here to run at the start.
                            logger.lifecycle("we have successfully built $v.name and can post a messaage to remote server")
                        }
                    }
                }
}

how can I ensure the cppFlag gets added only for a specific build type. TIA



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source