BuildConfigField is not accessible in androidTest class

My project has different build types i.e. debug, beta and production and also have different product flavors i.e. QA and Integration. I have defined a

buildConfigField 'int', 'APP_THEME', ''

in the productFlavors to have a separate theme for each flavor. The generated for app source set have the APP_THEME field and it is working as expected.

Recently I have started writing instrumentation tests for my app. When I try to run these tests Android studio gives me the error that can not resolve AKTheme i.e.

final int APP_THEME = in the generated for the test source set.

It seems that is not accessible to the generated file (test source set). I searched over internet but didn't find any help.

1 answer

  • answered 2018-05-16 05:26 Vladyslav Matviienko is a reference, not a value, while in BuildConfig you can only use values.
    There are couple of ways to achieve what you want:

    1. Use the String name of the style in BuildConfig:
      buildConfigField 'String', 'APP_THEME', '"AKTheme"'
      and then in code to get the style res id:
      int style = context.getResources().getIdentifier(BuildConfig.APP_THEME, "style", context.getPackageName());
      Now you can use style.
    2. You can use different source-sets.
      If you are using different buildtypes, you can create a directory for that build type, and put any different resources specially for that build type in that directory. The directory should be created in the same directory as main sources directory, and named exactly the same as the buildType. Details: