title: How to define macros for native code inside a build type in gradle?
body: <p>I've built a library with native code in Android Studio. It has some time dependent parts (in C) that must be under certain levels of latency, and I need to measure them. So I've implemented some measurement routines. However, I don't want the measurement routines to work in a release compilation. </p>
  
  <p>So I have defined macros that will allow me to compile or not code based on the build type:</p>
  
  <p>In my native code:</p>
  
  <pre><code>#ifdef MEASURE
  measureRoutine1();
  if (MEASURE == 1) {
      measureRoutine2();
  }
  #endif
  </code></pre>
  
  <p>In my gradle file:</p>
  
  <pre><code>buildTypes {
          release {
              signingConfig signingConfigs.storeSignature
              debuggable false
              jniDebuggable false
              versionNameSuffix "2.3"
              shrinkResources true
              minifyEnabled true
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
          debug {
              jniDebuggable true
              minifyEnabled false
              versionNameSuffix '2.3.1'
          }
          measure1 {
              initWith debug
              externalNativeBuild {
                  cmake {
                      cppFlags += "-DMEASURE=0"
                      cFlags += "-DMEASURE=0"
                  }
              }
          }
          measure2 {
              initWith debug
              externalNativeBuild {
                  cmake {
                      cppFlags += "-DMEASURE=1"
                      cFlags += "-DMEASURE=1"
                  }
              }
          }
      }
  </code></pre>
  
  <p>Now, my problem is that it doesn't matter what buildType I compile, the MEASURE flag is not defined in my native code, so the measuring code never gets compiled. </p>
  
  <p><strong>What am I doing wrong? How can I achieve what I need?</strong></p>
  
  <p><em>PS:
  The only way I get the MEASURE macro to be defined in native is if I put the cFlags, outside the buildTypes in the defaultConfig space.
  This following code works, but I need to hardcode the MEASURE macro every time I want to compile with measurements.</em></p>
  
  <pre><code>defaultConfig {
          minSdkVersion 21
          targetSdkVersion 25
          versionCode 6
          versionName "2.3"
  
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
          externalNativeBuild {
              cmake {
                  cppFlags "-std=c++11 -fexceptions -O2 -ffast-math -DMEASURE=0"
                  cFlags "-O2 -ffast-math -DMEASURE=0"
              }
          }
      }
  </code></pre>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>If I define the MEASURE macro under the buildType release (only under release or both under release and debug) the macro appears defined in my native code with de value defined in release. It seems that it only isn't defined under the debug build type. Any ideas of what might be happening? </p>
  
  <p><em>I'm using Android Studio 2.3.3 with the latest stable updates up to date. Also, I've created a new test project to check if my old project is bugged, but same story in both.</em></p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>I'm starting to think that maybe Android Studio is not building the right buildType. Another funny thing is happening, I have tried defining a boolean build config field for java inside the debug buildType, and even though the auto generated BuildConfig.java has the field set to true, at execution time with debug I get an error that the variable does not exist (unless I put the variable under the release buildType). So it looks like no matter what I select in my build variants, it is always building the release build type.</p>
  
  <p>Here is my full test project build gradle:</p>
  
  <pre><code>apply plugin: 'com.android.library'
  
  android {
      compileSdkVersion 25
      buildToolsVersion "26.0.0"
  
      defaultConfig {
          minSdkVersion 21
          targetSdkVersion 25
          versionCode 1
          versionName "1.0"
  
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
          debug {
              minifyEnabled false
              versionNameSuffix '2.3.1'
              externalNativeBuild {
                  cmake {
                      cppFlags += "-DMEASURE=0"
                      cFlags += "-DMEASURE=0"
                  }
              }
              buildConfigField "Boolean", "DEBUG_MODE", "Boolean.parseBoolean(\"true\")"
          }    
      }
  
      externalNativeBuild {
          cmake {
              path 'src/main/cpp/CMakeLists.txt'
          }
      }
  }
  
  dependencies {
      compile fileTree(include: ['*.jar'], dir: 'libs')
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      compile 'com.android.support:appcompat-v7:25.3.1'
      testCompile 'junit:junit:4.12'
  }
  </code></pre>
  
  <p>Here is the auto-generated BuildConfig.java file:</p>
  
  <pre><code>public final class BuildConfig {
    public static final boolean DEBUG = Boolean.parseBoolean("true");
    public static final String APPLICATION_ID = "com.company.TestApp";
    public static final String BUILD_TYPE = "debug";
    public static final String FLAVOR = "";
    public static final int VERSION_CODE = 1;
    public static final String VERSION_NAME = "1.02.3.1";
    // Fields from build type: debug
    public static final Boolean DEBUG_MODE = Boolean.parseBoolean("true");
  }
  </code></pre>
  
  <p>And here is the error I get at execution time when I use BuildConfig.DEBUG_MODE in my code:</p>
  
  <pre><code>Error:(14, 27) error: cannot find symbol variable DEBUG_MODE
  </code></pre>
  
  <p>What might be happening? </p>