0

It is possible to set a variable in debug or release of buildTypes of app module. This doc has explained how to do that, Android: Managing different server URL for development and release.

However, my problem is different slightly. I have Project_A which is dependency to my App_Module. A class on my Project_A needs to know this build is Debug or Release. I created a variable in buildTypes based on what above doc's said (in App_Module). However, the variable seems is not visible to this dependency (Project_A).

I have following code in a class of Project_A:

if (BuildConfig.DEBUG)
{
    MyConstants.URL_BASE = "https://my.debug.com";
}
else
{
    MyConstants.URL_BASE = "https://my.release.com";     
}

When I check the package of BuildConfig, the package belongs to Project_A (and there is no sign of App_Module in dropdown list of auto import packages). So what is your recommendation? How can I check build variant from dependency?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • http://stackoverflow.com/a/17201265/1852441 Did u try this ? – Jaswanth Manigundan Aug 09 '16 at 06:43
  • @JaswanthManigundan, yes, both (your link and mine) are similar answers that are using `debug` and `release` of `buildTypes`. Thanks. – Hesam Aug 09 '16 at 15:49
  • Your Module A can have a debug and release buildtype as well. If module A change de build variant then the other modules will change it accordingly. – cutiko Jan 06 '19 at 21:50

1 Answers1

0

I used following solution for my problem. I defined Debug variable in Application class of Project_A:

 // Placeholder for the BuildConfig.DEBUG flag. Should be overwritten by the final application class
 // with the correct value based on build variant (debug/release).
 public static boolean       DEBUG = true;

Then, I'm set it from Application class of App_Module.

// Setup the debug flag
PassengerLibApplication.DEBUG = BuildConfig.DEBUG;

Therefore, my above code will be changed to

if (Project_A_Application.DEBUG)
{
    MyConstants.URL_BASE = "https://my.debug.com";
}
else
{
    MyConstants.URL_BASE = "https://my.release.com";     
}
Hesam
  • 52,260
  • 74
  • 224
  • 365