0

I have an android application that is already in production. Some times, I get feedback on it and I have to fix bugs locally as it communicates with a server. I have all these set up locally so the changes I make are basically what will happen in production. In my app, I have a constants class like this one;

public class Constants{
    private Constants(){}
    public static final String DEV_URL = "http://dev.app.com/";
    public static final String PROD_URL = "http://prod.app.com/";
}

With this kind of setting, when in development, I comment out the prod url and vice versa when publishing the app. I don't like that. I would like to have a dynamic way to have both URLs to exist but only work on the context the app is in i.e. release or debug. Something like:

public class Constants{
    private Constants(){}
    if(DEBUG){
    // assuming it's debug mode
        public static final String BASE_URL = "http://dev.app.com/";
    }
    else{
    // assuming it's release mode
        public static final String BASE_URL = "http://prod.app.com/";
    }
}

The closest I have come to it is using this method but I can't (or have no idea) of how to get the context. Any help or direction will be a big help.

Community
  • 1
  • 1
musale
  • 534
  • 8
  • 18
  • simplest solution will be using if (BuildConfig.DEBUG) { // do something for a debug build } it returns true for debug else false for alternate options [try these](http://stackoverflow.com/questions/7085644/how-to-check-if-apk-is-signed-or-debug-build) – Bali Nov 22 '16 at 08:38
  • @Bali I tried `BuildConfig.DEBUG` but I get this error `Unknown Class: Build.Config` – musale Nov 22 '16 at 08:44
  • what's the import statement for BuildConfig you are using makesure it's yourpackagename.BuildConfig – Bali Nov 22 '16 at 09:02
  • @Bali yes it's exactly that and it throws the `Unknown Class: BuildConfig.DEBUG` error – musale Nov 22 '16 at 09:03
  • try alternate solution in provided link as this works fine with my code – Bali Nov 22 '16 at 09:20
  • @Bali at what point do you use it? in my case, I want to use it when declaring base urls i.e. dev url and prod url. Same name, different values for dev and prod. You get it? Check the update – musale Nov 22 '16 at 09:24
  • i'm using it mainly in activities – Bali Nov 22 '16 at 09:28
  • @Bali well mine's on that constants class. Tried it inside constructor of the class and it didn't throw an error. Let me check some way to work around that. will update if I manage with my current setting – musale Nov 22 '16 at 09:31
  • [BuildConfig](http://stackoverflow.com/documentation/android/95/gradle-for-android/2542/define-and-use-build-configuration-fields#t=201611220937136589154) is generated in the gen folder as [.BuildConfig.java](http://stackoverflow.com/a/9822140/519334). You may need to "rebuild all" for the first time generation of BuildConfig.java – k3b Nov 22 '16 at 10:00

0 Answers0