Set Android.mk flag through Gradle depending on build type

Viewed 2505

I need to set a flag inside Android.mk file which tells to the C code whether it's debug mode or not.

LOCAL_MODULE := auth
LOCAL_SRC_FILES := auth.c
LOCAL_CFLAGS := -DDEBUG_MODE=0
LOCAL_EXPORT_CFLAGS := $(LOCAL_CFLAGS)
LOCAL_LDLIBS := -llog
LOCAL_C_INCLUDES := auth.h

include $(BUILD_SHARED_LIBRARY)

The problem is that I often forget to change the value when building a release and viceversa so I'm looking forward to a way that does it automatically.

I tried changing Android.mk to:

LOCAL_CFLAGS := -DDEBUG_MODE

And changed the build.gradle to the following:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                cFlags = " -DDEBUG_MODE=0 "
            }
        }
        debug {
            debuggable true
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                cFlags = " -DDEBUG_MODE=1 "
            }
        }
    }

    sourceSets.main {
        jni.srcDirs = ['src/main/none']
        jniLibs.srcDirs = ["src/main/libs"]
    }

    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
    return void
}

This was not enough, the flag is not changing depending on build type. I found few information about this and the above code is the result of what I read on the internet. Do you have any suggestion about how to get it to work properly?

2 Answers
Related