【问题标题】:How to Automatically Modify versionName in Manifest during Build?如何在构建期间自动修改 Manifest 中的 versionName?
【发布时间】:2014-11-26 07:22:38
【问题描述】:

到目前为止,我一直专注于我的应用程序的编程,很少关注使构建过程更智能。因此,我一直在手动操作(“笨方法”),包括在 AndroidManifest.xml 中手动更新android:versionCodeandroid:versionName

我现在想自动(即在构建或导出时):

  1. git 获取包含构建和版本代码的最新标签/分支。
  2. 解析它们,以便我可以将它们分配给 AndroidManifest.xml 中的相应字段。
  3. 相应修改AndroidManifest.xml。
  4. 继续正常的构建过程(Eclipse+ADT,没有任何 Ant),就好像我手动完成了 1-2-3...

我发现了一些关于“pre-build step”、builders 和 build.xml 的线索,但我不知道在哪里可以找到这些线索以及从哪里开始。

关于我在哪里可以找到有关该主题的更多信息的任何提示或指示? (一个循序渐进的教程是理想的)

更新 1: 我发现 this thread 暗示我:

  1. 右键单击项目,Properties > Builders
  2. 添加一个指向项目的 Ant 构建文件的构建器。
  3. 在 Java 构建器之前调用该构建器

很好,但是项目的Ant 构建文件在哪里?我在哪里可以找到它?

更新 2: 显然,export the entire project into an Ant file 是可能的。但我不确定那是我想要的。预构建步骤是否必须始终包含 Ant build file

更新 3: 构建 Ant 文件 only for the pre-build step 是正确的方法吗?

【问题讨论】:

  • 很好...我也期待答案..
  • 如果使用 Maven,请查看 manifestVersionCode 和 manifestVersionName here

标签: android eclipse adt


【解决方案1】:

这是我用来将 versionCode 和 versionName 动态分配给 AndroidManifest.xml 的方法。它仅在使用 ant 构建时才有效,因此您必须先安装它。然后在命令行中进入项目目录并执行“android update project -p.”,这将创建使用ant 构建所需的文件,例如local.properties 和build.xml。

然后打开 build.xml 并将其放入:

  <target name="-pre-build" depends="-custom-git-version,-custom-manifest-version">
  </target>

  <!-- Packages the application. -->
  <target name="-post-build">
    <antcall target="-custom-restore-manifest"/>

    <property name="suffix" value="${git.commits}-${git.version}.apk" />

    <exec executable="sed" inputstring="${out.final.file}" outputproperty="out.final.renamedfile">
      <arg value="s/\.apk/-${suffix}/" />
    </exec>

    <copy file="${out.final.file}" tofile="${out.final.renamedfile}" />
    <echo>Final file copied to: ${out.final.renamedfile}</echo>
  </target>  

  <!-- Custom targets -->
  <target name="-custom-git-version">
    <exec executable="sh" outputproperty="git.commits">
      <arg value="-c" />
      <arg value="git log --pretty=format:'' | wc -l" />
    </exec>
    <echo>git.commits: ${git.commits}</echo>
    <exec executable="git" outputproperty="git.version">
       <arg value="describe" />
       <arg value="--tags" />
       <arg value="--long" />
    </exec>
    <echo>git.version: ${git.version}</echo>
  </target>

  <target name="-custom-manifest-version">
     <echo>Creating backup of AndroidManifest.xml</echo>
     <copy file="AndroidManifest.xml" tofile="AndroidManifest.xml.antbak" preservelastmodified="true" />

     <replaceregexp
        file="AndroidManifest.xml"
        match='android:versionCode="(\d+)"'
        replace='android:versionCode="${git.commits}"' />

     <replaceregexp
        file="AndroidManifest.xml"
    match='android:versionName="(\d+\.\d+)\.\d+"'
    replace='android:versionName="\1.${git.commits}"' />
  </target>

  <target name="-custom-restore-manifest">
    <echo>Restoring backup of AndroidManifest.xml</echo>
    <move file="AndroidManifest.xml.antbak"
          tofile="AndroidManifest.xml"
          preservelastmodified="true"
          overwrite="true" />
  </target>

这个输出并不完全是你想要的,但它是一个开始 - 随意修改它:) 结果类似于“yourapp--.apk

