Android使用Ant批量打包

1.配置java环境变量

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

2.下载Ant(这里的Ant不是Eclipse和Android SDK 里面自带的ant)

官方下载网址:http://ant.apache.org/

下载.zip即可

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能轻松解决这个问题。下载ant-contrib-1.0b3.jar放到Ant的lib文件夹即可。直接百度搜索ant-contrib-1.0b3.jar。

Ant批量打包中比较重要的三个文件:build.xml,ant.properties,local.properties

这三个文件要放在Android工程根目录下。

1.build.xml

<?xml version="1.0" encoding="UTF-8"?><!-- 项目名称XXXX,可用全局替换为当前项目名称 --><project    name="XXXX"    default="deploy" >    <!--         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:/ant/lib/ant-contrib-1.0b3.jar</span>" />        </classpath>    </taskdef>    <import file="${sdk.dir}/tools/ant/build.xml" />    <target name="deploy" >        <foreach            delimiter=","            list="${market_channels}"            param="channel"            target="modify_manifest" >        </foreach>    </target>    <target name="modify_manifest" >        <replaceregexp            byline="false"            flags="g" >            <regexp pattern="android:name="UMENG_CHANNEL" android:value="(.*)"" />            <substitution expression="android:name="UMENG_CHANNEL" android:value="${channel}"" />            <fileset                dir=""                includes="AndroidManifest.xml" />        </replaceregexp>                <antcall target="release" />        <copy tofile="${gos.path}/XXXX_${channel}.apk" >            <fileset                dir="${out.absolute.dir}/"                includes="XXXX-release.apk" />        </copy>        <delete includeEmptyDirs="true" >            <fileset                dir="${out.absolute.dir}"                includes="**/*" />        </delete>        <echo message="=======Build Success!!!====================" />    </target></project></span>


解释下:红色标注部分以自己的Ant安装路径为准!

2.ant.properties

application.package= //项目包名
ant.project.name=//项目名(同build.xml)
java.encoding=UTF-8 //项目编码

out.absolute.dir=d:/apk/compile //打包时生成临时文件存放地,最终build会自己清空
gos.path=d:/apk //最终apk存放文件夹

key.store=//签名文件位置
key.store.password=//签名密码

key.alias=//签名别名(生成keystore时的单位名)

key.alias.password=//别名密码

app_version=//apk版本

market_channels=huawei,Google,Gfan,AnZhi,MuMayi //apk渠道(逗号分隔 )

扫盲一下:如何生成keystore

偷一个懒用Eclipse IDE来生成keystore,当然你也可以用命令行生成,自己随意吧。

2.1在Eclipse中右键工程

2.2选择Export Sign Application Package...点击Next

2.3选择create new keystore,Browser选择你要生成的key的路径

设置密码,点击Next

2.4 填写你的Alias 别名和密码 点击Next生成keystore

对应ant.properties中的各个字段,仔细二认真的核对吧

3.local.properties

# This file is automatically generated by Android Tools.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must *NOT* be checked into Version Control Systems,# as it contains information specific to your local configuration.# location of the SDK. This is only used by Ant# For customization when using a Version Control System, please read the# header note.sdk.dir=D:\\adt-bundle-windows\\sdk

解释一下:红色标注部分填写你自己的sdk目录

4.最后一步修改AnroidManifest.xml文件

 <meta-data            android:name="UMENG_APPKEY"            android:value="53bf9d4556240b753100a99d" /> <meta-data android:name="UMENG_CHANNEL" android:value="MuMayi"/>


红色标注部分必须在一行

以上这一行必须在同一行内,决不能换行,这是由于在build.xml做了如下限定

<replaceregexp            byline="false"            flags="g" >            <regexp pattern="android:name="UMENG_CHANNEL" android:value="(.*)"" />            <substitution expression="android:name="UMENG_CHANNEL" android:value="${channel}"" />            <fileset                dir=""                includes="AndroidManifest.xml" />        </replaceregexp></span>


AnroidManifest.xml文件中如果有<!-- -->中文的注释,建议你删除AnroidManifest.xml中的注释,或者换成英文注释,否则你循环打包时,当打完一个包后抛出UTF-8编码错误。原因就是因为注释的原因。切记!

5.命令行下,cd到自己要打包的Android工程的目录,ant deploy,观察cmd命令行打印信息,有错误调试后再次运行该命令。如果批量打包成功后会打印出build success!!!

6.当你打包好后,在用反编译工具,AndroidBy或者ApkTool等反编译工具查看打包后的AnroidManifest.xml文件中的渠道值是否变化?如果变化了再将打包后的APK安装在手机上看是否能正常安装?,如果都是肯定的答案,那么恭喜你,你成功了!!!

更多相关文章

  1. 反射类查看类下面的所有方法及变量
  2. android NDK 入门之在JNI中修改java中对象的成员变量的值
  3. android工具-annotations
  4. 如何配置adb
  5. 学习android JNI的那些事儿--------4. Field & Method --> Acces
  6. 如何在Android项目中配置BuildConfig常量
  7. Android开发准备——Java和Android(安卓)Studio环境搭建
  8. Android(安卓)OpenCV:读取Matlab生成的.mat文件并转换成OpenCV的M
  9. 一键多渠道打包 只需1秒

随机推荐

  1. Android(安卓)viewPager实现翻动
  2. Android系统目录结构详解
  3. Android(安卓)Platform 3.0 SDK和Eclipse
  4. android 小游戏 ---- 数独(三)
  5. Android 控制WIFI相关操作
  6. Android之消息处理机制(二)Handler的本质-M
  7. [置顶] Android应用程序的自动更新升级(自
  8. android的Surface Flinger服务启动分析
  9. Android(安卓)之 串口通信
  10. 收藏-------------Android应用程序组件Co