I have dozens of api keys to facebook and twitter and many other services, what is the standard way to have different values for the keys depending on if I am making a development build vs. a staging build vs. 开发者_如何学JAVAa production build?
You can use a static flag to use it in a switch block to define your keys. That works for a simple project with two or three alternative keys.
If you really have that many, to use in several projects, I suggest you to add them to a helper class, so you minimise the code changing in your classes. Something like:
public class BuildHelper {
public static final int DEBUG=0;
public static final int STAGING=1;
public static final int PRODUCTION=2;
public static int BUILD;
public static String getFbKey() {
switch(BUILD) {
case DEBUG: return "xxx";
case STAGING: return "yyy";
case PRODUCTION: return "zzz";
}
return null;
}
public static String getTwitterKey() {
switch(BUILD) {
case DEBUG: return "xxx";
case STAGING: return "yyy";
case PRODUCTION: return "zzz";
}
return null;
}
}
and use it as:
public class YourClass extends Activity {
public static String FB_KEY;
public static String TWITTER_KEY;
//etc.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BuildHelper.BUILD=BuildHelper.DEBUG; // or STAGING or PRODUCTION
FB_KEY = BuildHelper.getFbKey();
TWITTER_KEY = BuildHelper.getTwitterKey();
//etc.
}
}
I would keep them in separate properties files and reference the applicable property file in your build script/eclipse class path depending on what you're doing.
精彩评论