Get started with Ant to build an Android development environment

Source: Internet
Author: User

Get started with Ant to build an Android development environment

Build an Android project using Ant to build an Android development environment

Configuring the ANT Environment

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

On Windows, you should select the ZIP package and unzip the zip archive into a directory.

Open the System environment variable, click New on the System variable column, enter the variable name "Ant_home", the variable value is ANT's root directory, such as "D:\Android\apache-ant-1.9.0", take care not to enclose double quotation marks.

Locate the path variable in the system variable, click Edit, add "%ant_home%\bin" at the end of the variable value, take care not to enclose the double quotation mark, and use ";" Separated from the previous variable values.

Open a CMD window, enter "Ant", if the message shows that the environment configuration of ant is successful, if: ' Ant ' is not an internal or external command, it is not a running program or a batch file. Check to see if there is a problem with the path.

Configuring the Ant in Eclipse

Before using ant in Eclipse, for the ant build.xml file to be able to install the formatted format for indentation and highlighting, and to be able to code hints, first of all, a simple set up.

Open Windows-preferences, expand General,editors, select File Associations, click Add ..., enter Build.xml in the Add File Type dialog box, click OK.

Next in the File type: column Check Build.xml, in the associated editor: bar check ant editor, click the Default,build.xml icon to become a small ant, configuration completed.

Compiling Java code and native code for Android using Ant

Create a new Android project Testant, create a new Build.xml file in the project's root directory

Enter the following:

<?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 Project root, enter Ant Init

Compile successfully, to parse this build.xml:

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

Project is the root node of the ant project, and the Name property is the project names, and default is the default target for Target,init, which is the same as Ant init when we enter Ant.

    <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 action to perform, Init is the name we have for this target, it can be build,clean, and so on. In this target, you are doing a check of the ant version, such as a less than 1.7.0 will output an error message.

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>

The action is to delete all temporary directories, enter ant clean in the cmd window, the four temporary directories will be deleted, and cleanup in Eclipse is an effect.

Compiling Android projects with Ant

In the SDK, Google has written for us a build.xml file, which is the SDK root directory \tools\ant\build.xml, so we just have to introduce this build.xml to compile Android project.

Before you do this, start by creating a new Local.properties file that introduces the SDK and NDK paths

Enter the content:

Sdk.dir=d:\\android\\android-sdkndk.dir=d:\\android\\android-ndk

The path for the SDK and NDK, respectively, to install your own actual path to configure.

Enter the code in the Build.xml of the test project:

<?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.d IR is missing. Make sure to generate Local.properties using ' Android Update Project ' "unless=" Ndk.dir "/> <import file=    " ${SDK. Dir}/tools/ant/build.xml "/></project>

When you execute ant release or ant debug on the command line, the corresponding compilation is performed.

The above is just to compile Java code, how to compile native code, in the NDK Google can not mention enough build.xml, which requires our own implementation. In the new NDK, we just switch to the root of the project at the command line and then execute

"D:\Android\android-ndk\ndk-build.cmd" (the red part to replace its own path)

You can, for example, add native code to the Testant project, refer to Http://www.cnblogs.com/sw926/p/3232311.html

Executive Ndk-build

So we just have to execute a cmd command in Ant and add it to the 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 execute the ant native on the command line

Executed the Ndk-build.

All of these are Android compilation operations that use Ant to make progress, which allows for more complex operations such as bulk substitution of resource files, bulk packaging, and so on.

The Build.xml reference FBReader.

[Collection] [ copy link to friend] [ print] [ close]

Get started with Ant to build an Android development environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.