一种可能的方法。
在您的 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
or ant
可能是替代方案,但让我们使用 Eclipse 和 ADT 来实现。
首先,将构建器添加到您的项目中(我的项目 -> 属性 -> 构建器)
这个构建器应该调用android-increment-manifest-version
使用项目的AndroidManifest.xml
作为论点
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