In the development of Android Ant: set up and entry environment

Configure the Ant environment

Download Ant: http://ant.apache.org/bindownload.cgi

In the windows should be zip compressed package, the zip decompression of compressed packets to a directory.

Open system environment variables, new variables in the system bar click, variable name input “ ANT_HOME”, variable values to the root directory, such as Ant “ D:\Android\apache-ant-1.9.0”, pay attention not to take double quotation marks.

Find the Path variable in the system variables, click Edit, in the variable value added at the end of “%ANT_HOME%\bin”, pay attention not to take double quotation marks, and to use “; ” and before the variables separated.

Open a CMD window, enter the “ ant”, if show information, description of Ant environment configuration, if the display:'ant'not internal or external command, also is not operable program or batch file. To check the path if there is a problem.

The configuration of Ant in Eclipse

Before using the Ant in eclipse, in order to make the Ant the build.xml file can be installed are formulated in the format of indentation and highlighted, and makes the code tips, first of all to simply set the.

Open the Windows - Preferences, expand General, Editors, select File Associations, click the Add... Add File Type, enter build.xml in the dialog box, click Ok.

In the File type: select the build.xml in Associated Editor column, Ant Editor column: selected, click on the Default, the build.xml icon into a small ant, configuration.

Java code and native code using the Ant compiler Android

Create a new Android project TestAnt, a new build.xml file in the root directory of the project

Enter the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<project name="TestAnt" default="init">
    
    <target name="init">
        <fail message="Ant 1.7.0 or higher is required.">
            <condition>
                <not>
                    <antversion property="ant.version" atleast="1.7.0" />
                </not>
            </condition>
        </fail>
    </target>
    
</project>

Open CMD, switch to the project's root directory, type ant init

Compile successfully, to parse this build.xml:

<project name="TestAnt" default="init">

Project is the root node of the Ant project, the name attribute is the name of the project, default is the default implementation of the target, init as the default target, when we input when the Ant and Ant init is the same effect.

    <target name="init">
        <fail message="Ant 1.7.0 or higher is required.">
            <condition>
                <not>
                    <antversion property="ant.version" atleast="1.7.0" />
                </not>
            </condition>
        </fail>
    </target>

Target specifies the operation to be performed, init is what we play for the name of this target, can also be build, clean and so on. In this target, is the implementation of the Ant version, such as a less than 1.7.0 words will output error information.

For example, we can add a clean target

    <target name="clean">
        <echo message="Deleting temporary files..." />
        <delete dir="gen" />
        <delete dir="bin" />
        <delete dir="out" />
        <delete dir="obj" />
        <echo message="DONE (Deleting temporary files)" />
    </target>

Implementation of the operation is to delete all the temporary directory, enter the Ant clean in the CMD window, the four temporary directory will be deleted, and in the implementation of eclipse cleaning is an effect.

The use of Ant compiler Android Engineering

In SDK, Google has written a build.xml file to us, is the SDK root directory \tools\ant\build.xml, so we just put the build.xml into the Android project can be compiled.

Before that first of all to a new local.properties file, introduces SDK and NDK

The input to the content:

sdk.dir=D:\\Android\\android-sdk
ndk.dir=D:\\Android\\android-ndk

Path to SDK and NDK respectively, to the actual path to install their own configuration.

Enter the code in the test project in build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="TestAnt" default="release">
    <loadproperties srcFile="local.properties" />
    <loadproperties srcFile="project.properties" />

    <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'" unless="sdk.dir" />
    <fail message="ndk.dir is missing. Make sure to generate local.properties using 'android update project'" unless="ndk.dir" />

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

Implementation of ant release or ant debug at the command line, can execute corresponding compiler.

More than just Java code compiler, how to compile native code, in NDK Google can not carry enough build.xml, this requires our own implementation. In the new version of NDK, as long as we in the command line switch to the root directory of the project, and then perform

“D:\Android\android-ndk\ndk-build.cmd”(red to replace their path)

You can, for example, add the native code in the TestAnt project, reference

Implementation of ndk-build

So long as we in the Ant executes a CMD command can, add in build.xml

    <target name="native">
        <echo message="Building native libraries..." />
        <exec executable="${ndk.dir}/ndk-build.cmd" failonerror="true" />
        <echo message="DONE (Building native libraries)" />
    </target>

Then the implementation of ant native on the command line

The implementation of ndk-build.

This is the use of Ant Android compiler operation progress, on this basis can achieve more complex operations, such as batch replace resource file, batch packing etc.

The build.xml reference FBReader.

Posted by Bowen at November 18, 2013 - 4:48 PM