Reputation: 3233
I have an android app that uses a certain url to communicate with our server when used in production. I want to be able to use a different url when I am debugging, so that I can run the app against a local instance of the server on my machine.
How can I do this without manually editing the url?
Upvotes: 1
Views: 163
Reputation: 1392
If you are using Android Development Tools for Eclipse, look into BuildConfig.DEBUG. There is some discussion of it at When does ADT set BuildConfig.DEBUG to false?
Upvotes: 0
Reputation: 3389
You could make a utility class that generates urls for you. Extending on what Robin Ellerkmann mentioned before me.
public class UrlGenerator{
private static final String STAGIN_URL = "http://www.statingurl.com/";
private static final String PRDOUCTION_URL = "http://www.productionurl.com/";
private static final boolean DEBUGGING = true;
private static String getBaseUrl(){
if(DEBUGGING)
return STAGING_URL;
else
return PRODUCTION_URL;
}
public static String getLoginUrl(){
return getBaseUrl() + "login";
}
}
And then throughout your app something like
String loginRequestUrl = UrlGenerator.getLoginUrl();
That way it is all managed in your utility class.
Upvotes: -1
Reputation: 3393
Using Android Studio, you can define build variants, and define strings in that case:
Plugin version greater than 0.7.x (current method)
android {
buildTypes {
debug {
buildConfigField "int", "FOO", "42"
buildConfigField "String", "FOO_STRING", "\"foo\""
}
release {
buildConfigField "int", "FOO", "52"
buildConfigField "String", "FOO_STRING", "\"bar\""
}
}
}
Plugin version less than 0.7 (old)
android {
buildTypes {
debug {
buildConfig "public final static int FOO = 42;"
}
release {
buildConfig "public final static int FOO = 52;"
}
}
}
You can access them with BuildConfig.FOO
android {
buildTypes {
debug{
resValue "string", "app_name", "My App Name Debug"
}
release {
resValue "string", "app_name", "My App Name"
}
}
}
You can access them in the usual way with @string/app_name
or R.string.app_name
source: https://stackoverflow.com/a/17201265/1096905 (All credits to him, i just copy&paste)
Upvotes: 4
Reputation: 2113
Use a global DEBUGGING boolean variable and if/else statements in your code.
private boolean DEBUGGING = true;
if (DEBUGGING) {
//Use local machine url here
} else {
//Use real url here
}
Upvotes: -2