22

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.

stid.smth
  • 1,525
  • 2
  • 11
  • 11

4 Answers4

38

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.

cistearns
  • 2,488
  • 21
  • 24
  • Thanks. Your suggestion works well. The comments in my build.xml is a bit buggy with the following lines: The rules file is imported from /platforms//templates/android_rules.xml Instead of including the 'android_rules.xml', you should include '/tools/ant/ant_rules_r3.xml'. Otherwise the build will fail. – stid.smth Mar 14 '11 at 03:03
  • Is there a way to automate this with Eclipse APK export? – Sney Oct 05 '11 at 11:51
  • Not that I am aware of. ANT is intended to be the place you do build automation. You might file it as a feature request to the ADT team. – cistearns Oct 21 '11 at 06:17
  • @stid.smth Interestingly there is no ant_rules_r3.xml in my android sdk installation at /tools/ant - should I just create it manually? If yes, what it's supposed to look like? I also checked /platforms//templates/ - got no rule files there neither. – AgentKnopf Mar 28 '12 at 18:10
  • On an additional node: The beginning the the build.xml file in /tools/ant reads: which makes me wonder if the rules are included in the build file oO ? – AgentKnopf Mar 28 '12 at 18:12
  • 1
    In the latest version of the Android SDK the ANT build system has changed. You will want to look at cloning /tools/ant/build.xml. – cistearns Mar 29 '12 at 16:43
  • Maybe you can show us your files (build.xml, custom_rules.xml, ant.properties, , as this solution I don't have a custom_rules.xml file at all. – JPM Oct 10 '12 at 19:35
9

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

mivola
  • 439
  • 6
  • 9
4

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

Jagadeesh
  • 41
  • 1
-4

Yes there is a way to generate apk file with any name i give a process to you fallow this process the you got .apk mention your name.

1-Right click on application in package explorer . 2-Chose export. 3-chose Android 4-Choose Export Android application. 5-Create new keystore . 6- fill the form. 7-: Give apk file name what you want 8-:give location to apk to store apk .

Then you find solution of your problem. I hope this is help.

DynamicMind
  • 4,240
  • 1
  • 26
  • 43
  • Sorry but I mean to barely use the 'ant' command instead of eclipse IDE. I'm not using eclipse currently. – stid.smth Mar 11 '11 at 09:19
  • 1
    I also think you misunderstood the question, the asker is searching for a automated addition of the version number to the filename. – Peterdk Mar 16 '11 at 21:52