这并没有解决您问题中的version.code 部分,但这是我在我们的项目中完成它的方式:
我创建了一个 C# 工具,用于收集我想包含在我们的 about 页面中的信息。在我们项目的构建器列表(Properties->Builders) 的第一步中调用此工具,您可以在其中指定工具和命令行参数。在我们的实例中,构建器信息如下所示:
位置:C:\Projects\Mobile\Tools\AndroidBuildVersioner\AndroidBuildVersioner\bin\Release\AndroidBuildVersioner.exe
参数:
-f C:\Projects\Mobile\Android\ProjectName\res\values\build_version.xml
在我的例子中,该工具使用 perforce 库检查项目的头版本和关联的库,然后将此信息写入可写文件build_version.xml。该文件在工作区中仍然是可写的,因为它是一个自动生成的文件。输出只是一组字符串资源,因此项目的关于活动的信息很容易获得。我最初也是在写清单,但是当构建工具修改你也必须手动修改的文件时,当然很容易遇到冲突。
如果有帮助,这里有几个例子:
build_version.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="build_revision">9</string>
<string name="build_date">7/25/2011 9:48:13 AM</string>
<string name="build_machine">JMARTIN-PC</string>
<string name="build_user">jmartin</string>
</resources>
AndroidBuildVersioner.exe - 这个简化的块只是增加一个本地版本号并将它和一些额外的环境信息写回字符串 xml 文件。
private static void updateBuildFileInfo(String file)
{
String fileContents = ReadFile(file);
String buildRevision = "0";
String newFileContents = @"<?xml version=""1.0"" encoding=""utf-8""?>"
+ Environment.NewLine + "<resources>" + Environment.NewLine ;
//find the build version in the contents of the file so it can be incremented
Match m = Regex.Match(fileContents, @"<string name=""build_revision"">(.*)</string>");
if (m.Success)
buildRevision = m.Groups[1].Value;
int intBuildRevision = 0;
try
{
intBuildRevision = Convert.ToInt32(buildRevision);
}
catch (FormatException e)
{
Console.WriteLine("Input string is not a sequence of digits.");
}
catch (OverflowException e)
{
Console.WriteLine("The number cannot fit in an Int32.");
}
finally
{
++intBuildRevision;
newFileContents += '\t' + @"<string name=""build_revision"">" + intBuildRevision + "</string>" + Environment.NewLine;
newFileContents += '\t' + @"<string name=""build_date"">" + DateTime.Now.ToString() + "</string>" + Environment.NewLine;
newFileContents += '\t' + @"<string name=""build_machine"">" + Environment.MachineName + "</string>" + Environment.NewLine;
newFileContents += '\t' + @"<string name=""build_user"">" + Environment.UserName + "</string>" + Environment.NewLine;
newFileContents += "</resources>";
}
writeOutput(file, newFileContents);
}