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

查看:21
本文介绍了如何在构建时设置 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

MyApp-DEBUG

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

with a debug build, or I could use

MyApp

用于发布版本.这可能吗?

for a release build. Is this possible?

推荐答案

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

There are two tasks in Ant that can help:

首先是 .你给它一个包含可以替换的参数的文件名,然后给它<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 的副本,版本#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.

我的首选方法是将文件复制到另一个目录,并使用 ;/<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>

两者都可以使用属性文件而不是指定单个令牌./ 对可以获取一个 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天全站免登陆