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

androidantapkversion

Normally we build android package in debug mode with the command

ant debug

and got the apk filename AppName-debug.apk.

Is there any way to generate the apk file in the format of AppName-debug-<version-number>.apk directly without 'mv' command?

Thanks.

Best Solution

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.