如何在构建时设置strings.xml值? [英] How to set strings.xml values at build time?

查看:382
本文介绍了如何在构建时设置strings.xml值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ant构建我的Android应用程序,并希望在构建时在我的strings.xml中设置值之一.例如,我可以使用

I'm building my Android application with Ant, and would like to set one of the values in my strings.xml at build time. For example, I could use

< string name ="app_name"> MyApp-DEBUG</string>

使用调试版本,或者我可以使用

with a debug build, or I could use

< string name ="app_name"> MyApp< string>

发布版本.这可能吗?

推荐答案

Ant中有两项任务可以提供帮助:

There are two tasks in Ant that can help:

首先是< replace> .您给它一个文件名,其中包含可以替换的参数,并给< replace>任务这些参数的值.它将替换它们在文件中.我不喜欢此任务,因为它用于替换受版本控制的内容,如果您不小心,最终可能会无意中更改文件.

First is the <replace>. You give it a file name that contains parameters that can be replaced, and you give the <replace> task the values for those parameters. It replaces them in the file. I don't like this task because it's used to replace stuff that is under version control, and if you're not careful, you can end up changing the file without meaning to.

<settings>
     <properties>
          <property name="server" value="@SERVER@"/>
     </properties>'
</settings>

替换任务

 <replace file="settings.xml">
    <replacetoken token="@SERVER@"  value="google.com"/>
 </replace>

我已经看到很多版本控制存储库,其中替换文件的版本3是偶然替换了具有替换参数的文件的文件(并且直到下一个版本都没有更改参数时才意识到这一点).然后,版本4是​​具有替换参数的版本2的副本.紧随其后的是错误的版本5,然后是版本6并不断恢复文件.

I've seen plenty of version control repositories where revision #3 of the replaced file was an accidental checkin of the the file with the replaced parameters (and not realizing it until the next release when the parameters didn't get changed). Then version #4 is a duplicate of version #2 which had the replacement parameters. Followed by a bad version #5, followed by a version #6 which restores the file, and on and on.

我的首选方法是将文件复制到另一个目录,然后使用< filterset>/< filter> 标记以在复制时更改文件:

My preferred method is to copy the file over to another directory, and use <filterset>/<filter> tokens to change the file while being copied:

 <copy todir="${target.dir}"
    file="settings.xml">
    <filterset>
        <filter token="SERVER" value="google"/>
    </filterset>
  </copy>

两者都可以使用属性文件,而不是指定单个令牌.< copy>/< filterset> 对可以获取文件的 fileset 并立即替换一堆令牌.(请注意不要将其传递为二进制文件!).

Both can use a property file instead of specifying individual tokens. The <copy>/<filterset> pair can take a fileset of files and replace a bunch of tokens at once. (Be careful not to pass it a binary file!).

这篇关于如何在构建时设置strings.xml值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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