简体   繁体   English

如何在构建期间将版本设置为Android清单

[英]How to set version to android manifest during build

Is there any way I could generate value for the versionCode in android manifest during build? 有什么办法可以在构建期间为Android清单中的versionCode生成值? The problem is that since AndroidManifest.xml is versioned, it must not be modified in place. 问题在于,由于AndroidManifest.xml是版本控制的,因此不能就地对其进行修改。 I'd prefer if it was possible to set it when building with eclipse too. 我希望在使用Eclipse构建时也可以进行设置。

I want to generate it based on information from version control system. 我想基于版本控制系统中的信息生成它。 It's not possible to use keyword expansion, because it needs to be derived from information about whole tree, not just the AndrdoidManifest.xml file. 不可能使用关键字扩展,因为它需要从整个树的信息中获取,而不仅仅是AndrdoidManifest.xml文件。

I can kind-of easily deal with versionName . 我可以轻松地处理versionName Since it can refer to a resource and since there can be any number of .xml files in res/values, I can write a non-versioned res/values/version.xml with the appropriate value. 由于它可以引用资源,并且在res / values中可以有任意数量的.xml文件,因此我可以编写具有适当值的非版本化的res / values / version.xml。 But it does not look like the same trick should work with versionCode (if I try it it installs directly, but I don't have environment for testing installation via market set up yet). 但这似乎与versionCode不能使用相同的技巧(如果我尝试将其直接安装,但是还没有通过市场设置测试安装的环境)。

Update: The main_rules.xml , which defines the rules for building with ant uses a version.code property, so it's possible there. 更新: main_rules.xml定义了使用ant进行构建的规则,使用了version.code属性,因此可以在此处使用。 The question still stands for building with Eclipse. 问题仍然代表使用Eclipse进行构建。

This doesn't address the version.code part of your questions, but here's how I'm accomplishing it in our project: 这不会解决您的问题的version.code部分,但是这是我在项目中如何完成此操作的方法:

I've created a C# tool that collects info that I want to include in our about page. 我创建了一个C#工具,该工具收集了我想包含在“关于”页面中的信息。 This tool is invoked at the first step in our project's builders list (Properties->Builders) where you can specify the tool and command line args. 在项目的构建器列表(Properties->Builders)第一步中调用此工具,您可以在其中指定工具和命令行参数。 In our instance the builder info looks like this: 在我们的实例中,构建器信息如下所示:

Location: C:\\Projects\\Mobile\\Tools\\AndroidBuildVersioner\\AndroidBuildVersioner\\bin\\Release\\AndroidBuildVersioner.exe 位置: C:\\Projects\\Mobile\\Tools\\AndroidBuildVersioner\\AndroidBuildVersioner\\bin\\Release\\AndroidBuildVersioner.exe

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

The tool examines the head revision of the project and associated libs in my case using a perforce lib and then writes this info tp the writable file build_version.xml . 该工具在我的情况下使用perforce lib检查项目的主修订版和相关的lib,然后将此信息写入可写文件build_version.xml This file remains writable in the workspace since it's an auto generated file. 该文件是自动生成的文件,因此在工作区中仍可写。 The output is just a set of string resources so that the info is easily available to the project's about activity. 输出只是一组字符串资源,因此该信息可轻松用于项目的about活动。 I was also initially writing to the manifest as well, but of course it's easy to run into conflicts when build tools modify files that you also have to modify by hand. 我最初也曾写清单,但是当构建工具修改文件(也必须手动修改)时,很容易陷入冲突。

here are a couple of examples if it helps: 如果有帮助,请举几个例子:

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 - this simplified block just increments a local build number and writes it and some extra env info back to the strings xml file. 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);
    }

现在,我使用CMake构建应用程序(该应用程序主要是本机和多平台的),在构建过程中生成了所有Android项目,包括AndroidManifest.xmlbuild.xml ,因此我可以在其中插入任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM