4

所以我试图弄清楚如何在 Eclipse 中设置编译标志,所以当我开发我的 Android 应用程序时,我可以进行特定的构建。示例我有一个基于 WebView 的应用程序,我希望能够构建一个具有不同 URL 的 QA 版本,web.loadUrl("http://www.com");我真的不希望有 2 个项目 QA 和 Release。我一直在研究一种基本自动化这个过程的方法。在编译和测试应用程序之前,我不想每次都去更改代码中的 URL。

4

2 回答 2

3

使 url 字符串可配置,例如通过属性文件,您可以借助 java Properties轻松访问该文件。如果要更改 URL,请确保不需要重新编译应用程序。

Properties applicationConfiguration = new Properties();
applicationConfiguration.load(this.getClass().getResourceAsStream("application.properties"));
String url = applicationConfiguration.getProperty("my.url", "http://defaulurl.com");
于 2013-04-17T16:50:03.610 回答
2

In SDK Tools, Revision 17 (March 2012):

  • Added a feature that allows you to run some code only in debug mode. Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. You can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions.

I find that the easiest way to accomplish what you want, is by checking the BuildConfig.DEBUG boolean.

The DEBUG boolean is always true when developing, only when you export a signed or unsigned apk, is it set to false. You can use it as:

if (BuildConfig.DEBUG) { // Developing
    myURL = "http://www.bing.com";
}

// Or the other way around:

if (!BuildConfig.DEBUG) { // Released (signed app)
    myURL = "http://www.google.com";
}
于 2013-04-20T01:13:27.573 回答