AndroidSDK内置ANT编译build.xml

本文探讨了Android SDK内置于tools/ant目录下的build.xml文件,它是理解ANT编译过程的重要参考资料,对于开发者学习和掌握ANT构建工具极具价值。
摘要由CSDN通过智能技术生成

Android SDK的内置ant编译build文件

在sdk目录的tools/ant/文件夹下,对学习ANT编译过程很是一个很好的参考。

<?xml version="1.0" encoding="UTF-8"?>
<project name="android_rules" default="debug">

    <!--
        This build file is imported by the project build file. It contains
        all the targets and tasks necessary to build Android projects, be they
        regular projects, library projects, or test projects.

        At the beginning of the file is a list of properties that can be overridden
        by adding them to your ant.properties (properties are immutable, so their
        first definition sticks and is never changed).

        Follows:
        - custom task definitions,
        - more properties (do not override those unless the whole build system is modified).
        - macros used throughout the build,
        - base build targets,
        - debug-specific build targets,
        - release-specific build targets,
        - instrument-specific build targets,
        - test project-specific build targets,
        - install targets,
        - help target
    -->

    <!-- ******************************************************* -->
    <!-- **************** Overridable Properties *************** -->
    <!-- ******************************************************* -->

    <!-- You can override these values in your build.xml or ant.properties.
         Overriding any other properties may result in broken build. -->

    <!-- Tells adb which device to target. You can change this from the command line
         by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
         the emulator. -->
    <property name="adb.device.arg" value="" />

    <!-- fileset exclude patterns (space separated) to prevent
         files inside src/ from being packaged. -->
    <property name="android.package.excludes" value="" />

    <!-- set some properties used for filtering/override. If those weren't defined
         before, then this will create them with empty values, which are then ignored
         by the custom tasks receiving them. -->
    <property name="version.code" value="" />
    <property name="version.name" value="" />
    <property name="aapt.resource.filter" value="" />
    <!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
         Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

         Overall patterns syntax is:
           [!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...

         - The first character flag ! avoids printing a warning.
         - Pattern can have the flag "<dir>" to match only directories
           or "<file>" to match only files. Default is to match both.
         - Match is not case-sensitive.
    -->
    <property name="aapt.ignore.assets" value="" />

    <!-- compilation options -->
    <property name="java.encoding" value="UTF-8" />
    <property name="java.target" value="1.5" />
    <property name="java.source" value="1.5" />
    <property name="java.compilerargs" value="" />

    <!-- Renderscript options -->
    <property name="renderscript.debug.opt.level" value="O0" />
    <property name="renderscript.release.opt.level" value="O3" />

    <!-- manifest merger default value -->
    <property name="manifestmerger.enabled" value="false" />

    <!-- instrumentation options -->
    <property name="emma.filter" value="" />

    <!-- Verbosity -->
    <property name="verbose" value="false" />

    <!-- ******************************************************* -->
    <!-- ********************* Custom Tasks ******************** -->
    <!-- ******************************************************* -->

    <!-- jar file from where the tasks are loaded -->
    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
    </path>

    <!-- Custom tasks -->
    <taskdef resource="anttasks.properties" classpathref="android.antlibs" />

    <!-- Emma configuration -->
    <property name="emma.dir" value="${sdk.dir}/tools/lib" />
    <path id="emma.lib">
        <pathelement location="${emma.dir}/emma.jar" />
        <pathelement location="${emma.dir}/emma_ant.jar" />
    </path>
    <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
    <!-- End of emma configuration -->


    <!-- ******************************************************* -->
    <!-- ******************* Other Properties ****************** -->
    <!-- ******************************************************* -->
    <!-- overriding these properties may break the build
         unless the whole file is updated -->

    <!-- Input directories -->
    <property name="source.dir" value="src" />
    <property name="source.absolute.dir" location="${source.dir}" />
    <property name="gen.absolute.dir" location="gen" />
    <property name="resource.absolute.dir" location="res" />
    <property name="asset.dir" value="assets" />
    <property name="asset.absolute.dir" location="${asset.dir}" />
    <property name="jar.libs.dir" value="libs" />
    <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
    <property name="native.libs.absolute.dir" location="libs" />

    <property name="manifest.file" value="AndroidManifest.xml" />
    <property name="manifest.abs.file" location="${manifest.file}" />

    <!-- Output directories -->
    <property name="out.dir" value="bin" />
    <property name="out.absolute.dir" location="${out.dir}" />
    <property name="out.classes.absolute.dir" location="${out.dir}/classes" />
    <property name="out.res.absolute.dir" location="${out.dir}/res" />
    <property name="out.aidl.absolute.dir" location="${out.dir}/aidl" />
    <property name="out.manifest.abs.file" location="${out.dir}/AndroidManifest.xml" />

    <!-- tools location -->
    <property name="android.tools.dir" location="${sdk.dir}/tools" />
    <property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />
    <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
    <condition property="bat" value=".bat" else=""><os family="windows" /></condition>
    <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />
    <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
    <property name="aidl" location="${android.platform.tools.dir}/aidl${exe}" />
    <property name="aapt" location="${android.platform.tools.dir}/aapt${exe}" />
    <property name="dx" location="${android.platform.tools.dir}/dx${bat}" />
    <property name="renderscript" location="${android.platform.tools.dir}/llvm-rs-cc${exe}"/>

    <!-- Renderscript include Path -->
    <path id="android.renderscript.include.path">
        <pathelement location="${android.platform.tools.dir}/renderscript/include" />
        <pathelement location="${android.platform.tools.dir}/renderscript/clang-include" />
    </path>

    <!-- Intermediate files -->
    <property name="dex.file.name" value="classes.dex" />
    <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />
    <property name="resource.package.file.name" value="${ant.project.name}.ap_" />

    <!-- Build property file -->
    <property name="out.build.prop.file" location="${out.absolute.dir}/build.prop" />


    <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'
         The property 'verbosity' is not user configurable and depends exclusively on 'verbose'
         value.-->
    <condition property="verbosity" value="verbose" else="quiet">
        <istrue value="${verbose}" />
    </condition>

    <!-- properties for signing in release mode -->
    <condition property="has.keystore">
        <and>
            <isset property="key.store" />
            <length string="${key.store}" when="greater" length="0" />
            <isset property="key.alias" />
        </and>
    </condition>
    <condition property="has.password">
        <and>
            <isset property="has.keystore" />
            <isset property="key.store.password" />
            <isset property="key.alias.password" />
        </and>
    </condition>

    <!-- properties for packaging -->
    <property name="build.packaging.nocrunch" value="true" />

    <!-- whether we need to fork javac.
         This is only needed on Windows when running Java < 7 -->
    <condition else="false" property="need.javac.fork">
        <and>
            <matches pattern="1\.[56]" string="${java.specification.version}"/>
            <not>
                <os family="unix"/>
            </not>
        </and>
    </condition>

    <!-- ******************************************************* -->
    <!-- ************************ Macros *********************** -->
    <!-- ******************************************************* -->

    <!-- macro to do a task on if project.is.library is false.
         elseText attribute is displayed otherwise -->
    <macrodef name="do-only-if-not-library">
        <attribute name="elseText" />
        <element name="task-to-do" implicit="yes" />
        <sequential>
        <if condition="${project.is.library}">
            <else>
                <task-to-do />
            </else>
            <then>
                <echo level="info">@{elseText}</echo>
            </then>
        </if>
        </sequential>
    </macrodef>

    <!-- macro to do a task on if manifest.hasCode is true.
         elseText attribute is displayed otherwise -->
    <macrodef name="do-only-if-manifest-hasCode">
        <attribute name="elseText" default=""/>
        <element name="task-to-do" implicit="yes" />
        <sequential>
        <if condition="${manifest.hasCode}">
            <then>
                <task-to-do />
            </then>
            <else>
                <if>
                    <condition>
                        <length string="@{elseText}" trim="true" when="greater" length="0" />
                    </condition>
                    <then>
                        <echo level="info">@{elseText}</echo>
                    </then>
                </if>
            </else>
        </if>
        </sequential>
    </macrodef>


    <!-- Configurable macro, which allows to pass as parameters output directory,
         output dex filename and external libraries to dex (optional) -->
    <macrodef name="dex-helper">
        <element name="external-libs" optional="yes" />
        <attribute name="nolocals" default="false" />
        <sequential>
            <!-- sets the primary input for dex. If a pre-dex task sets it to
                 something else this has no effect -->
            <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />

            <!-- 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>

            <dex executable="${dx}"
                    output="${intermediate.dex.file}"
                    nolocals="@{nolocals}"
                    verbose="${verbose}">
                <path path="${out.dex.input.absolute.dir}"/>
                <path refid="out.dex.jar.input.ref" />
                <external-libs />
            </dex>
        </sequential>
    </macrodef>

    <!-- This is macro that enable passing variable list of external jar files to ApkBuilder
         Example of use:
         <package-helper>
             <extra-jars>
                <jarfolder path="my_jars" />
                <jarfile path="foo/bar.jar" />
                <jarfolder path="your_jars" />
             </extra-jars>
         </package-helper> -->
    <macrodef name="package-helper">
        <element name="extra-jars" optional="yes" />
        <sequential>
            <apkbuilder
                    outfolder="${out.absolute.dir}"
                    resourcefile="${resource.package.file.name}"
                    apkfilepath="${out.packaged.file}"
                    debugpackaging="${build.is.packaging.debug}"
                    debugsigning="${build.is.signing.debug}"
                    verbose="${verbose}"
                    hascode="${manifest.hasCode}"
                    previousBuildType="${build.last.is.packaging.debug}/${build.last.is.signing.debug}"
                    buildType="${build.is.packaging.debug}/${build.is.signing.debug}">
                <dex path="${intermediate.dex.file}"/>
                <sourcefolder path="${source.absolute.dir}"/>
                <jarfile refid="project.all.jars.path" />
                <nativefolder path="${native.libs.absolute.dir}" />
                <nativefolder refid="project.library.native.folder.path" />
                <extra-jars/>
            </apkbuilder>
        </sequential>
    </macrodef>

    <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
         debug, -debug-with-emma and release.-->
    <macrodef name="zipalign-helper">
        <attribute name="in.package" />
        <attribute name="out.package
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值