0

I am trying to use custom build file in order to display the current git version in my Android application. I haven't used ant before and I have not really an idea how to use it. I read plenty of topics in SO and searched quite a lot in Google but I cannot figure it out. I don't really have the time to learn everything about ant but I need this thing running. At the bottom, you can find the code.

Current status

The file custom_rules.xml is imported in the build.xml created by Eclipse. The macrodef part is invoked but the targets not. I tried to change the External Tools Configurations, tab Targets but whenever I check a target (no matter in which ant file), I get a message:

Unknown argument: -pre-build

for example (when I put checkmark on -pre-build). I tried adding this line:

<import file="${sdk.dir}/tools/ant/build.xml" />

and defining sdk.dir but that doesn't change anything. What am I missing? As I said, I have no idea about ant and the only tutorial that helped me was this one.

Current code (custom_rules.xml)

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse.ant.import ?>
<project name="Custom build">

    <macrodef name="git" taskname="@{taskname}">
        <attribute name="command" />
        <attribute name="dir" default="" />
        <attribute name="property" default="" />
        <attribute name="taskname" default="" />
        <attribute name="failonerror" default="on" />
        <element name="args" optional="true" />
        <sequential>
            <exec executable="git" dir="@{dir}" outputproperty="@{property}" 
                failifexecutionfails="@{failonerror}" failonerror="@{failonerror}">
                <arg value="@{command}" />
                <args/>
            </exec>
        </sequential>
    </macrodef>

    <target name="-pre-build">
        <git command="rev-list" property="versioning.code" taskname="versioning">
            <args>
                <arg value="master" />
                <arg value="--first-parent" />
                <arg value="--count" />
            </args>
        </git>
        <git command="describe" property="versioning.name" taskname="versioning">
            <args>
                <arg value="--always" />
            </args>
        </git>
        <echo level="info" taskname="versioning">${versioning.code}, ${versioning.name}</echo>
        <replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="${versioning.code}"' />
        <replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="${versioning.name}"' />
    </target>

    <target name="-post-build" >
        <replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="0"' />
        <replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="0"' />
    </target>

</project>
Antiohia
  • 1,142
  • 13
  • 37

2 Answers2

0

I just figured it out.

  • I renamed the targets to pre-build and post-build without -.
  • I went to External Tools Configurations and selected build.xml to the left. On the right, I went to the tab Targets, checked my two targets (in addition to the build target which already had a checkmark) and set the order to be pre-build, build, post-build.
  • I went to the project properties and I selected Builders on the left. I created a new builder using the build.xml file and having the three targets from the previous bullet point, in the same order. I placed this builder before the Java builder.
  • I removed the post-build target as it puts the version back to 0 and seems to do that earlier than I would like.

I am still not sure that this is the optimal solution. Also, the solution fails when there is no git versioning. I tried using this code for solving the issue but it didn't worked. Nevertheless, it is the best I could do and it helps me getting the info I need in the app.

Community
  • 1
  • 1
Antiohia
  • 1,142
  • 13
  • 37
-1

Use Git executable with Argument --version and catch the output in a property for further usage, f.e. :

<project> 

<exec  executable="git" outputproperty="gitversion">
 <arg value="--version"/>
</exec>

<echo>$${gitversion} => ${gitversion}</echo>
</project>

output :

[echo] ${gitversion} => git version 1.8.3.msysgit.0
Rebse
  • 10,307
  • 2
  • 38
  • 66
  • I want to have the git version of my project and my code already contains the correct git command. The problem is making the ant running and not the git command. – Antiohia Jun 04 '14 at 08:53