Android - 使用 ant 创建更改配置值的构建配置
我想要的是一种具有依赖于构建配置的设置的方法。举一个具体的例子,我的 Android 应用程序连接到一个 Web 服务。在开发中,我希望从可配置值中提取服务 url。在测试中,我想要引入一个不同的值。在生产中,我想要引入另一个值。
所以,在代码中我有这样的东西:
public class HttpRequestHelper
{
private static String GetServiceUrl(ServiceAction action)
{
return serviceUrl + action.toString();
}
}
默认情况下(通过 Eclipse 调试/运行时),我希望该 url 为 http:// localhost:1234
在测试中我想要 https://test.mydomain.com
在生产中我想要 < a href="https://mydomain.com" rel="noreferrer">https://mydomain.com
我是 eclipse 和 ant 的新手,自从使用 java 以来已经很长时间了。我该如何进行设置? build.xml 应该是什么样子?我知道当我想构建测试/产品版本时,我需要使用命令行。没关系。但我不知道如何根据构建自动设置此 serviceUrl。我什至不确定放置此信息的最佳位置(资源、属性文件?)。我真的想避免设置它、构建、设置它、构建等等。
What I want is a way to have settings that are dependent on build configuration. To give a specific example, my android application connects to a web service. In development, I want the service url to be pulled in from a configurable value. In Test, I want a different value pulled in. In production, yet another value.
So, in code I have something like this:
public class HttpRequestHelper
{
private static String GetServiceUrl(ServiceAction action)
{
return serviceUrl + action.toString();
}
}
By default (when debugging/running through eclipse), I want that url to be http://localhost:1234
In Test I want https://test.mydomain.com
In Production I want https://mydomain.com
I am new to eclipse and ant and it has been a long time since I used java. How do I go about setting this up? What should the build.xml look like? I understand that when I want to build the test/prod versions I will need to use the command line. That's okay. But I don't know how to get this serviceUrl auto-set dependent on the build. I'm not even sure the best place to put this information (a resource, a properties file?). I really want to avoid setting it, building, setting it, building, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如上面提到的答案所说,您必须将 URL 放入属性文件中,例如 dev.properties、test.properties、prod.properties 等。
现在您唯一需要做的就是使您的构建足够智能以选择属性文件取决于环境。
这可以通过向 ANT 传递参数来完成,例如:
$ ant -file MyBuild.xml -DcurrentEnv=dev (用于开发环境)
$ ant -file MyBuild.xml -DcurrentEnv=test (用于测试)
$ ant -file MyBuild.xml -DcurrentEnv=prod (For Production)
在你的构建脚本中,你可以这样包含你的属性文件:
有了这个,无论你在构建时提供什么名称,属性文件具有该名称的将被拿起。
As answers mentioned above says, you have to place the URLs in a property file like dev.properties, test.properties, prod.properties etc..
Now only thing that you need to do is making your build intelligent enough to choose a property file depending upon environment.
That can be done by passing a parameter to ANT, something like:
$ ant -file MyBuild.xml -DcurrentEnv=dev (For Development environment)
$ ant -file MyBuild.xml -DcurrentEnv=test (For Test)
$ ant -file MyBuild.xml -DcurrentEnv=prod (For Production)
Inside your build script, this is how you can include your property file:
With this in place, whatever name you supply at the time of build, property file with that name will be picked up.
您可以尝试在 build.properties 文件中包含以下属性文件:
并且您可以拥有 http://localhost:1234或 local.properties 中的 https://test.mydomain.com 用于开发和集成测试,它可能是在 default.properties 中设置为 https://mydomain.com。
通过这样做,您将在不同的构建环境中获得不同的 service.url 值。您可以使用该值生成一个配置文件,并将其解析到您的代码中,或者将其设置为 env 变量,或者只是将其放入资源文件中,Android 将为您读取它:
You could try to have a following property file in your build.properties file:
And you could have http://localhost:1234 or https://test.mydomain.com in local.properties for your development and integration testing, and it could be set to https://mydomain.com in default.properties.
By do ing this, you have will get different value for service.url in different build environment. You could use that value to generate a config file, and parse it into your code, or set it to env variable, or just put it into a resource file, and Android will read it for you:
我首先将 URL 放入属性文件中,然后将其放入类路径中。制作测试和生产属性文件。然后根据构建将正确的文件放置到类路径上并在运行时提取属性。
I would start by placing the urls into a properties file that you can then place onto the classpath. Make a test and a production properties file. Then depending on the build place the correct file onto the classpath and pull the properties at runtime.
找到了一个教程,其中详细介绍了使用 ant 自动化构建系统、创建和使用构建配置以及使用一个命令构建发布项目的所有细节。这是: http://www.androidengineer .com/2010/06/using-ant-to-automate-building-android.html
看起来有点长,但它涵盖了所有涉及的步骤和细节。
Found a tutorial which goes through all the details of using ant to automate a build system, to create and use build configurations, as well as to build the release project with one command. Here it is: http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html
Seems a little long, but it goes through all the steps and details involved.