Ant學習

這裏介紹了ant入門,自己編寫了build.xml並且執行編譯工作。

下面是build.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<!--project元素。name屬性指定工程的名稱,default屬性  
指定ant默認執行的target,即不指定要執行的target時,ant  
執行的target。basedir指定本工程的基準目錄,這裏指定爲  
當前目錄。-->
<project name="Main" default="compile" basedir="." >
<!--property元素,定義了兩個元素,供後面以${元素名}的形式引用。-->
<property name="targetdir" value="classes" />
<property name="srcdir" value="src" />

<!--定義一個path元素,指定id屬性,供後面引用-->
<path id="library">
	<!--定義一個fileset元素,用於指定需要用到的文件。dir屬性指定哪個目錄下的文件。-->
	<fileset dir="lib">
		<!--指定包含lib目錄下的所有jar包-->
		<include name="*.jar"/>
	</fileset>
</path>


<!--定義一個target元素,name屬性爲必須,用於標識此target。description屬性用於描述此target,無太大意義。depends屬性指定執行此target,所依賴的其他target。這裏指定爲了運行compile這個target,需首先執行clean,copy-resources這兩個target。-->
<target name="compile" description="Compiles the Task" depends="clean">
<!--javac是ant的核心任務(task),用於編譯JAVA源程序。srcdir屬性指定源程序所在的目錄,${srcdir}表明引用上面定義的名爲srcdir的property元素的值。destdir指定編譯後生成的.class文件的輸出目錄。同樣地,這裏使用了${targetdir}的形式引用了上面定義的名爲targetdir的property元素的值。classpathref表明引用上面定義的id爲library的path元素。在這裏,即可把它包含的.jar包加載到classpath中,而不需要像筆者以前那樣自己手動運行setclasspath命令。-->
<javac srcdir="${srcdir}" destdir="${targetdir}" classpathref="library" includeantruntime="no"/>
</target>




<!--定義一個名爲clean的target。-->
<target name="clean">
        <!--delete是ant的一個核心任務,用於刪除目錄或文  
        件。這裏用來刪除classes目錄。即先把以前創建的classes目錄刪除,免得舊文件造成干擾-->
<delete dir="${targetdir}"/>
        <!--mkdir也是一核心任務,用於創建目錄。這裏用來  
        新創建classes目錄-->
<mkdir dir="${targetdir}"/>
</target>




<target name="copy-resources">
        <!--copy也是ant的一個核心任務,用於複製目錄或文  
        件。todir指定複製的目標目錄。本示例未使用此任務-->
<copy todir="${targetdir}">
<!--fileset元素指定要複製的文件集,dir屬性指定複製的源目錄,exclude元素指定排除此目錄下的所有java源文件,即複製除這些文件之外的所有文件-->
<fileset dir="${srcdir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>




<!--depends屬性指定運行此target應先運行名爲compile的target-->
<target name="run" depends="compile">
<!--java是ant的核心任務,用於執行某個java類。classname屬性用於指定要運行的類,
這裏要用到類的全名fork設爲true表明使用另外的JVM來運行我們的JAVA類,而不是使用運行ant的那個JVM。classpathref與上面的javac裏的一樣-->
<java fork="true" classname="com.example.main.Main" classpathref="library">
<!--將classes目錄加載到classpath中-->
<classpath path="${targetdir}"/>
</java>
</target>



</project>

瞭解了標籤之後,就很好理解ant的build.xml了。

這是一個簡單的例子。下面這個例子是安卓使用DexClassLoader來加載代碼並執行的demo。其中也是用到了ant編譯。

build.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright 2011 Google Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<project name="secondary_dex_sample" default="help">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
            unless="sdk.dir"
    />


<!-- extension targets. Uncomment the ones where you want to do custom work
     in between standard targets -->
