【问题标题】:How to set version to android manifest during build如何在构建期间将版本设置为 android manifest
【发布时间】:2011-03-03 08:02:51
【问题描述】:

有什么方法可以在构建过程中为 android manifest 中的versionCode 生成值吗?问题是,由于AndroidManifest.xml 是版本化的,所以不能就地修改它。如果在使用 eclipse 构建时也可以设置它,我更愿意。

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

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

更新: main_rules.xml 定义了使用 ant 构建的规则,它使用了 version.code 属性,因此在那里是可能的。问题仍然是用 Eclipse 构建。

【问题讨论】:

    标签: android android-manifest versioning


    【解决方案1】:

    这并没有解决您问题中的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);
        }
    

    【讨论】:

    • 是的,这与我最终所做的非常相似,只是我从jni/Application.mk 调用它(我们的大部分应用程序是 C++)。对于 manifest,事实证明基于 ant 的构建器实际上可以替换 version.code,但我最终做了一个临时副本并在那里替换了各种东西,因为我需要的不仅仅是 version.code 和自定义资源之外的更多更改..
    【解决方案2】:

    我现在使用 CMake 构建应用程序(该应用程序大部分是原生的和多平台的),在构建过程中生成所有的 android 项目,包括 AndroidManifest.xmlbuild.xml,所以我可以在那里插入任何我想要的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-12
      • 2017-12-29
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2019-04-02
      • 2020-07-30
      • 1970-01-01
      相关资源
      最近更新 更多