Android 多渠道自动打包

来源:互联网 发布:武汉网络诈骗团伙名单 编辑:程序博客网 时间:2024/05/02 14:10

首先确保你已经配置好了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创建

             在命令行下进入工程所在目录 例如:
             cd F:\workspace\HelloWorld>

            首先 需要 在AndroidManafesyt.xml 文件中天加
           <meta-data  android:name="qudao"  android:value="channel" /> 注意里面的channe会被最终的渠道l替换掉。

     执行 android update project -p F:\workspace\HelloWorld -t 2
             -p 指定工程路径 这里必须是全路径 -t 指定编译的目标SDK 参数由android list targets命令给出  我的SDK只有两个(下面为执行结果)我选android-19 也就是ID为2的。所以参数是2.
             F:\workspace\HelloWorld>android list targets
Available Android targets:
----------
id: 1 or "android-10"
     Name: Android 2.3.3
     Type: Platform
     API level: 10
     Revision: 2
     Skins:
     ABIs : no ABIs.
----------
id: 2 or "android-19"
     Name: Android 4.4.2
     Type: Platform
     API level: 19
     Revision: 2
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W
XGA720, WXGA800, WXGA800-7in
     ABIs : armeabi-v7a
             之后,工程会多出两个文件build.xml local.properties
             build.xml内容如下:
             
<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="http://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       

<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文件目录。

6.0        注意

<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 文件              

             在eclipse中工程里创建ant.properties 文件 在里面添加
             key.store=F:/workspace/release_key                  
key.alias=release_key
key.store.password=123456
key.alias.password=123456
market_channels=xx,yy,zz
app_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命令也会得到同样的错误。 

            

            
0 0
原创粉丝点击