一种可能的方法。
在您的 AndroidManifest.xml 中将元数据添加到您的活动中以保留修订版或您想要使用的任何内容
<activity android:name="SampleActivity">
<meta-data android:value="$Rev: 164 $" android:name="com.example.version" />
</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();
}
您可能还想在清单中设置android:versionName="$Rev$"。
要自动化增加android:versionCode 中的版本号并提交AndroidManifest.xml 的过程,有多种选择,使用maven 或ant 构建可能是替代方案,但让我们使用Eclipse 和ADT 来实现。
首先,将构建器添加到您的项目中(我的项目 -> 属性 -> 构建器)
此构建器应使用项目的AndroidManifest.xml 作为参数调用android-increment-manifest-version
android-increment-manifest-version 脚本类似于
#! /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