views:

171

answers:

3

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?

Thanks

+2  A: 

I would check out isDebuggerConnected

Joseph
Thanks, that may come in handy one day, but I can technically be running the emulator in development mode w/o a debugger attached.
Joe Holloway
Technically you can use notepad.exe to write Android programs.
sirlancelot
@sirlancelot My OS doesn't provide a `notepad.exe`. Regardless, I can come up with other scenarios where `isDebuggerConnected` isn't enough. For example, what if I need to run my application in development mode on a physical device? What about running an automated test suite? There are many ways to address this configuration problem, which is why I asked about best practices to see what others have done. Anyway, I gave @Joseph a +1 for his contribution, but don't feel that it fully addressed my question.
Joe Holloway
+3  A: 

The following solution assumes that in manifest file you always set android:debuggable=true while developing and android:debuggable=false for application release.

Now you can check this attribute's value from your code by checking the ApplicationInfo.FLAG_DEBUGGABLE flag in the ApplicationInfo obtained from PackageManager.

The following code snippet could help:

PackageInfo packageInfo = ... // get package info for your context
int flags = packageInfo.applicationInfo.flags; 
if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
    // development mode
} else {
    // release mode
}
Viktor Bresan
Thanks, this looks like a decent solution.
Joe Holloway
this came in handy, thanks!
schwiz
A: 

Here's the method I use:

http://whereblogger.klaki.net/2009/10/choosing-android-maps-api-key-at-run.html

I use it to toggle debug logging and the maps API key.

wirbly