使用它,您将根据您的需要执行“ant clean debug”或“ant clean release”来构建您的应用程序。您还可以使用以下内容创建“ant.properties”文件:

key.store=keystore_file
key.store.password=some_password
key.alias=some_alias
key.alias.password=some_other_password

启用应用的自动签名。

您还应该阅读以下内容:http://developer.android.com/tools/building/building-cmdline.html

【讨论】:

  • 谢谢+1。我现在需要一些时间来测试这个解决方案......当我得到它的工作时我会接受。
  • 感谢您的代码。它也适用于我的情况,并为我的目的进行了一些小的修改。
  • 这是一个很好的答案,我希望我能投票更多次
【解决方案2】:

您在设置预构建步骤方面走在了正确的轨道上,但 ant 构建文件是您自己从头开始创建的。 Eclipse 有一些它在外部使用的 ant 脚本来处理自动编译、打包和其他内容,但是您想创建一个单独的脚本来执行您想要的这些额外步骤。

因此,您将不得不学习一些关于 ant 脚本的知识才能完成这项工作。您找到的其中一些链接为您提供了有关如何创建简单 ant 文件的基本概念。除此之外,您可能需要使用的一些 ant 任务是:

Exec - 你需要这个来执行你的 git 命令来获取你的版本信息。它有一个名为 resultProperty 的参数,您可以使用该参数将命令的输出存储到 ant 可以访问的属性中。 (或者您可以将命令输出到一个文件中,然后 ant 可以访问该文件。)

ReplaceRegExp - 您将需要它来替换您放置在 AndroidManifest.xml 中的令牌(可能是 @VERSIONCODE@ 和 @VERSIONNAME@ ),这些值最终应该被执行,并由 exec 返回值。

您可能还希望在开始时执行一个 exec 任务,以将您的 AndroidManifest.xml 文件恢复到其原始状态(带有适当的标记),以便无需手动清理即可重复。我会提供更多关于你需要在这些 exec 任务中运行的 git 命令的信息,但恐怕我所有的经验都是使用 Subversion,所以你必须填补那里的空白。

【讨论】:

  • 谢谢+1。我现在需要一些时间来测试这个解决方案......当我得到它的工作时我会接受。
【解决方案3】:

您应该考虑使用maven-android 构建。干净地构建项目后,使用version-update 插件自动增加版本号。

使用 maven-android 编写构建脚本并不容易 - 但付出的努力是值得的,您应该考虑这个途径。

另外,这个tutorial 可能会派上用场(我将这里描述的技术的变体用于我自己的构建)

编辑(2014 年):

考虑迁移到 Android Studio 并使用 Gradle。见:How to autoincrement versionCode in Android Gradle

【讨论】:

  • +1 以获得您的答案和链接,但我倾向于远离不受官方支持的工具,因为主流鼓励支持并被 Google 推向我们的开发人员。如果没有其他方法可以保持当前的 Eclipse+ADT 设置,我会考虑使用 maven-android。
  • 谢谢,我正在使用 Maven 和 Jenkins 来自动构建 Android 应用程序,这篇文章为我指明了自动版本控制的正确方向
  • 不客气。我的回答可能在 2012 年有效。但这是 2014 年,您可能要考虑迁移到 Android Studio 和 Gradle。诸如版本增量之类的东西烘焙到 Gradle 中并且非常易于使用。
【解决方案4】:

我设法实现这一点的方式:构建 > 执行 shell(我们需要一些 php 代码来从数据库接收一些信息)并且字符串替换发生在 php 中:

#!/usr/bin/php
<?php
    $filename = $WORKSPACE."/src/com/me/myapp/MyActivity.java";
    $file = @file_get_contents($filename);
    if($file) {
        $repl = preg_replace("OriginalString", "NewString", $file);
        file_put_contents($filename, $repl);
        echo "\n\nReplaced some stuff in $filename";
    }
?>

【讨论】:

  • 我猜你已经展示了 PHP 示例,它不用于构建 android 应用程序
  • 您使用的工具是否能完成工作并不重要。您的解决方案是许多其他解决方案中的一个可能。赞成。
猜你喜欢
  • 2012-03-26
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多