構成を静的な最終定数にする必要がある場合、解決策は少し複雑になります。このソリューションは、マニフェストにデバッグ可能が正しく設定されているかどうかにも依存しているため、完全に確実なものではありません。
デバッグ可能を覚えておくために私が考えることができる唯一の方法は、後で説明するデバッグフラグを使用して、初期画面の背景色を赤に変更することです。少しばかげていますが、うまくいくでしょう。
アドレス変数をstaticfinalとして宣言し、それに値を割り当てないようにすることができます。
public static final String webServiceAddress;
以下を使用して、アプリがデバッグ可能に設定されているかどうかに関する情報を取得できます。
getApplicationInfo().flags;
つまり、アプリをリリースし、マニフェストでdebuggableをfalseに設定するときにスイッチを切り替える必要がありますが、ユーザーに表示したくないログメッセージをオフにするには、とにかくこれを行う必要があります。
デフォルトのアクティビティのonCreateでは、これを使用して分岐し、正しいアドレスを割り当てることができます。これが完全な例です。
//Store release configuration here, using final constants
public class ReleaseConfig {
//Don't set webServiceAddress yet
public static final String webSericeAddress;
public static boolean configSet = false;
static
{
//Set it as soon as this class is accessed
//As long as this class is first accessed after the main activity's onCreate
// runs, we can set this info in a final constant
webServiceAddress = MainActivity.configuredAddress;
}
}
public class MainActivity extends Activity {
public static String configuredAddress;
//This should be one of the first methods called in the activity
public void onCreate(Bundle savedInstanceState)
{
//Figure out if we are in debug mode or not
boolean debuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
//Save off our debug configuration
if (debuggable) configuredAddress = "the debuggable address";
else configuredAddress = "the production address";
//Access the static class, which will run it's static init block
//By the time this runs, we'll have the info we need to set the final constant
ReleaseConfig.configSet = true;
}