Ahmed
Ahmed

Reputation: 3260

Change application name through ant script in android

I am trying to change an application name on compilation through an ant script(which is in the build.xml). Application name is stored in strings.xml. Following is the piece of code,

<target name="changeName">
        <property
            name="applicationName"
            value="30004" />
        <replaceregexp
            file="C:\Users\<user-name>\git\appname\res\values\strings.xml"
            match='&lt;string name="app_name"&lt;test&lt;/string&lt;'
            replace="\1${applicationName}\2"
            byline="true"/>         

    </target>

But it doesnt change the name in strings.xml. Can someone please correct me on what I am doing wrong? Also please let me know how I can achieve similar operation through relative paths.

Thanks

Edit: Following is the xml that needs to be changed through the above mentioned ant script

<string name="app_name">Test Application</string>

Upvotes: 1

Views: 643

Answers (1)

CAustin
CAustin

Reputation: 4614

The following replacement will change the "app-name" attribute for any <string> element that contains this attribute:

<replaceregexp
    file="C:\Users\<user-name>\git\appname\res\values\strings.xml"
    match="&lt;string name=&quot;.+&quot;"
    replace="&lt;string name=&quot;${applicationName}&quot;"
/>

To save yourself from escape code hell, you might want to set some properties using CDATA before running the replacement. This way you can clearly see the regex patterns:

<property name="regex.match"><![CDATA[<string name=".+"]]></property>
<property name="regex.replace"><![CDATA[<string name="${applicationName}"]]></property>
<replaceregexp
    file="C:\Users\<user-name>\git\appname\res\values\strings.xml"
    match="${regex.match}"
    replace="${regex.replace}"
/>

Upvotes: 1

Related Questions