ant 加快eclipse下android项目编译速度

本文主要介绍通过ant和一些eclipse配置加快eclipse下android项目编译速度。

主要包括两部分ant提高效率eclipse配置项提高效率。关于eclipse中android项目的编译过程分析见上一篇博客。

Ps:本文中编译过程输出都是以真机(htc g7)作为avd,api level为8。

用eclipse进行android开发的朋友都会发现,无论是java文件还是一个资源文件的修改,在build的时候都会有一个重新编译的过程,而这个过程及其缓慢,导致项目开发效率极其低下。

1、ant配置提高效率

这个部分主要介绍ant可以为android开发做什么,如何生成ant项目,为已存在的项目生成ant的build xml,如何利用ant生成配置。

相应java开发对ant都有一定熟悉,关于下载安装、配置及使用不多介绍,具体可见apache ant

1.1 ant可以为android开发做什么

a. 添加自定义生成步骤

b. 使用自动生成系统

c. 使用生成配置

d. 一个命令build项目

1.2 生成ant项目

使用如下命令

android create project --name YourProjectName --path C:\dev\YourProject --target android-3 --package com.company.testproject --activity MainActivity

相当于eclipse -> new -> other -> android project,参数相当于dialog中需要填写的信息

1.3 为已存在的项目生成ant的build xml

运行命令行到当前项目根目录下,运行如下命令:

android update project --path .

结果如下:

Updated local.properties
Added file C:\dev\blog\antbuild\build.xml

表示增加成功,可以使用ant命令查看相关信息和帮助,如下:

>ant
Buildfile: C:\dev\blog\antbuild\build.xml
    [setup] Android SDK Tools Revision 6
    [setup] Project Target: Android 1.5
    [setup] API level: 3
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.
    [setup] Importing rules file: platforms\android-3\ant\ant_rules_r2.xml

help:
     [echo] Android Ant Build. Available targets:
     [echo]    help:      Displays this help.
     [echo]    clean:     Removes output files created by other targets.
     [echo]    compile:   Compiles project's .java files into .class files.
     [echo]    debug:     Builds the application and signs it with a debug key.
     [echo]    release:   Builds the application. The generated apk file must be
     [echo]               signed before it is published.
     [echo]    install:   Installs/reinstalls the debug package onto a running
     [echo]               emulator or device.
     [echo]               If the application was previously installed, the
     [echo]               signatures must match.
     [echo]    uninstall: Uninstalls the application from a running emulator or
     [echo]               device.

BUILD SUCCESSFUL
 

1.4 如何利用ant生成配置

项目中经常需要对一些字符串或是日志级别进行设置,这里我们可以通过ant的配置项完成

新建build.properties文件,内容为

# Turn on or off logging.
config.logging=true

调用地方如下使用

public class Config {
    /** Whether or not to include logging statements in the application. */
    public final static boolean LOGGING = @CONFIG.LOGGING@;
}  

需要在build.xml中添加如下部分

<!-- Copy Config.java to our source tree, replacing custom tokens with values in build.properties. The configuration depends on "clean" because otherwise the build system will not detect changes in the configuration. -->
  <target name="config">

	  <property name="config-target-path" value="${source.dir}/com/androidengineer/antbuild"/>

	  <!-- Copy the configuration file, replacing tokens in the file. -->
	  <copy file="config/Config.java" todir="${config-target-path}"
			overwrite="true" encoding="utf-8">
	   <filterset>
		<filter token="CONFIG.LOGGING" value="${config.logging}"/>
	   </filterset>
	  </copy>
	  
	  <!-- Now set it to read-only, as we don't want people accidentally
		   editing the wrong one. NOTE: This step is unnecessary, but I do
		   it so the developers remember that this is not the original file. -->
	  <chmod file="${config-target-path}/Config.java" perm="-w"/>
	  <attrib file="${config-target-path}/Config.java" readonly="true"/>

 </target>
具体可以参考 http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html

2、eclipse配置项提高效率

右击项目选择Properties -> Builders,取消勾选所有的Builders

原因尚不明,但很有效果,修改文件后保存,编译极快。

3、修改sdk依赖版本,据悉在api level 8之前编译速度良好。

未降级api进行测试

参考:

http://oae9.wordpress.com/2011/03/22/android-workaround-for-slow-building-workspace-problem-in-eclipse/

相关推荐