Android - 使用 ant 创建更改配置值的构建配置 [英] Android - use ant to create build configurations that change configuration values

查看:27
本文介绍了Android - 使用 ant 创建更改配置值的构建配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是一种拥有依赖于构建配置的设置的方法.举一个具体的例子,我的 android 应用程序连接到一个 web 服务.在开发中,我希望从可配置值中提取服务 url.在测试中,我想要一个不同的值.在生产中,另一个值.

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();
    }
}

默认情况下(通过 eclipse 调试/运行时),我希望该 url 为 http://localhost:1234

By default (when debugging/running through eclipse), I want that url to be http://localhost:1234

在测试中我想要 https://test.mydomain.com

在生产中我想要 https://mydomain.com

我是 eclipse 和 ant 的新手,自从我使用 java 已经很长时间了.我该如何设置?build.xml 应该是什么样的?我知道当我想构建测试/生产版本时,我需要使用命令行.没关系.但我不知道如何根据构建获得此 serviceUrl 自动设置.我什至不确定放置这些信息的最佳位置(资源、属性文件?).我真的很想避免设置,构建,设置,构建等等.

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.

推荐答案

正如上面提到的答案所说,您必须将 URL 放在属性文件中,例如 dev.properties、test.properties、prod.properties 等.

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.

这可以通过向 ANT 传递参数来完成,例如:

That can be done by passing a parameter to ANT, something like:

$ ant -file MyBuild.xml -DcurrentEnv=dev (针对开发环境)
$ ant -file MyBuild.xml -DcurrentEnv=test(测试用)
$ ant -file MyBuild.xml -DcurrentEnv=prod(用于生产)

$ 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:

<target name="jarMe">
    <jar destfile="sample.jar" basedir="src" includes="${currentEnv}.properties"/>
</target>

有了这个,无论您在构建时提供什么名称,都将选取具有该名称的属性文件.

With this in place, whatever name you supply at the time of build, property file with that name will be picked up.

这篇关于Android - 使用 ant 创建更改配置值的构建配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