embed version details in android APK
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 th开发者_开发百科ingy to inject svnversion output?
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)
this builder should invoke the android-increment-manifest-version
using the project's AndroidManifest.xml
as the argument
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
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]
From the idea of dtmilano.blogspot.com... but using only the SVN revision:
#! /bin/bash
# The first argument is full path of AndroidManifest.xml file
usage() {
echo "usage: $PROGNAME AndroidManifest.xml" >&2
exit 1
}
if [ "$#" -ne 1 ]
then
usage
fi
PROGNAME=$(basename $0)
MANIFEST="$1"
SVNREVISION=$(svn info | sed -n '5p' | sed -e 's/.*[^0-9]\([0-9]*\).*/\1/g')
if [ -f $MANIFEST ]; then
sed "s/android:versionCode=\"[0-9]*\"/android:versionCode=\"${SVNREVISION}\"/" $MANIFEST > $MANIFEST.tmp
mv $FULLPATH.tmp $MANIFEST
echo "The android:versionCode properties of file ${MANIFEST} is ${SVNREVISION}";
fi;
#svn commit -m "Update version in $MANIFEST" $MANIFEST
The version number change only when SVN Revision change, ie only when you exec svn commit.
If You want, You can add a check for Changed revision, if so execute the sed for alter file. Is also possible to change Revision to the last SVN revision on server.
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.
精彩评论