2011-03-03 38 views
1

有没有什么方法可以在构建过程中为Android清单中的versionCode生成值?问题是,由于AndroidManifest.xml是版本控制的,所以不得修改。我更喜欢在用eclipse构建时也可以设置它。如何在构建过程中将版本设置为Android清单

我想根据版本控制系统的信息生成它。无法使用关键字扩展,因为它需要从整个树的信息中导出,而不仅仅是AndrdoidManifest.xml文件。

我可以很容易地处理versionName。由于它可以引用资源,并且由于res/values中可以有任意数量的.xml文件,因此可以使用适当的值编写一个非版本化的res/values/version.xml。但它看起来不像是应该与versionCode一样的技巧(如果我尝试它直接安装,但我没有通过市场设置测试安装的环境)。

更新:main_rules.xml,它定义了与ant建立的规则使用version.code财产,因此有可能在那里。这个问题仍然代表着使用Eclipse进行构建。

回答

0

我现在建立使用CMake的(应用程序主要是本地和多平台)应用程序,生成所有Android项目包括构建过程中AndroidManifest.xmlbuild.xml的,这样我就可以插入任何我想要在那里。

1

这不会解决你的问题version.code一部分,但这里是我是如何实现它在我们的项目中:

我创建了一个C#的工具,收集,我想我们要包括信息页。该工具在我们项目的构建器列表(Properties->Builders)的第一步中调用,您可以在其中指定工具和命令行参数。在我们的例子建设者信息如下:

地点:C:\Projects\Mobile\Tools\AndroidBuildVersioner\AndroidBuildVersioner\bin\Release\AndroidBuildVersioner.exe

参数: -f C:\Projects\Mobile\Android\ProjectName\res\values\build_version.xml

该工具使用一个Perforce lib和然后检查项目负责修订和相关的库在我的情况将此信息写入可写文件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 - 这个简化的只是增加一个本地版本号,并将其写入和一些额外的ENV信息回到字符串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); 
    } 
+0

是的,除了我从'jni/Application.mk'(我们的大多数应用程序是C++)调用它之外,它和我最终做的非常相似。对于清单,事实证明,基于ant的构建器实际上可以替换版本。代码,但是我最终做了一个临时副本,并代之以各种各样的东西,因为我需要更多的更改,而不仅仅是'version.code'和自定义资源。 – 2011-07-26 05:39:58

相关问题