How to include version string in the filename when building Android apk with ant? How to include version string in the filename when building Android apk with ant? android android

How to include version string in the filename when building Android apk with ant?


I have done this in an automated way, by using the info from the Android manifest.

My approach was to modify the local project ant build.xml file to import a custom copy of the master build.xml file, in the latest version of the SDK the master file that is imported into your local file is located at <SDK>/tools/ant/build.xml (was android_rules.xml in previous versions) Note the import line in the default build.xml file local to your project.

The comments in the local build.xml created by the Android tools contain details on how to do customization, such that future updates to the tools will not break your changes.

In the custom rules file add:

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

This reads in the manifest as a set of ant properties, with a custom prefix to ensure no name collision.

You can then add ${mymanifest.manifest.android:versionName} to any of the spots that the apk name is defined, just search for .apk in the custom rules file.

I've used this process to automate versioning files from a build server.

With some additional tweaks it is also possible to have your release app signed automatically, buy setting the passwords in properties and using those in the signing task. The actual passwords where stored in a separate properties file that only lived on the build server. While a minor security risk, in some cases it is out weighed by the need for full end to end build automation.


Another, more Android-like solution is to use the XPATH task as it is done in the ${sdk.dir}/tools/ant/build.xml:

<xpath input="AndroidManifest.xml" expression="/manifest/@android:versionName"                        output="versionName" default="unknown"/><echo message="name: ${versionName}"></echo>

This requires the definition of the task:

<path id="android.antlibs">    <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" /></path><taskdef name="xpath"        classname="com.android.ant.XPathTask"        classpathref="android.antlibs" />

I don't know which is the minimum SDK version, but it works with SDK14.

Regards,Michael


Solution described in the following post has custom_rules.xml file that can be just copied to achieve the desired result.http://jeffreysambells.com/2013/02/14/build-your-android-apk-with-the-manifest-version-number