Use Ant 1 in Android development: Environment setup and getting started

Source: Internet
Author: User

Configure the Ant Environment

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

On windows, you should select a zip package to decompress the zip package to a directory.

Open the system environment variable, and click New in the system variables column. Enter "ANT_HOME" for the variable name and set the variable value to the Ant root directory, for example, "D: \ Android \ apache-ant-1.9.0 ". do not include double quotation marks.

Find the Path variable in the system variable, click Edit, and add "% ANT_HOME % \ bin" at the end of the variable value. Be sure not to include double quotation marks and use "; "is separated from the previous variable values.

Open a cmd window and enter "ant". If the information is displayed, the Ant environment is configured successfully. If 'ant' is not an internal or external command, it is not a running program or batch processing file. Check whether the path is correct.

Configure Ant in Eclipse

Before using Ant in eclipse, to enable the Ant build. xml file to install the specified format for indent and highlighting, and to prompt code, you must first set it easily.

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

Next, select build. xml in File type: bar, select Ant Editor in Associated Editor: bar, and click Default. The build. xml icon becomes a small Ant, And the configuration is complete.

Use Ant to compile Android java code and native code

Create an Android project TestAnt and create a build. xml file under the root directory of the project.

Enter the following content:

                                                                                                             

Open cmd, switch to the project root directory, and enter ant init

After compilation is successful, parse the build. xml:

 

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

                                                                                                     

Target specifies the operation to be executed. init is the name we give for this target, it can also be build, clean, and so on. In this target, the Ant version is checked. If the Ant version is smaller than 1.7.0, an error is returned.

For example, we can add a clean target

                                                              

The operation is to delete all the temporary directories. Enter Ant clean in the cmd window, and the four temporary directories will be deleted. This is the same effect as cleaning in eclipse.

Use Ant to compile Android Projects

In the SDK, Google has prepared a build for us. the xml file is the sdk root directory \ tools \ ant \ build. xml, so we only need to build this. you can compile the Android project by introducing xml.

Before that, create a local. properties file and introduce the sdk and ndk paths.

Enter the following content:

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

The path is the sdk and ndk respectively, and you need to install your own actual path for configuration.

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

                         

Execute ant release or ant debug in the command line to execute the corresponding compilation.

The above is just the compilation of Java code. How to compile native code? In NDK, Google does not mention build. xml, which needs to be implemented by ourselves. In the new NDK version, we only need to switch to the root directory of the project in the command line, and then execute

\ Ndk-build.cmd (red part should be replaced with your own path)

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

Execute ndk-build

So we only need to execute a cmd command in Ant, and add

                                   

Then execute ant native in the command line.

Ndk-build is executed.

The above sections describe how to use Ant to perform progressive Android compilation operations. On this basis, more complex operations can be implemented, such as replacing resource files in batches and packaging in batches.

For build. xml, see FBReader.

 

 

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.