如何在Build期间自动修改Manifest中的versionName?

Ete*_*ner 25 eclipse android adt

到目前为止,我一直专注于我的应用程序的编程,并且很少关注使构建过程更加智能.因此,我一直在手工操作("愚蠢的方式"),包括手动更新android:versionCodeandroid:versionNameAndroidManifest.xml.

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

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

我找到了一些关于" 预构建步骤",构建器和build.xml的线索,但我不知道在哪里可以找到这些以及从哪里开始.

有关该主题的更多信息的任何提示或指示?(一步一步的教程是理想的)

更新1:我发现这个帖子暗示我:

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

很好,但这里是该项目的蚂蚁构建文件?我在哪里找到它?

更新2:显然,可以将整个项目导出到Ant文件中.但我不确定这是我想要的.预构建步骤必须始终包含Ant构建文件吗?

更新3:正在构建一个Ant文件,仅用于预构建步骤,正确的方法吗?

cer*_*.cz 18

这是我用来动态地将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>
Run Code Online (Sandbox Code Playgroud)

这个输出并不是你想要的,但它是一个开始 - 随意修改它:)结果就像"yourapp - .kk

使用它,您将根据您的需要执行" 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
Run Code Online (Sandbox Code Playgroud)

启用自动签名您的应用程序.

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