<!--
    <target name="-pre-build">
    </target>
    <target name="-pre-compile">
    </target>

    /* This is typically used for code obfuscation.
       Compiled code location: ${out.classes.absolute.dir}
       If this is not done in place, override ${out.dex.input.absolute.dir} */
    <target name="-post-compile">
    </target>
-->

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->

    <!-- This is a modified version of the "dex-helper" macro.  It added the "input-dir" and
         "output-dex-file" required attributes.
         Configurable macro, which allows to pass as parameters input directory,
         output directory, output dex filename and external libraries to dex (optional) -->
    <macrodef name="dex-helper-mod">
        <attribute name="input-dir" />
        <attribute name="output-dex-file" />
        <element name="external-libs" optional="yes" />
        <element name="extra-parameters" optional="yes" />
        <attribute name="nolocals" default="false" />
        <sequential>
            <!-- set the secondary dx input: the project (and library) jar files
                 If a pre-dex task sets it to something else this has no effect -->
            <if>
                <condition>
                    <isreference refid="out.dex.jar.input.ref" />
                </condition>
                <else>
                    <path id="out.dex.jar.input.ref">
                        <path refid="project.all.jars.path" />
                    </path>
                </else>
            </if>

            <echo>Converting compiled files and external libraries into @{output-dex-file}...</echo>
            <dex executable="${dx}"
                    output="@{output-dex-file}"
                    nolocals="@{nolocals}"
                    verbose="${verbose}">
                <path path="@{input-dir}"/>
                <path refid="out.dex.jar.input.ref" />
                <external-libs />
            </dex>
        </sequential>
    </macrodef>

    <!-- This is a modified version of "-dex" target taken from $SDK/tools/ant/main_rules.xml -->
    <!-- Converts this project's .class files into .dex files -->
    <target name="-dex" depends="-compile, -post-compile, -obfuscate"
            unless="do.not.compile">
        <if condition="${manifest.hasCode}"> <!-- 是否包含java code -->
            <then>
                <!-- Create staging directories to store .class files to be converted to the -->
                <!-- default dex and the secondary dex. -->
                <mkdir dir="${out.classes.absolute.dir}.1"/>
                <mkdir dir="${out.classes.absolute.dir}.2"/>

                <!-- Primary dex to include everything but the concrete library implementation. -->
                <copy todir="${out.classes.absolute.dir}.1" >
                    <fileset dir="${out.classes.absolute.dir}" >
                        <exclude name="com/example/dex/lib/**" />
                    </fileset>
                </copy>
                <!-- Secondary dex to include the concrete library implementation. -->
                <copy todir="${out.classes.absolute.dir}.2" >
                    <fileset dir="${out.classes.absolute.dir}" >
                        <include name="com/example/dex/lib/**" />
                    </fileset>
                </copy>

                <!-- Compile .class files from the two stage directories to the apppropriate dex files. -->
                <dex-helper-mod input-dir="${out.classes.absolute.dir}.1"
                    output-dex-file="${out.absolute.dir}/${dex.file.name}" />  <!-- classes.dex -->
                <mkdir dir="${out.absolute.dir}/secondary_dex_dir" />
                <dex-helper-mod input-dir="${out.classes.absolute.dir}.2"
                    output-dex-file="${out.absolute.dir}/secondary_dex_dir/classes.dex" />
                <!-- Jar the secondary dex file so it can be consumed by the DexClassLoader. -->
                <!-- Package the output in the assets directory of the apk. -->
                <jar destfile="${asset.absolute.dir}/secondary_dex.jar"
                     basedir="${out.absolute.dir}/secondary_dex_dir" includes="classes.dex" />
            </then>
            <else>
                <echo>hasCode = false. Skipping...</echo>
            </else>
        </if>
    </target>

    <!-- version-tag: custom -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>

其中用到的一些標籤可以多關注下。比如說import、if、copy、macrodef等。

DexClassLoader的使用例子在這裏:http://download.csdn.net/detail/imzoer/6325741

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章