Android and Ant - 3. 了解 Ant Script Files

來看看 ant.properties, build.xml, local.properties 這三個位於 Android project 根目錄下的檔案的作用。

Ant Script Files

  1. local.properties

    使用 android create projectandroid update project 產生這個檔案時,會自動填入 Android SDK 的所在路徑(sdk.dir),如果 project 使用到 JNI,要檢查一下這裡有沒有設定 ndk.dir,少了就自行補上。

    # This file is used to override default values used by the Ant build system.
    #
    # This file must be checked into Version Control Systems, as it is
    # integral to the build system of your project.
    
    # This file is only used by the Ant script.
    
    # You can use this to override default values such as
    #  'source.dir' for the location of your java source folder and
    #  'out.dir' for the location of your output folder.
    
    # You can also use it define how the release builds are signed by declaring
    # the following properties:
    #  'key.store' for the location of your keystore and
    #  'key.alias' for the name of the key to use.
    # The password will be asked during the build when you use the 'release' target.
    sdk.dir=C:\Program Files\Android\android-sdk
    
    # NDK path is optional
    ndk.dir=C:\AndroidNDK\android-ndk-r8e
    
  2. ant.properties

    如果打包 APK 時需要簽名(sign),則需要在此檔案裡填寫 keystore path, keystore password, alias name, alias password,編譯過程中 Ant script 會查看這個檔案,如果有設置就會用這裡指定的 keystore 簽名 APK。

    # This file is used to override default values used by the Ant build system.
    #
    # This file must be checked into Version Control Systems, as it is
    # integral to the build system of your project.
    
    # This file is only used by the Ant script.
    
    # You can use this to override default values such as
    #  'source.dir' for the location of your java source folder and
    #  'out.dir' for the location of your output folder.
    
    # You can also use it define how the release builds are signed by declaring
    # the following properties:
    #  'key.store' for the location of your keystore and
    #  'key.alias' for the name of the key to use.
    # The password will be asked during the build when you use the 'release' target.
    key.store=YOUR_KEYSTORE_PATH
    key.alias=YOUR_KEYSTORE_ALIAS
    key.store.password=YOUR_KEYSTORE_PASSWORD
    key.alias.password=YOUR_ALIAS_PASSWORD
    
  3. build.xml

    檔案內容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="HelloApp" 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" />
    
        <!-- if sdk.dir was not set from one of the property file, then
             get it from the ANDROID_HOME env var.
             This must be done before we load project.properties since
             the proguard config can use sdk.dir -->
        <property environment="env" />
        <condition property="sdk.dir" value="${env.ANDROID_HOME}">
            <isset property="env.ANDROID_HOME" />
        </condition>
    
        <!-- 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 the ANDROID_HOME environment variable."
                unless="sdk.dir"
        />
    
        <!--
            Import per project custom build rules if present at the root of the project.
            This is the place to put custom intermediary targets such as:
                -pre-build
                -pre-compile
                -post-compile (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})
                -post-package
                -post-build
                -pre-clean
        -->
        <import file="custom_rules.xml" optional="true" />
    
        <!-- 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"
        -->
        <!-- version-tag: 1 -->
        <import file="${sdk.dir}/tools/ant/build.xml" />
    
    </project>
    

    這個檔案是 Ant script build 的起始點,註解很詳細,可以看到幾個關鍵處:

    • 會載入 local.properties, ant.properties, project.properties 三個位於 project 根目錄下的檔案
    • 會檢查是否有設置 sdk.dir,如果沒有,就直接終止編譯
    • custom_rules.xml 不是必要檔案,此檔案是用來客製化編譯過程中的以下幾個階段,例如:想要在編譯之前,設定一些 preprocessor (#define),就可以寫在 -pre-build;想在編譯完成後把 APK 移動到其他位置,可以覆寫 -post-build。

      • -pre-build
      • -pre-compile
      • -post-compile
      • -post-package
      • -post-build
      • -pre-clean
    • 最後進入 Ant script 的主要檔案,此檔案是在 Android SDK 裡,位於 ${sdk.dir}/tools/ant/build.xml

Comments

Comments powered by Disqus