在我来说,我只是想改变strings.xml
几个值的不同版本之间。
首先,我必须加载ant-contrib
库,定义for
循环任务:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/ant-contrib-1.0b5-SNAPSHOT.jar" />
</classpath>
</taskdef>
我把我的配置,config.names
的名单,在properties
文件:
config.url.root=http://projectserver.aptivate.org/
config.names=student-production, teacher-production, student-testing, teacher-testing
,并确定一个build-all
目标,即在config.names
上循环:
<target name="build-all">
<for param="config.name" trim="true" list="${config.names}">
<sequential>
定义自定义resources
目录为每一个在config.resources
财产保存目录名称:
<var name="config.resources" unset="true" />
<property name="config.resources" value="bin/res-generated/@{config.name}" />
删除它,然后从res
复制全球资源放入其中:
<delete dir="${config.resources}" />
<copy todir="${config.resources}">
<fileset dir="res"/>
</copy>
变化-
到/
在配置名称中,使其成为URL参数中的路径:
<var name="config.path" unset="true" />
<propertyregex property="config.path"
input="@{config.name}" regexp="-"
replace="/" casesensitive="true" />
运行XSLT转换修改strings.xml
文件:
<xslt in="res/values/strings.xml"
out="${config.resources}/values/strings.xml"
style="ant/create_xml_configs.xslt"
force="true">
<param name="config.url.root" expression="${config.url.root}" />
<param name="config.name" expression="@{config.name}" />
<param name="config.path" expression="${config.path}" />
</xslt>
这是我使用XSLT样式表:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="config.url.root" />
<xsl:param name="config.name" />
<xsl:param name="config.path" />
<!-- http://my.safaribooksonline.com/book/xml/9780596527211/creating-output/xslt-id-4.6 -->
<xsl:template match="/">
<!--
This file is automatically generated from res/values/strings.xml
by ant/custom_rules.xml using ant/create_xml_configs.xslt.
Do not modify it by hand; your changes will be overwritten.
-->
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- the value of update_server_url must end with a slash! -->
<xsl:template match="string[@name='update_server_url']/text()">
<xsl:value-of select="$config.url.root" /><xsl:value-of select="$config.path" />/
</xsl:template>
<xsl:template match="string[@name='app_version']/text()">
<xsl:value-of select="." />-<xsl:value-of select="$config.name" />
</xsl:template>
</xsl:stylesheet>
再换custom_rules.xml
,我再从原来的提取app_version
(未修改)res/values/strings.xml
:
<xpath input="res/values/strings.xml"
expression="/resources/string[@name='app_version']"
output="resources.strings.app_version" />
并使用antcall
任务调用debug
编译:
<antcall target="debug">
<param name="resource.absolute.dir" value="${config.resources}" />
<param name="out.final.file" value="${out.absolute.dir}/${ant.project.name}-${resources.strings.app_version}[email protected]{config.name}.apk" />
</antcall>
两个更改的属性值:
resource.absolute.dir
告诉debug
目标来使用我的修改res
目录,在上面的config.resources
属性定义;
out.final.file
告诉它生成一个不同名称的APK,包括配置名称(例如student-testing
)和从strings.xml
中提取的版本号。
然后,最后,我可以从命令行运行ant build-all
并构建所有四个目标。多一点点的脚本,只是build-all
目标年底前,列出了编译APK文件一起供参考:
<echo message="Output packages:" />
<for param="config.name" trim="true" list="${config.names}">
<sequential>
<echo message="${out.absolute.dir}/${ant.project.name}-${resources.strings.app_version}[email protected]{config.name}.apk" />
</sequential>
</for>
的可能重复[Android的 - 同一应用程序的多个定制版(HTTP://计算器问题/ 1222302/android - 多个定制版本的同一应用程序) – EboMike 2010-10-21 00:57:01
就我所知,没有任何解决方案与Eclipse很好地集成。对于为什么这样一个简单的任务似乎如此复杂,我其实很难过。不要开始火焰战争,但我已经看过了我们的iPhone开发者的iPhone开发环境,从外部传递params到你的应用程序是非常简单的。这让我难过。 – 2010-10-21 01:07:10
我知道如何用maven-android-plugin来做到这一点,其实很简单。然而,它需要将你的应用程序与maven绑定,此外,在maven-android-plugin/m2e-android中似乎与最新的Android SDK(r14和r15)有一些不兼容,可能不是你想要的。 – yorkw 2011-11-02 09:01:02