Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 863
stid.smth Avatar asked Mar 11 '11 07:03

stid.smth


3 Answers

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.

like image 128
cistearns Avatar answered Nov 04 '22 00:11

cistearns


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

like image 32
mivola Avatar answered Nov 04 '22 00:11

mivola


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

like image 4
Jagadeesh Avatar answered Nov 04 '22 00:11

Jagadeesh