1

我正在尝试使用 Ant 构建文件自动更新 Android Manifest。代码本身似乎可以工作(手动运行时),但从 Eclipse 运行项目时,我似乎无法让它完全工作。预构建部分有效,问题是清理(构建后)。我正在尝试在构建之后撤消所有内容,以便在 repo 中没有任何更改的文件:

<project name="manifest-versioning" default="pre-build" >

<target name="pre-build" depends="get-repository-info,add-manifest-version">

    <echo>Android Manifest was updated</echo>

</target>

<target name="get-repository-info">

    <echo>Get the total number of git commits</echo>
    <exec executable="sh" outputproperty="git.commits">
        <arg value="-c" />
        <arg value="git log --pretty=format:&apos;&apos; | wc -l" />
    </exec>
    <echo>git.commits: ${git.commits}</echo>

    <echo>Get the version from the git release branch</echo>
    <exec executable="sh" outputproperty="git.branchversion">
        <arg value="-c" />
        <arg value="git rev-parse --abbrev-ref HEAD  | cut -d &apos;-&apos; -f2" />
    </exec>
    <echo>git.branchversion: ${git.branchversion}</echo>

    <echo>Get the number of git release branch commits</echo>
    <exec executable="sh" outputproperty="git.branchcommits">
        <arg value="-c" />
        <arg value="git log master..release-${git.branchversion} --oneline | wc -l" />
    </exec>
    <echo>git.branchcommits: ${git.branchcommits}</echo>

    <echo>Get the last known subversion revision number from git svn</echo>
    <exec executable="sh" outputproperty="git.svnrevision">
        <arg value="-c" />
        <arg value="git svn log --oneline -1 | cut -d &apos; &apos; -f1 | cut -d &apos;r&apos; -f2" />
    </exec>
    <echo>git.svn.revision: ${git.svnrevision}</echo>

    <!-- create a temporary property file for later use -->
    <propertyfile file="git.properties" comment="Git properties">
        <entry key="commits" type="int" value="${git.commits}" />
        <entry key="branchversion" value="${git.branchversion}" />
        <entry key="branchcommits" type="int" value="${git.branchcommits}" />
        <entry key="svnrevision" type="int" value="${git.svnrevision}" />
    </propertyfile>

</target>

<target name="add-manifest-version" depends="get-repository-info">

    <echo>Creating backup of AndroidManifest.xml</echo>
    <copy file="AndroidManifest.xml"
        tofile="AndroidManifest.xml.antbak"
        preservelastmodified="false" /> <!-- true copies postcopy changes as well -->

    <echo>Adding version numbers to AndroidManifest.xml</echo>
    <replaceregexp
        file="AndroidManifest.xml"
        match="android:versionCode=&quot;(\d+)&quot;"
        replace="android:versionCode=&quot;${git.svnrevision}&quot;" />
    <replaceregexp
        file="AndroidManifest.xml"
        match="android:versionName=&quot;(\d+\.\d+)\.\d+&quot;"
        replace="android:versionName=&quot;${git.branchversion}.${git.branchcommits}&quot;" />

</target>

<target name="post-build" depends="restore-manifest,create-versioned-apk">

    <echo>Android Manifest was restored</echo>

</target>

<target name="restore-manifest">

    <echo>Restoring backup of AndroidManifest.xml</echo>
    <move file="AndroidManifest.xml.antbak"
      tofile="AndroidManifest.xml"
      preservelastmodified="false"
      overwrite="true" />

</target>

<target name="create-versioned-apk" depends="restore-manifest">

    <property name="out.final.file" value="bin/Zorgdashboard.apk" />
    <property prefix="git" file="git.properties"/>
    <property name="suffix" value="${git.branchversion}.${git.branchcommits}.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>

    <delete file="git.properties" />

</target>

</project>

我猜我在设定目标时做错了什么。我还尝试将恢复部分放在单独的文件中,但无济于事。这甚至可能吗?

4

0 回答 0