25

So far I have been focusing on my application's programming and paid little attention to making the build process smarter. Thus I have been doing things pretty much manually (the "dumb way"), including updating by hand android:versionCode and android:versionName in AndroidManifest.xml.

I would like now to automatically (i.e. upon Build or upon Export):

  1. Fetch from git the latest tag/branch containing build and version codes.
  2. Parse them so that I can assign them to the respective fields in AndroidManifest.xml.
  3. Modify AndroidManifest.xml accordingly.
  4. Proceed with the normal build process (Eclipse+ADT, no Ant whatsoever), as if I did 1-2-3 by hand...

I found a few clues about a "pre-build step", builders and build.xml, but I have no idea where to find those and where to start.

Any tips or pointers on where I could find more information on the subject? (a step-by-step tutorial would be ideal)

Update 1: I found this thread to be suggesting that I:

  1. Right-click on the project, Properties > Builders
  2. Add a builder that points to the project's Ant build file.
  3. Order that builder to be invoked before the Java builder

Fine, but where is the project's Ant build file? Where do I find it?

Update 2: Apparently, it's possible to export the entire project into an Ant file. But I am not sure that's I want. Must a pre-build step always include an Ant build file?

Update 3: Is building an Ant file, only for the pre-build step, the right approach?

2
  • 4
    nice... me too expecting the answer..
    – prakash
    Sep 14, 2012 at 14:46
  • Easy piece if using Maven, check out manifestVersionCode and manifestVersionName here.
    – yorkw
    Sep 15, 2012 at 3:17

4 Answers 4

18

Here's what I use to dynamically assign a versionCode and versionName to AndroidManifest.xml. It works only when building with ant, so you'll have to install it first. Then go to the project directory in your command line and execute "android update project -p .", which will create the necessary files for building with ant, like local.properties and build.xml.

Then open build.xml and place this inside:

  <target name="-pre-build" depends="-custom-git-version,-custom-manifest-version">
  </target>

  <!-- Packages the application. -->
  <target name="-post-build">
    <antcall target="-custom-restore-manifest"/>

    <property name="suffix" value="${git.commits}-${git.version}.apk" />

    <exec executable="sed" inputstring="${out.final.file}" outputproperty="out.final.renamedfile">
      <arg value="s/\.apk/-${suffix}/" />
    </exec>

    <copy file="${out.final.file}" tofile="${out.final.renamedfile}" />
    <echo>Final file copied to: ${out.final.renamedfile}</echo>
  </target>  

  <!-- Custom targets -->
  <target name="-custom-git-version">
    <exec executable="sh" outputproperty="git.commits">
      <arg value="-c" />
      <arg value="git log --pretty=format:'' | wc -l" />
    </exec>
    <echo>git.commits: ${git.commits}</echo>
    <exec executable="git" outputproperty="git.version">
       <arg value="describe" />
       <arg value="--tags" />
       <arg value="--long" />
    </exec>
    <echo>git.version: ${git.version}</echo>
  </target>

  <target name="-custom-manifest-version">
     <echo>Creating backup of AndroidManifest.xml</echo>
     <copy file="AndroidManifest.xml" tofile="AndroidManifest.xml.antbak" preservelastmodified="true" />

     <replaceregexp
        file="AndroidManifest.xml"
        match='android:versionCode="(\d+)"'
        replace='android:versionCode="${git.commits}"' />

     <replaceregexp
        file="AndroidManifest.xml"
    match='android:versionName="(\d+\.\d+)\.\d+"'
    replace='android:versionName="\1.${git.commits}"' />
  </target>

  <target name="-custom-restore-manifest">
    <echo>Restoring backup of AndroidManifest.xml</echo>
    <move file="AndroidManifest.xml.antbak"
          tofile="AndroidManifest.xml"
          preservelastmodified="true"
          overwrite="true" />
  </target>

The output of this is not exactly what you want, but it is a start - feel free to modify it :) The result is something like "yourapp--.apk

