Jona
Jona

Reputation: 13555

How to replace xml attribute value?

I'm looking for a way to update an xml file attribute value. For example the following xml I would like to replace the attribute android:versionCode value 30003 with another value. I'm having a hard time understanding how ant can do that using replace or regex.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.see"
    android:installLocation="auto"
    android:versionCode="30003"
    android:versionName="@string/app_version" >
</manifest>

Upvotes: 2

Views: 2780

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66714

If it is really that simple, you could use replaceregexp:

<property name="newVersionCode" value="30004"/>
<replaceregexp file="${src}/AndroidManifest.xml"
               match='(android:versionCode=").*(")'
               replace="\1${newVersionCode}\2"
               byline="true"
/>

Otherwise, you should consider using the XSLT task. You would want to copy the original file into a temp directory, then apply a stylesheet where the new value is specified as a parameter, and generate the output over the original manifest.

Upvotes: 4

Related Questions