views:

420

answers:

3

My code is stored in SVN version control. I use Eclipse to build my Android application.

In my application, I have an about-box. I want to show the correct source control revision/tag/whatever in this.

Is there a way of automating this so that my version string in the about-box is always right, or is this something I have to hand-edit each time I commit?

Thx for the early answers about $keywords$.

Setting the SVN property svn:keywords to Rev does expand a private String m_svn_rev = "$Rev:$" every time I submit that file.

SVN is a per-file version control system.

Which leads instead to wonder if I can somehow pre-process some files in the Android build thingy to inject svnversion output?

+1  A: 

generic advice only, you don't mention your version control system.

embed the appropriate header (something like $Version:$) in the text for the about box. When you commit, the new version # will be stored and when you build the value will be shown.

[edit]
1. create your about box as a separate source, only check it in when you increment the version.
2. embedded your own version in a header file (and don't forget to change it) then use that version string in your about box (as an extern string * for example)
[/edit]

KevinDTimm
I think you wanted to add this link, http://svnbook.red-bean.com/nightly/en/svn.advanced.props.special.keywords.html
basszero
no, I didn't :) the OP might have though.
KevinDTimm
+2  A: 

One possible approach. In your AndroidManifest.xml add metadata to your Activities to keep the revision or whatever you want to use

<activity android:name="SampleActivity">
    <meta-data android:value="$Rev: 164 $" android:name="com.example.version" />
</activity>

then in your Activity

try {
    final ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
    final String version = (String)ai.metaData.get("com.example.version");
    System.out.println("version: " + version);
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

You may also want to set android:versionName="$Rev$" in your manifest.

To automate the process of incrementing the version number in android:versionCode and committing the AndroidManifest.xml there are various options, building with maven or ant could be alternatives but let's do it using Eclipse and ADT.

First, add a builder to your project (My Project -> Properties -> Builders)

alt text

this builder should invoke the android-increment-manifest-version using the project's AndroidManifest.xml as the argument

alt text

android-increment-manifest-version script is something like

#! /bin/bash
# android-increment-manifest-version:
# increment the version number found in the AndroidManifest.xml file
# (android:versionCode="n") in place and commit it to subversion.
#
# Copyright (C) 2010 Diego Torres Milano - http://dtmilano.blogspot.com

usage() {
    echo "usage: $PROGNAME AndroidManifest.xml" >&2
    exit 1
}

PROGNAME=$(basename $0)

if [ "$#" -ne 1 ]
then
    usage
fi

MANIFEST="$1"

perl -npi -e 's/^(.*android:versionCode=")(\d+)(".*)$/"$1" . ($2+1) . "$3"/e;' $MANIFEST
svn ci -m "Version incremented" $MANIFEST
dtmilano
Yeap, I'm doing something like this, but it's per-file so it doesn't get updated every time I submit other files :(
Will
What are you using as your build system? If you use maven or ant, you can script it so that it does this every time you build.
Mayra
Hi i tried to use your script to set the value of the tag android:versionName to the current revision. So i set android:versionName="$Rev$" and in your script i changed (.*android:versionCode=") to (.*android:versionName=") but every times the result is just "$Rev$". Do you have any hints?
Roflcoptr
(\d+) is matching digits only.
dtmilano
Roflcoptr, you need to enable Revision in svn:keywords property for that file. See http://svnbook.red-bean.com/en/1.1/ch07s02.html for more details.
Artem Russakovskii
A: 

This may be a tangent, but I use Cruse Control's $label tag to bump the version in my about screen. Each time I check something in, CC does an update and spits out a build. One of the steps in my ant script injects the $label value from the cruise control system into a value within strings.xml. This gives me a new version number with each commit. I can check the CC logs and match a build number up to an SVN revision number.

Probably more work than you're interested in doing for something this simple. I'd also wager that any other build system out there will have a similar feature.

haseman