首先確保你已經配置好了Android 開發環境,並創建了ANDROID_SDK_HOME 環境變量。
ANT配置
下載Ant並解壓,這裏我們選擇了ant-1.9.0 解壓目錄是D:\JAVA\apache-ant-1.9.0 配置環境變量ANT_HOME=D:\JAVA\apache-ant-1.9.在PATH中添加 %ANT_HOME%\bin;
ant-contrib-1.0b3.jar 配置
下載ant-contrib-1.0b3.jar 並將其放到D:\JAVA\apache-ant-1.9.0\lib中。
build.xml創建
Available Android targets:----------id: 1 or "android-10"Name: Android 2.3.3Type: PlatformAPI level: 10Revision: 2Skins:ABIs : no ABIs.----------id: 2 or "android-19"Name: Android 4.4.2Type: PlatformAPI level: 19Revision: 2Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7inABIs : armeabi-v7a
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> <project name="MainActivity" 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>
修改點:<?xml version="1.0" encoding="UTF-8"?> <project name="HelloWorld" default="release"> <!-- 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_SDK_HOME}"> <isset property="env.ANDROID_SDK_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" /> <property name="out.unaligned.dir" value="F:\HelloWorld_${app_version}/" /> <mkdir dir="${out.unaligned.dir}" /> <target name="modify_update_file"> <echo>*********************** make channel ${channel}</echo> <replaceregexp file="AndroidManifest.xml" match='channel' replace='${channel}' byline="false" encoding="utf-8" /> <property name="out.unaligned.file" location="${out.unaligned.dir}/HelloWorld_${app_version}_${channel}_android.apk"/> </target> <target name="make_one_channels" depends="savemanifest,modify_update_file,release,replacemanifest,deletebin" description="description"> </target> <target name="replacemanifest"> <echo>*********************** replacemanifest</echo> <delete file="${basedir}\AndroidManifest.xml"/> <copy file="..\temp\build\META-INF\AndroidManifest.xml" todir="${basedir}" encoding="utf-8"/> </target> <target name="savemanifest"> <echo>*********************** savemanifest</echo> <copy file="${basedir}\AndroidManifest.xml" todir="..\temp\build\META-INF" encoding="utf-8" /> </target> <target name="deletebin"> <delete dir="${basedir}\bin" /> </target> <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="F:\adt-bundle-windows-x86-20131030\sdk\tools\lib\ant-contrib-1.0b3.jar" /> <target name="make_channels"> <foreach target="make_one_channels" list="${market_channels}" delimiter="," param="channel"> </foreach> </target> </project><img src="https://img-my.csdn.net/uploads/201406/02/1401642364_9125.png" alt="" />
1.0
<project name="HelloWorld" default="release">
2.0
<condition property="sdk.dir" value="${env.ANDROID_SDK_HOME}">
<isset property="env.ANDROID_SDK_HOME" />
</condition>
3.0
<property name="out.unaligned.dir" value="F:\HelloWorld_${app_version}/" /> 輸出文件目錄
4.0
<property name="out.unaligned.file" location="${out.unaligned.dir}/HelloWorld_${app_version}_${channel}_android.apk"/> 輸出文件名稱
5.0
6.0 注意<taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="F:\adt-bundle-windows-x86-20131030\sdk\tools\lib\ant-contrib-1.0b3.jar" /> 指定ant-contrib-1.0b3.jar文件目錄。
<foreach target="make_one_channels" list="${market_channels}" delimiter="," param="channel">中的channel
和 AndroidManefest.xml 中的 <meta-data android:name="qudao" android:value="channel" /> channel 該值必須一樣。根據該屬性配置的
ant.properties 文件
key.alias=release_keykey.store.password=123456key.alias.password=123456market_channels=xx,yy,zzapp_version=1_0_build_0分別爲:證書文件 別名 證書密碼 別名密碼 渠道名字 版本號這裏的證書路徑 一定已/ 分割 而不是\ (我在這裏就遇到了問題
編譯
繼續執行命令:
ant make_channels
如果成功 會出現
BUILD SUCCESSFUL
Total time: 3 seconds 同時出現編譯完的文件
自己編譯時 出現的錯誤:
F:\adt-bundle-windows-x86-20131030\sdk\tools\ant\build.xml:1139: The following e
rror occurred while executing this line:
F:\adt-bundle-windows-x86-20131030\sdk\tools\ant\build.xml:1151: Value for 'keys
tore' is not valid. It must resolve to a single path
證書文件指定的路徑不對導致。通過ant release命令也會得到同樣的錯誤。