Using this you'll build your application with executing "ant clean debug", or "ant clean release", depending on what you want. You can also create "ant.properties" file with this content:

key.store=keystore_file
key.store.password=some_password
key.alias=some_alias
key.alias.password=some_other_password

to enable automatic signing of your app.

You should also read this: http://developer.android.com/tools/building/building-cmdline.html

3
  • Thanks +1. I now need to find some time to test this solution... I will accept when I get it working. Oct 12, 2012 at 14:41
  • Thanks for the code. It work in my case as well with some minor modification for my purpose.
    – Aki
    Apr 23, 2013 at 22:09
  • this is such a great answer, i wish i could upvote more times
    – yano
    Jun 29, 2015 at 23:02
3

You are on the right track with setting up a pre-build step, but the ant build file is something you'll create yourself from scratch. Eclipse has some ant scripts that it uses externally that handle the automated compilation, packaging and stuff, but you want to create a separate one that just does these extra steps you want.

So, you're going to have to learn a bit about ant scripting to get this done. Some of those links you found give you the basic idea of how to create a simple ant file. To add to that, some of the ant tasks you will probably need to use are:

Exec - You will need this to execute your git command that gets your version info. It has an argument called resultProperty you can use to store the output of the command into a property that ant can access. (or you can just have the command output to a file and ant can access that.)

ReplaceRegExp - You will need this to replace tokens (maybe @VERSIONCODE@ and @VERSIONNAME@ ) you place in your AndroidManifest.xml where the values should eventually go, with the values returned by the exec.

You will probably also want to execute an exec task at the beginning to restore your AndroidManifest.xml file to it's original state (with the tokens in place) so it's repeatable without manual cleanup. I would provide more info on the git commands you need to run within these exec tasks, but I'm afraid all my experience is with Subversion, so you'll have to fill in the gaps there.

1
  • Thanks +1. I now need to find some time to test this solution... I will accept when I get it working. Oct 12, 2012 at 14:41
2

You should consider building with maven-android. Once you have your project building cleanly, use the version-update plugin to automatically increment your version number.

Writing a build script with maven-android can't be described as easy - but the payoff is worth the effort and you should consider this avenue.

Also, this tutorial might come in handy (I use a variant of the technique described here for my own builds)

EDIT (2014):

Consider migrating to Android Studio and using Gradle. See: How to autoincrement versionCode in Android Gradle

3
  • +1 for your answer and links but I tend to stay away from tools that are not officially supported as the mainstream encouraged endorsed and shoved upon us developers by Google. I will consider maven-android if I find no other way that keeps my current Eclipse+ADT setup. Oct 12, 2012 at 14:39
  • Thank you, I am using Maven and Jenkins to automate my builds for Android applications and this post pointed me in the right direction for the automatic versioning
    – dleerob
    Nov 25, 2014 at 14:00
  • You're welcome. My answer was probably valid in 2012. But this is 2014 and you might want to consider migrating to Android Studio and Gradle. Stuff like version increments baked into Gradle and really easy to use.
    – Jay Sidri
    Nov 25, 2014 at 23:20
1

The way I managed to achieve this: build > execute shell (we needed some php code to receive some info from a db) and the string replacement happens in php:

#!/usr/bin/php
<?php
    $filename = $WORKSPACE."/src/com/me/myapp/MyActivity.java";
    $file = @file_get_contents($filename);
    if($file) {
        $repl = preg_replace("OriginalString", "NewString", $file);
        file_put_contents($filename, $repl);
        echo "\n\nReplaced some stuff in $filename";
    }
?>
2
  • My guess is that you have shown PHP example, which is not used for building android apps
    – Wand Maker
    Dec 18, 2013 at 6:01
  • 2
    The tool you use shouldn't matter if it gets the job done. Yours is a possible solution among many others. Upvoted.
    – Jay Sidri
    May 10, 2014 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.