Using AndroidManifest.xml version code in ant

android, ant, java

Solution

Maybe there is an android dedicated solution, which i dont know, but apart from that you can treat android manifest file as any other xml file -> so all you need is an ant task for parsing xml file and retrieving values from it.

as writing custom ant task in java is not a rocket science - there allready is something you could try to use.

see: How to parse a xml by ANT

so in your case it would be sth like (with use of http://www.oopsconsultancy.com/software/xmltask/):

<target name="retrieveFromManifest" desciption="retrieveFromManifest">
    <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
    <xmltask source="${pathToAndroidManifestFile}">
        <copy path="/manifest/@android:versionCode" property="androidVersionCode"/>
        <copy path="/manifest/@android:versionName" property="androidVersionName"/>
        <copy path="/manifest/@package" property="androidPackage"/>
    </xmltask>
    <echo message="Version code: ${androidVersionCode}" />
    <echo message="Version name: ${androidVersionName}" />
    <echo message="package: ${androidPackage}" />
</target>

EDIT:

If you think about parsing this in your android project's build.xml then you have xpath task "out of the box". Any automatically generated build file for android project imports the global sdk's build file `<import file="${sdk.dir}/tools/ant/build.xml" />`, in which you have:

<taskdef name="xpath"
        classname="com.android.ant.XPathTask"
        classpathref="android.antlibs" />

And example usage some lines later:

<xpath input="AndroidManifest.xml" 
                expression="/manifest/application/@android:hasCode"
                output="manifest.hasCode" default="true"/>

Problem

Does anyone know if it's possible to get variables from the AndroidManifest.xml for ant use? Specifically, I'm trying to get `versionCode`,`versionName`, and `packageName` out somehow. Thanks in advance. Conclusion It seems that this is too troublesome to do, and since this problem is easily solved with Maven I've given up on the possibility of a quick solution for now.

Original source

Related problems