通過ant指令碼編譯打包android工程
通過ant指令碼,編譯打包android工程
1.Android程式編譯、打包、簽名、釋出的三種方式:
方式一:命令列手動編譯打包
方式二:使用ant自動編譯打包
方式三:使用eclipse+ADT編譯打包
2.Android編譯、打包的步驟:
2.1第一步 生成R.java類檔案:
Eclipse中會自動生成R.java,ant和命令列使用android SDK提供的aapt.ext程式生成R.java。
2.2第二步 將.aidl檔案生成.java類檔案:
Eclipse中自動生成,ant和命令列使用android SDK提供的aidl.exe生成.java檔案。
2.3第三步 編譯.java類檔案生成class檔案:
Eclipse中自動生成,ant和命令列使用jdk的javac編譯java類檔案生成class檔案。
2.4第四步 將class檔案打包生成classes.dex檔案:
Eclipse中自動生成,ant和命令列使用android SDK提供的dx.bat命令列指令碼生成classes.dex檔案。
2.5第五步 打包資原始檔(包括res、assets、androidmanifest.xml等):
Eclipse中自動生成,ant和命令列使用Android SDK提供的aapt.exe生成資源包檔案。
2.6第六步 生成未簽名的apk安裝檔案:
Eclipse中自動生成debug簽名檔案存放在bin目錄中,ant和命令列使用android SDK提供的apkbuilder.bat命令指令碼生成未簽名的apk安裝檔案。
2.7第七步 對未簽名的apk進行簽名生成簽名後的android檔案:
Eclipse中使用Android Tools進行簽名,ant和命令列使用jdk的jarsigner對未簽名的包進行apk簽名。
這個ant指令碼只能編譯打包一個單獨的android工程或依賴一個library 的android工程
首先配置ant的環境變數,這個我就不多少了,自己查。
ant指令碼,生成build.xml
- <span style="font-size:18px">H:\install\eclipse-SDK-3.7.2-win32\eclipse\android_sdk\tools\android.bat update project -n BuiltDemo -t android-8 -p H:\workspace\prac_a3\BuiltDemo</span>
- <span style="font-size:18px">sdk.dir=H:/install/eclipse-SDK-3.7.2-win32/eclipse/android_sdk
- ANDROID_HOME=H:/install/eclipse-SDK-3.7.2-win32/eclipse/android_sdk
- ANT_HOME=F:/Soft/ant/apache-ant-1.9.2
- JAVA_HOME=C:/Program Files/Java/jdk1.6.0_10</span>
- <span style="font-size:18px">H:\install\eclipse-SDK-3.7.2-win32\eclipse\android_sdk\tools\android.bat debug
- H:\install\eclipse-SDK-3.7.2-win32\eclipse\android_sdk\tools\android.bat release</span>
- <span style="font-size:18px"><?xml version="1.0" encoding="UTF-8"?>
- <project name="BuiltDemo" default="release">
- <property file="local.properties" />
- <!-- ANT環境變數 -->
- <property environment="env" />
- <!-- 應用名稱 -->
- <property name="appName" value="BuiltDemo" />
- <property name="basedir" value="H:/workspace/prac_a3/BuiltDemo" />
- <property name="library-dir" value="H:/workspace/prac_a3/BuiltDemo">
- </property>
- <!-- SDK目錄(獲取作業系統環境變數ANDROID_SDK_HOME的值) -->
- <!-- <property name="sdk-folder" value="${env.ANDROID_SDK_HOME}" /> -->
- <property name="sdk-folder" value="${env.ANDROID_HOME}" />
- <!-- SDK指定平臺目錄 -->
- <property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-8" />
- <!-- SDK中tools目錄 -->
- <property name="sdk-tools" value="${sdk-folder}/tools" />
- <!-- SDK指定平臺中tools目錄 -->
- <property name="sdk-platform-tools" value="${sdk-folder}/platform-tools" />
- <!-- 使用到的命令(當前系統為windows,如果系統為linux,可將.bat檔案替換成相對應的命令) -->
- <property name="aapt" value="${sdk-platform-tools}/aapt.exe" />
- <property name="aidl" value="${sdk-platform-tools}/aidl.exe" />
- <property name="dx" value="${sdk-platform-tools}/dx.bat" />
- <property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />
- <property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner" />
- <!-- 編譯需要的jar; 如果專案使用到地圖服務則需要maps.jar -->
- <property name="android-jar" value="${sdk-platform-folder}/android.jar" />
- <!-- 編譯aidl檔案所需的預處理框架檔案framework.aidl -->
- <property name="framework-aidl" value="${sdk-platform-folder}/framework.aidl" />
- <!-- 生成R檔案的相對目錄 -->
- <property name="outdir-gen" value="gen" />
- <!-- 編譯後的檔案放置目錄 -->
- <property name="outdir-bin" value="out" />
- <!-- 清單檔案 -->
- <property name="manifest-xml" value="AndroidManifest.xml" />
- <!-- 原始檔目錄 -->
- <property name="resource-dir" value="res" />
- <property name="asset-dir" value="assets" />
- <!-- java原始檔目錄 -->
- <property name="srcdir" value="src" />
- <property name="srcdir-ospath" value="${basedir}/${srcdir}" />
- <!-- 外部類庫所在目錄 -->
- <property name="external-lib" value="libs" />
- <property name="external-lib-ospath" value="${basedir}/${external-lib}" />
- <!-- 生成class目錄 -->
- <property name="outdir-classes" value="${outdir-bin}" />
- <property name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />
- <!-- classes.dex相關變數 -->
- <property name="dex-file" value="classes.dex" />
- <property name="dex-path" value="${outdir-bin}/${dex-file}" />
- <property name="dex-ospath" value="${basedir}/${dex-path}" />
- <!-- 經過aapt生成的資源包檔案 -->
- <property name="resources-package" value="${outdir-bin}/resources.ap_" />
- <property name="resources-package-ospath" value="${basedir}/${resources-package}" />
- <!-- 未認證apk包 -->
- <property name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />
- <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
- <!-- 證照檔案 -->
- <property name="keystore-file" value="${basedir}/sbx" />
- <!-- 已認證apk包 -->
- <property name="out-signed-package" value="${outdir-bin}/${appName}.apk" />
- <property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />
- <!-- 初始化工作 -->
- <target name="init">
- <echo>Initializing all output directories...</echo>
- <delete dir="${outdir-bin}" />
- <mkdir dir="${outdir-bin}" />
- <mkdir dir="${outdir-classes}" />
- </target>
- <!-- 根據工程中的資原始檔生成R.java檔案 -->
- <target name="gen-R" depends="init">
- <echo>Generating R.java from the resources...</echo>
- <!--<exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-m" />
- <arg value="-J" />
- <arg value="${outdir-gen}" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-M" />
- <arg value="${manifest-xml}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- </exec>-->
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-m" />
- <arg value="--auto-add-overlay" />
- <arg value="-J" />
- <arg value="${outdir-gen}" />
- <arg value="-M" />
- <arg value="${manifest-xml}" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-S" />
- <arg value="${library-dir}/${resource-dir}" />
- <arg value="--extra-packages" />
- <arg value="com.mobcent.share.android" />
- <arg value="-A" />
- <arg value="${asset-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- </exec>
- </target>
- <!-- 編譯aidl檔案 -->
- <target name="aidl" depends="gen-R">
- <echo>Compiling .aidl into java files...</echo>
- <apply executable="${aidl}" failonerror="true">
- <!-- 指定預處理檔案 -->
- <arg value="-p${framework-aidl}" />
- <!-- aidl宣告的目錄 -->
- <arg value="-I${srcdir}" />
- <!-- 目標檔案目錄 -->
- <arg value="-o${outdir-gen}" />
- <!-- 指定哪些檔案需要編譯 -->
- <fileset dir="${srcdir}">
- <include name="**/*.aidl" />
- </fileset>
- </apply>
- </target>
- <!-- 將工程中的java原始檔編譯成class檔案 -->
- <target name="compile" depends="aidl">
- <echo>Compiling java source code...</echo>
- <javac encoding="utf-8" target="1.6" destdir="${outdir-classes}" bootclasspath="${android-jar}">
- <src path="src" />
- <src path="gen" />
- <src path="${library-dir}/src" />
- <classpath>
- <fileset dir="${external-lib}" includes="*.jar" />
- <fileset dir="${library-dir}/libs" includes="*.jar" />
- <filelist>
- <file name="${android-maps-jar}" />
- </filelist>
- </classpath>
- </javac>
- </target>
- <!-- 將.class檔案轉化成.dex檔案 -->
- <target name="dex" depends="compile">
- <echo>Converting compiled files and external libraries into a .dex
- file...
- </echo>
- <exec executable="${dx}" failonerror="true">
- <arg value="--dex" />
- <!-- 輸出檔案 -->
- <arg value="--output=${dex-ospath}" />
- <!-- 要生成.dex檔案的源classes和libraries -->
- <arg value="${outdir-classes-ospath}" />
- <arg value="${external-lib-ospath}" />
- <!-- <arg value="${library-dir}/libs" /> -->
- </exec>
- </target>
- <!-- 將資原始檔放進輸出目錄 -->
- <!--在這截斷-->
- <target name="package-res-and-assets" depends="dex">
- <echo>Packaging resources and assets...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-M" />
- <arg value="${manifest-xml}" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-A" />
- <arg value="${asset-dir}" />
- <arg value="-S" />
- <arg value="${library-dir}/${resource-dir}" />
- <arg value="-A" />
- <arg value="${library-dir}/${asset-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- <arg value="-F" />
- <arg value="${resources-package}" />
- <arg value="--auto-add-overlay" />
- </exec>
- </target>
- <!-- 打包成未簽證的apk -->
- <target name="package" depends="dex, package-res-and-assets">
- <echo>Packaging unsigned apk for release...</echo>
- <exec executable="${apkbuilder}" failonerror="true">
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="-u" />
- <arg value="-z" />
- <arg value="${resources-package-ospath}" />
- <arg value="-f" />
- <arg value="${dex-ospath}" />
- <arg value="-rf" />
- <arg value="${srcdir-ospath}" />
- <arg value="-nf"/>
- <arg value="${library-dir}/libs"/>
- </exec>
- <echo>It will need to be signed with jarsigner before being published.
- </echo>
- </target>
- <!-- 對apk進行簽證 -->
- <target name="jarsigner" depends="package">
- <echo>Packaging signed apk for release...</echo>
- <exec executable="${jarsigner}" failonerror="true">
- <arg value="-keystore" />
- <arg value="${keystore-file}" />
- <arg value="-storepass" />
- <arg value="111111" />
- <arg value="-keypass" />
- <arg value="111111" />
- <arg value="-signedjar" />
- <arg value="${out-signed-package-ospath}" />
- <arg value="${out-unsigned-package-ospath}" />
- <!-- 不要忘了證照的別名 -->
- <arg value="sbx" />
- </exec>
- </target>
- <!-- 釋出 -->
- <target name="release" depends="jarsigner">
- <!-- 刪除未簽證apk -->
- <delete file="${out-unsigned-package-ospath}" />
- <echo>APK is released. path:${out-signed-package-ospath}</echo>
- </target>
- </project>
- </span>
Android官方提供的打包指令碼: 1400多行,我加了中文註釋,希望能看懂。
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="pet_dog_base_forum" default="release">
- <!--自己需要新增的屬性 -->
- <property name="sdk.dir" value="C:/Program Files/android-sdk_r15-windows/android-sdk-windows" />
- <!--匯入project.properties 檔案,設定了編譯的target 和相關的library工程-->
- <property file="project.properties" />
- <!--匯入build.properties檔案,設定了android的目錄和key-->
- <property file="ant.properties" />
- <!--
- 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 build.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
- 步驟如下:
- —— 自定義task
- —— 設定相關屬性
- —— 全域性的使用整個構建
- —— 基本bulid的targets
- —— debug使用的targets
- —— release使用的targets
- —— 特定儀器使用的targets
- —— 測試使用的targets
- —— 安裝的targets
- —— 幫助的targets
- -->
- <!-- ********** Overrideable Properties ********** -->
- <!-- ********** 可重寫的屬性 ********** -->
- <!-- You can override these values in your build.xml or build.properties.
- Overriding any other properties may result in broken build. -->
- <!-- 你可以覆蓋build.xml 或者 build.properties 檔案中的任何屬性,覆蓋任何一個屬性,都可能導致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. -->
- <!-- 設定連結的機器,
- ant -Dadb.device.arg=-d 使用連結當前的裝置
- ant -Dadb.device.arg=-e 使用模擬器
- -->
- <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. -->
- <!--
- version.code,version.name可以替換AndroidManifest.xml中的android:versionCode和android:versionName
- -->
- <property name="version.code" value="11" />
- <property name="version.name" value="111" />
- <property name="aapt.resource.filter" value="" />
- <!-- compilation options -->
- <property name="java.encoding" value="UTF-8" />
- <property name="java.target" value="1.6" />
- <property name="java.source" value="1.6" />
- <!-- Verbosity -->
- <property name="verbose" value="false" />
- <!-- ********** Custom Tasks ********** -->
- <!-- ********** 自定義Tasks ********** -->
- <!-- 匯入自定義Task是需要的檔案 -->
- <path id="android.antlibs">
- <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
- </path>
- <!-- Custom tasks -->
- <taskdef name="setup" classname="com.android.ant.NewSetupTask" classpathref="android.antlibs" />
- <taskdef name="aapt" classname="com.android.ant.AaptExecTask" classpathref="android.antlibs" />
- <taskdef name="aidl" classname="com.android.ant.AidlExecTask" classpathref="android.antlibs" />
- <taskdef name="renderscript" classname="com.android.ant.RenderScriptTask" classpathref="android.antlibs" />
- <taskdef name="dex" classname="com.android.ant.DexExecTask" classpathref="android.antlibs" />
- <taskdef name="apkbuilder" classname="com.android.ant.ApkBuilderTask" classpathref="android.antlibs" />
- <taskdef name="zipalign" classname="com.android.ant.ZipAlignTask" classpathref="android.antlibs" />
- <taskdef name="xpath" classname="com.android.ant.XPathTask" classpathref="android.antlibs" />
- <taskdef name="if" classname="com.android.ant.IfElseTask" classpathref="android.antlibs" />
- <!-- Emma configuration
- EMMA 是一種快速的,基於位元組碼指令的Java 程式碼覆蓋工具。
- -->
- <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 -->
- <!-- 輸入檔案 -->
- <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.absolute.dir" location="assets" />
- <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="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" />
- <!-- 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}" />
- <!-- renderscript location is set by NewSetupTask since we have a choice of
- several executables based on minSdkVersion -->
- <!-- 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 build的屬性檔案 -->
- <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.
- 這是需要通過艾瑪,因為它使用多級verbosity不是簡單的“true”或“false”。屬性“冗長”不是使用者可配置的,只取決於verbose”值。
- -->
- <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" />
- <!-- ********** Macros ********** -->
- <!-- ********** 巨集定義 ********** -->
- <!-- macro to do a task on if project.is.library is false.
- elseText attribute is displayed otherwise -->
- <!-- 定義了沒有關聯library工程時,將會列印elseText -->
- <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>@{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>@{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)
- 配置巨集,允許通過引數設定輸出的目錄,dex檔案和dex額外的libraries
- -->
- <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="jar.libs.ref" />
- </path>
- </else>
- </if>
- <dex executable="${dx}" output="${intermediate.dex.file}" nolocals="@{nolocals}" verbose="${verbose}" previousBuildType="${build.last.target}" buildType="${build.target}">
- <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
- 設定ApkBuilder 是額外的jar檔案
- 預設把工程下libs中的jar檔案打到APK裡
- 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="jar.libs.ref" />
- <nativefolder path="${native.libs.absolute.dir}" />
- <nativefolder refid="project.libraries.libs" />
- <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.
- 通過zipaligns 對APK進行優化
- -->
- <macrodef name="zipalign-helper">
- <attribute name="in.package" />
- <attribute name="out.package" />
- <sequential>
- <zipalign executable="${zipalign}" input="@{in.package}" output="@{out.package}" verbose="${verbose}" />
- </sequential>
- </macrodef>
- <macrodef name="run-tests-helper">
- <attribute name="emma.enabled" default="false" />
- <element name="extra-instrument-args" optional="yes" />
- <sequential>
- <echo>Running tests ...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg line="${adb.device.arg}" />
- <arg value="shell" />
- <arg value="am" />
- <arg value="instrument" />
- <arg value="-w" />
- <arg value="-e" />
- <arg value="coverage" />
- <arg value="@{emma.enabled}" />
- <extra-instrument-args />
- <arg value="${manifest.package}/${test.runner}" />
- </exec>
- </sequential>
- </macrodef>
- <macrodef name="record-build-key">
- <attribute name="key" default="false" />
- <attribute name="value" default="false" />
- <sequential>
- <propertyfile file="${out.build.prop.file}" comment="Last build type">
- <entry key="@{key}" value="@{value}" />
- </propertyfile>
- </sequential>
- </macrodef>
- <macrodef name="record-build-info">
- <sequential>
- <record-build-key key="build.last.target" value="${build.target}" />
- <record-build-key key="build.last.is.instrumented" value="${build.is.instrumented}" />
- <record-build-key key="build.last.is.packaging.debug" value="${build.is.packaging.debug}" />
- <record-build-key key="build.last.is.signing.debug" value="${build.is.signing.debug}" />
- </sequential>
- </macrodef>
- <macrodef name="uninstall-helper">
- <attribute name="app.package" default="false" />
- <sequential>
- <echo>Uninstalling @{app.package} from the default emulator or device...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg line="${adb.device.arg}" />
- <arg value="uninstall" />
- <arg value="@{app.package}" />
- </exec>
- </sequential>
- </macrodef>
- <!-- ********** Build Targets ********** -->
- <!-- this target simply force running -setup making
- the project info be read. To be used as
- ant all clean
- to clean the main project as well as the libraries and tested project
- 執行-setup,在此之前必須執行clean,
- -->
- <target name="all" depends="-setup" />
- <!-- clean target -->
- <target name="clean" description="Removes output files created by other targets.">
- <delete dir="${out.absolute.dir}" verbose="${verbose}" />
- <delete dir="${gen.absolute.dir}" verbose="${verbose}" />
- <!-- if we know about a tested project or libraries, we clean them too. This
- will only work if the target 'all' was called first -->
- <if condition="${project.is.test}">
- <then>
- <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
- <subant failonerror="true">
- <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
- <target name="all" />
- <target name="clean" />
- </subant>
- </then>
- </if>
- <if>
- <condition>
- <isreference refid="project.libraries" />
- </condition>
- <then>
- <!-- 有libraries關聯工程的時候,呼叫libraries工程中build.xml -->
- <subant buildpathref="project.libraries" antfile="build.xml" failonerror="true">
- <target name="all" />
- <target name="clean" />
- </subant>
- </then>
- </if>
- </target>
- <!-- generic setup 初始化-->
- <target name="-setup">
- <if>
- <condition>
- <not>
- <isset property="setup.done" />
- </not>
- </condition>
- <then>
- <property name="setup.done" value="true" />
- <echo>Gathering info for ${ant.project.name}...</echo>
- <!-- load project properties, resolve Android target, library dependencies
- and set some properties with the results.
- All property names are passed as parameters ending in -Out
- 載入project properties,設定 Android target,依賴的library工程和一些其他的屬性
- -->
- <setup projectTypeOut="android.project.type" androidJarFileOut="android.jar" androidAidlFileOut="android.aidl" renderScriptExeOut="renderscript" renderScriptIncludeDirOut="android.rs" bootclasspathrefOut="android.target.classpath" projectLibrariesRootOut="project.libraries" projectLibrariesJarsOut="project.libraries.jars" projectLibrariesResOut="project.libraries.res" projectLibrariesPackageOut="project.libraries.package" projectLibrariesLibsOut="project.libraries.libs" targetApiOut="target.api" />
- <!-- sets a few boolean based on android.project.type
- to make the if task easier -->
- <condition property="project.is.library" else="false">
- <equals arg1="${android.project.type}" arg2="library" />
- </condition>
- <condition property="project.is.test" else="false">
- <equals arg1="${android.project.type}" arg2="test" />
- </condition>
- <!-- If a test project, resolve absolute path to tested project. -->
- <if condition="${project.is.test}">
- <then>
- <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
- </then>
- </if>
- </then>
- </if>
- </target>
- <!-- Pre build setup
- 預編譯
- -->
- <target name="-build-setup" depends="-setup">
- <!-- read the previous build mode -->
- <property file="${out.build.prop.file}" />
- <!-- if empty the prop won't be set, so set it to the current target
- to provide a default value equal to the current build -->
- <property name="build.last.target" value="${build.target}" />
- <!-- also set the default value for whether the build is instrumented -->
- <property name="build.last.is.instrumented" value="${build.is.instrumented}" />
- <property name="build.last.is.packaging.debug" value="${build.is.packaging.debug}" />
- <property name="build.last.is.signing.debug" value="${build.is.signing.debug}" />
- <!-- compile the libraries if any
- 編譯libraries
- -->
- <if>
- <condition>
- <isreference refid="project.libraries" />
- </condition>
- <then>
- <echo>Building Libraries</echo>
- <subant buildpathref="project.libraries" antfile="build.xml" target="${build.target}" failonerror="true" />
- <echo>
- </echo>
- <echo>############################################</echo>
- <echo>**** Back to project ${ant.project.name} ****</echo>
- <echo>############################################</echo>
- </then>
- </if>
- <!-- compile the main project if this is a test project
- 編譯主工程,如果這是測試工程
- -->
- <if condition="${project.is.test}">
- <then>
- <!-- figure out which target must be used to build the tested project.
- If emma is enabled, then use 'instrument' otherwise, use 'debug' -->
- <condition property="tested.project.target" value="instrument" else="debug">
- <isset property="emma.enabled" />
- </condition>
- <echo>Building tested project at ${tested.project.absolute.dir}</echo>
- <subant target="${tested.project.target}" failonerror="true">
- <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
- </subant>
- <echo>
- </echo>
- <echo>############################################</echo>
- <echo>**** Back to project ${ant.project.name} ****</echo>
- <echo>############################################</echo>
- </then>
- </if>
- <!-- Value of the hasCode attribute (Application node) extracted from manifest file -->
- <xpath input="AndroidManifest.xml" expression="/manifest/application/@android:hasCode" output="manifest.hasCode" default="true" />
- <!-- create a path with all the jar files, from the main project and the
- libraries
- 建立一個path,關聯所有的jar檔案。每個工程下的libs下的jar檔案
- -->
- <path id="jar.libs.ref">
- <fileset dir="${jar.libs.absolute.dir}" includes="*.jar" />
- <path refid="project.libraries.jars" />
- </path>
- <!-- special case for instrumented: if the previous build was
- instrumented but not this one, clear out the compiled code
- 特殊情況被打斷,清除已編譯的程式碼
- -->
- <if>
- <condition>
- <and>
- <istrue value="${build.last.is.instrumented}" />
- <isfalse value="${build.is.instrumented}" />
- </and>
- </condition>
- <then>
- <echo>Switching from instrumented to non-instrumented build.</echo>
- <echo>Deleting previous compilation output:</echo>
- <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
- </then>
- </if>
- <echo>Creating output directories if needed...</echo>
- <mkdir dir="${resource.absolute.dir}" />
- <mkdir dir="${jar.libs.absolute.dir}" />
- <mkdir dir="${out.absolute.dir}" />
- <mkdir dir="${out.res.absolute.dir}" />
- <do-only-if-manifest-hasCode>
- <mkdir dir="${gen.absolute.dir}" />
- <mkdir dir="${out.classes.absolute.dir}" />
- </do-only-if-manifest-hasCode>
- </target>
- <!-- empty default pre-build target. Create a similar target in
- your build.xml and it'll be called instead of this one. -->
- <target name="-pre-build" />
- <!-- Code Generation: compile resources (aapt -> R.java), aidl, renderscript
- 通過appt 生成R.jar檔案
- -->
- <target name="-code-gen">
- <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping aidl/renderscript/R.java">
- <echo>----------</echo>
- <echo>Handling aidl files...</echo>
- <aidl executable="${aidl}" framework="${android.aidl}" genFolder="${gen.absolute.dir}">
- <source path="${source.absolute.dir}" />
- </aidl>
- <!-- renderscript generates resources so it must be called before aapt -->
- <echo>----------</echo>
- <echo>Handling RenderScript files...</echo>
- <renderscript executable="${renderscript}" framework="${android.rs}" genFolder="${gen.absolute.dir}" resFolder="${resource.absolute.dir}/raw" targetApi="${target.api}">
- <source path="${source.absolute.dir}" />
- </renderscript>
- <echo>----------</echo>
- <echo>Handling Resources...</echo>
- <aapt executable="${aapt}" command="package" verbose="${verbose}" manifest="AndroidManifest.xml" androidjar="${android.jar}" rfolder="${gen.absolute.dir}" nonConstantId="${android.library}" projectLibrariesResName="project.libraries.res" projectLibrariesPackageName="project.libraries.package">
- <res path="${resource.absolute.dir}" />
- </aapt>
- </do-only-if-manifest-hasCode>
- </target>
- <!-- empty default pre-compile target. Create a similar target in
- your build.xml and it'll be called instead of this one. -->
- <target name="-pre-compile" />
- <!-- Compiles this project's .java files into .class files.
- 編譯
- -->
- <target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
- <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
- <!-- If android rules are used for a test project, its classpath should include
- tested project's location
- 如果是測試工程,classpath應該包括test的位置
- -->
- <condition property="extensible.classpath" value="${tested.project.absolute.dir}/bin/classes" else=".">
- <isset property="tested.project.absolute.dir" />
- </condition>
- <condition property="extensible.libs.classpath" value="${tested.project.absolute.dir}/${jar.libs.dir}" else="${jar.libs.dir}">
- <isset property="tested.project.absolute.dir" />
- </condition>
- <javac encoding="${java.encoding}" source="${java.source}" target="${java.target}" debug="true" extdirs="" destdir="${out.classes.absolute.dir}" bootclasspathref="android.target.classpath" verbose="${verbose}" classpath="${extensible.classpath}" classpathref="jar.libs.ref">
- <src path="${source.absolute.dir}" />
- <src path="${gen.absolute.dir}" />
- <classpath>
- <fileset dir="${extensible.libs.classpath}" includes="*.jar" />
- </classpath>
- </javac>
- <!-- if the project is a library then we generate a jar file
- 如果工程是library工程,則生成jar檔案
- -->
- <if condition="${project.is.library}">
- <then>
- <echo>Creating library output jar file...</echo>
- <property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
- <if>
- <condition>
- <length string="${android.package.excludes}" trim="true" when="greater" length="0" />
- </condition>
- <then>
- <echo>Custom jar packaging exclusion: ${android.package.excludes}
相關文章
- uni-app 通過命令列編譯打包APP命令列編譯
- Android編譯通過,執行編譯錯誤問題總結Android編譯
- Android 編譯打包的那些疑問Android編譯
- Android Apk 檔案反編譯和重新打包的過程分析AndroidAPK編譯
- Android進階:十四、熟悉Android打包編譯的流程Android編譯
- Android Apk反編譯系列教程(二)APK重打包AndroidAPK編譯
- AS中匯入android系統包編譯,執行全部通過Android編譯
- [Flutter翻譯]通過重新編譯Flutter引擎對Flutter應用進行逆向工程。Flutter編譯
- 高通程式碼編譯編譯
- MacOS X 編譯Android原始碼Mac編譯Android原始碼
- Android FrameWork 之原始碼編譯AndroidFramework原始碼編譯
- 使用 Fastlane 實現 iOS 跟 Android 自動打包指令碼ASTiOSAndroid指令碼
- Flutter Android 工程結構及應用層編譯原始碼深入分析FlutterAndroid編譯原始碼
- 通過編寫指令碼和程式來擴充套件SSIS包NZ指令碼套件
- [nghttp2]壓測工具,原始碼編譯並進行deb打包過程HTTP原始碼編譯
- android 反編譯APK取原始碼。Android編譯APK原始碼
- 【album】編譯工程編譯
- 編譯 App 工程編譯APP
- ffmpeg iOS平臺編譯 指令碼註釋iOS編譯指令碼
- Java的指令碼機制、編譯器APIJava指令碼編譯API
- libusb android ndk編譯--編譯mipsAndroid編譯
- 通過 Redis 定時執行指令碼Redis指令碼
- 通過shell指令碼防止埠掃描指令碼
- Ubuntu 15.04編譯Android 6.0.1原始碼-Nexus5真機編譯Ubuntu編譯Android原始碼
- Android 11 原始碼下載+編譯教程Android原始碼編譯
- 通過TCP碼流識別編碼TCP
- iOS自動化編譯打包iOS編譯
- Assimp Android 編譯Android編譯
- android編譯方法Android編譯
- android 反編譯Android編譯
- Android系統編譯指令make 、mmm、mm優缺點比較Android編譯
- Azure DevOps (八) 通過流水線編譯Docker映象dev編譯Docker
- Verilog 編譯指令簡介編譯
- 通過shell指令碼 批量新增使用者指令碼
- windows通過python指令碼重啟本地redisWindowsPython指令碼Redis
- 通過Android反編譯技術研究國內陌生人社交即時通訊的技術方案Android編譯
- Mac Android8.0原始碼編譯筆記MacAndroid原始碼編譯筆記
- dll預編譯提高webpack打包速度編譯Web
- Taro編譯打包優化實踐編譯優化