I'm building an Android application and would like to maintain a few environment variables that I can tweak depending on whether I'm in development mode or release mode. For example, I need to invoke a web service and the URL will be slightly different in either mode. I'd like to externalize this and other settings so I can change them easily based on my target deployment.
Are there any best practices or anything in the SDK to assist with this need?
Android build.gradle has Handles Debug and Release Environment well.
Append the following code snippet in
build.gradle
fileNow you can access the variable like below
I came across another approach today by accident that seems really straight forward.. Look at Build.TAGS, when the app is created for development this evaluates to the String "test-keys".
Doesn't get much easier than a string compare.
Also Build.MODEL and Build.PRODUCT evaluate to the String "google_sdk" on the emulator!
I would check out isDebuggerConnected
According to this stackoverflow post, in SDK Tools version 17 (we're on 19 as of this writing) adds a
BuildConfig.DEBUG
constant that is true when building a dev build.@viktor-bresan Thanks for a useful solution. It'd be more helpful if you just included a general way to retrieve the current application's context to make it a fully working example. Something along the lines of the below:
How about something like the code below ...