简体   繁体   中英

Using AndroidManifest.xml version code in ant

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.

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"/>

Actually it is not that hard:

<xmlproperty file="AndroidManifest.xml" prefix="mymanifest" collapseAttributes="true"/>

And you use it like this:

${mymanifest.manifest.android:versionName}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM