Android批量打包教程

2 篇文章 0 订阅

转载:http://blog.sina.com.cn/s/blog_74c22b21010173f8.html


1.配置JAVA的环境变量
     (参考http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef .html),

     很多Java程序员由于使用Eclipse不配置Java环境变量也能正常运行代码。但是如果想使用Ant命令批量打包本步骤必不可少。

2.   下载Ant(这里的Ant不是eclipseandroid SDk里面自带的ant
     官方下载地址: http://ant.apache.org/

3. 解压Ant并配置环境变量
   a) 解压Ant,比如解压到D:\ant
   b) 我的电脑->属性->高级->环境变量
 c) 系统变量新建ANT_HOME,变量值为d:\ant
 d) 系统变量新建或修改PATH:将%ANT_HOME%\bin;%ANT_HOME%\lib添加到环境变量的PATH中 (注意以上
路径均用反斜杠)

4. 验证ant配置是否正确
   在控制台输入Cmd 回车, ant 回车,如果出现:
     Buildfile: build.xml does not exist!
     Build failed
     恭喜你已经ant配置成功!!

       Ant批量打包的基本思想是,每次打包后自动替换渠道号,然后再次打包从而实现多渠道打包的目的。
但是Ant不支持循环,怎样循环打包? 扩展包Ant-contrib能轻松解决这个问题.可以翻墙的同学可以到http://ant-contrib.sourceforge.net/自行下载,下载后直接把ant-contrib-1.0b3.jar放到Ant的lib文件夹即可.

5. 编写build.xml
  将以下带有颜色的字体(包括路径,项目名称)都改成正确的名称
<?xml version="1.0" encoding="UTF-8"?>
<project name="ThumbPlay" 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" />

    <!-- 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 an env var"
            unless="sdk.dir"
    />


<!-- extension targets. Uncomment the ones where you want to do custom work
     in between standard targets -->
<!--
    <target name="-pre-build">
    </target>
    <target name="-pre-compile">
    </target>

   
    <target name="-post-compile">
    </target>
-->

    <!-- 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 -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
 <classpath>
   <pathelement location="D:/androidDev/batch-package-tool/ant1.8.3/lib/ant-contrib-1.0b3.jar"/>  
 </classpath>
</taskdef>
<import file="${sdk.dir}/tools/ant/build.xml" />




<target name="deploy">
  <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">  
  </foreach>  
</target>

<target name="modify_manifest">
<!--<replaceregexp file="AndroidManifest.xml" encoding="utf-8" match="android:value=&quot;(.*)&quot;" replace=""/>-->
   <replaceregexp flags="g" byline="false">  
   <regexp pattern="android:name=&quot;UMENG_CHANNEL&quot; android:value=&quot;(.*)&quot;" />  
   <substitution expression="android:name=&quot;UMENG_CHANNEL&quot; android:value=&quot;${channel}&quot;" />  
   <fileset dir="" includes="AndroidManifest.xml" />  
   </replaceregexp>
<!--<property  name="out.release.file" value="${out.absolute.dir}/${channel}.apk"/>-->
   <antcall target="release"/>
<copy tofile="${gos.path}/ThumbPlay_${channel}.apk">
    <fileset dir="${out.absolute.dir}/" includes="ThumbPlay-release.apk" />
</copy>
<delete includeEmptyDirs="true">  
   <fileset dir="${out.absolute.dir}" includes="**/*"/>           
</delete>
<echo message="==========================="/>
</target>
</project>

6. 配置local.properties
    sdk.dir=D:\\androidDev\\android-sdk 改成你的SDK所在的目录,注意转义字符

7. 配置ant.properties
# the config file for batch package.
application.package=com.leyou.thumb                 (你的 apk 文件的包名)
ant.project.name=ThumbPlay                              (你的 apk 文件的工程名)
java.encoding=utf-8

out.absolute.dir=C:/compile
gos.path=Z:/app-version/test                                            (打好的渠道包要放到的目的位置)

key.store=D:/androidApk/thumbplay/thumbplay.keystore        (keystore 文件路径 )
key.store.password=wushenshiji999                         (keystore 文件路径 )
key.alias=muzhigame                                                         keystore 文件别名)
key.alias.password=wushenshiji999                                    keystore 文件别名密码)


app_version=1.0.4                                                            (要打的渠道包的版本名称)
market_channels=guanwang,shuihu,wushen,shenhua,huawei      (渠道名称,要以逗号分隔,必须在一行内)

8. 最后一步,修改AndroidManifest.xml文件:

成功:Android批量打包教程
 
 以上这一行必须在同一行内,决不能换行,这是由于在build.xml做了如下限定

成功:Android批量打包教程

9.生成渠道包 
   a)进入工程根目录,我的为: D:\apps\workspace\ThumbPlay
     注意这里必须要去工程根目录,因为Ant命令运行需要找到工程根目录下的build.xml
    b)cmd输入命令:ant deploy 第一次运行或许需要的时间要长些,我的为大约2分50秒
    若控制台最后出现Build Success,说明打包成功
    若 控制台最后出现Build Failed,查看详细信息,找出错误所在,修改它,然后重新运行命令: ant deploy

备注:

再增加一些看到的优秀博文:
ANT英文官方教程 http://ant.apache.org/manual/index.html

有些同志还将 jenkins + ant(jenkins就是一个计划任务)联合起来使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值