0

I have a java statement like this:

int MAX_CACHE_AGE = TimeUnit.DAYS.toMillis(14);

And I want to have a max cache age of 0 when debugging. However, I also want to minimize the complexity of my build. What are some ways I can do this? How do you do this? I am developing for Android so if your method is Android specific, tell my anyway.

Drew
  • 12,578
  • 11
  • 58
  • 98
  • 1
    See: http://stackoverflow.com/questions/7022653/how-to-check-programmatically-whether-app-is-running-in-debug-mode-or-not – Tushar Mar 30 '13 at 02:11
  • http://stackoverflow.com/questions/15696361/differentiating-debug-and-production-build-of-an-android-app/ – 323go Mar 30 '13 at 02:14
  • Those answers would be nice, but getApplicationInfo().flags is always 0 for me. – Drew Mar 30 '13 at 02:32
  • Those answers don't work for me: getApplicationInfo().flags is always 0. Even with android:debuggable in the application part of my AndroidManifest. Also, they require a context to be available, which is not true everywhere. – Drew Mar 30 '13 at 02:34

1 Answers1

3

Initialize this variable in the onCreate() method if it's in an application component, or in the constructor if it's in a normal Java class. The Android tools have a BuildConfig class that is auto generated. You can use its DEBUG field to alter the value.

if(BuildConfig.DEBUG) {
    MAX_CACHE_AGE = TimeUnit.DAYS.toMillis(0);
} else {
    MAX_CACHE_AGE = TimeUnit.DAYS.toMillis(14);
} 
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195