1

I am building a library in Android Studio and wish to have a BuildConfigField boolean, called performance, set to true for performance builds. However, it returns false and I need to fix it.

I followed the normal steps to create the field. My issue arises when performance is set to true in BuildConfig.java, but the Java code executes as if it were false. The library build variant is performance and app variant is debug.

Below is Java code which evaluates performance as false, inside a library file. Furthermore, "BuildConfig.BUILD_TYPE" returns "debug".

    if (BuildConfig.performance) {
        // do the thing
    }

The build.gradle file for the library

android {
    buildTypes {
        release {}
        debug {}
        performance {}

        // set "performance = true" for performance build type
        libraryVariants.all { variant ->
            variant.outputs.all {
                if (variant.getName().contains("performance")) {
                    variant.buildConfigField "Boolean", "performance", "true"
                } else {
                    variant.buildConfigField "Boolean", "performance", "false"
                }
            }

        }
    }
}

From another SO link, I have tried the following without success

android {
    publishNonDefault true
}
clever_trevor
  • 1,530
  • 2
  • 22
  • 42

1 Answers1

1

Inside your build variants, specify the fields:

  release {
      ...
      buildConfigField "Boolean", "performance", "false"
  }
  performance {
      ...
      buildConfigField "Boolean", "performance", "true"
  }
Chisko
  • 3,092
  • 6
  • 27
  • 45
  • This does not work. BuildConfig.java shows `public static final Boolean performance = true;` however it is not evaluated and logged as such. – clever_trevor Mar 09 '18 at 20:50
  • @clever_trevor what do you mean it is not evaluated as such?? – Chisko Mar 09 '18 at 21:11
  • The value of `performance` is true inside BuildConfig.java, however when I walk through the code, it is treated as false and does not execute the block inside the if-statement above. I logged `performance` and it's false as well. – clever_trevor Mar 09 '18 at 21:16
  • Did you remove your `libraryVariants.all` block? You have the field in the class as you wanted. Sure you are not changing it elsewhere? – Chisko Mar 09 '18 at 21:17
  • Yes, I copied your solution and no luck. The field is only used in the lines above. If I move the `"true"` line to debug and change the build variant to debug, then field evaluates correctly. However, I need a separate build variant. Do you think the issue has to do with using a debug variant for the app and performance variant for the library? – clever_trevor Mar 09 '18 at 21:23
  • What do mean with "far from ideal"? Did you specify the field for all your build types? I solved your question, your problem seems to be your own logic... – Chisko Mar 09 '18 at 21:26