2

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>

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Its deleting the next attributes(ex versionName), if the entire xml tag is in the single line. Do we have any other way to resolve it?
Either tighten up the regex, or look to use a more robust find and replace implementation, such as the XSLT task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.