Android项目利用Ant实现打包功能

在Android项目的开发中,项目完成后打包发布是必不可少的一个步骤,针对企业内部APP,一般不需要多渠道发布,因此只要使用Android自带的ADT来发布APK就可以了。但是每次发布的时候还要输入密码、选择生成文件的的存储路径,这个如果只做一两次还行,如果每次都是用这种方法,那么感觉就比较繁琐了,因此这里我们就介绍使用Ant实现指定位置的打包的介绍。

一、安装和配置ant

使用Ant打包,那么我们的电脑上肯定要安装和配置ant,下面简要说一下ant的配置。

1、下载ant到官方网站:http://ant.apache.org/

将下载的zip保解压到指定目录,我的目录是D:\JavaEnv\apache-ant-1.9.6。

配置环境变量:在系统变量中新建变量名ANT_HOME,并指定变量值D:\JavaEnv\apache-ant-1.9.6,然后点击确定。如下图所示:


然后再将ant的bin和lib文件夹路径加入到Path中,如下图所示:


验证ant是否配置成功,打开CMD,输入指令:ant -version并回车,如下图所示即表示配置成功:

二、生成build.xml文件

打开CMD,通过输入以下命令进入需要打包的项目的根目录下,即AnroidMainfest.xml文件所在的目录,如图:

进入项目根目录以后输入指令:android update project -n 项目名称 -p . (参数-n后面接项目名称、参数-p 后面的点表示当前目录),然后回车生成build.xml文件:

查看Eclipse中项目根目录中的生成的文件,如图:


打开build.xml文件,并在build.xml文件中加入以下代码:

<property name="aapt.ignore.assets" value="<dir>crunch:!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~" />
    
	<!-- 导入自定义属性文件 -->
<property file="build.properties" />
    
<!-- 发布渠道名称 -->
	<property name="channelname" value="xxxx_android" />
	
	<!-- 发布渠道key -->
	<property name="channelkey" value="0000000" />
	
	<!-- APK输出目录 -->
<!-- <property name="out.final.file" location="${apk.out.dir}/${project.name}_${channelname}_${project.version}.apk" /> -->
	<property name="out.final.file" location="${apk.out.dir}/${project.name}${project.version}.apk" />

整个build.xml文件的如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Winsafe.MobilePhone.Wxdew.Dev" default="help">
    <property name="aapt.ignore.assets" value="<dir>crunch:!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~" />
    
	<!-- 导入自定义属性文件 -->
    <property file="build.properties" />
    
    <!-- 发布渠道名称 -->
	<property name="channelname" value="xxxx_android" />
	
	<!-- 发布渠道key -->
	<property name="channelkey" value="<span style="font-family: Arial, Helvetica, sans-serif;">0000000</span>" />
	
	<!-- APK输出目录 -->
	<!-- <property name="out.final.file" location="${apk.out.dir}/${project.name}_${channelname}_${project.version}.apk" /> -->
	<property name="out.final.file" location="${apk.out.dir}/${project.name}${project.version}.apk" />

    <!-- 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>

三、建立build.properties文件,并配置如下内容

在build.xml文件中有着一句代码:

    <property file="build.properties" /></span>

所以我么在项目的根目录下面建一个build.properties文件,文件内容如下:

#JDK目录
java.dir=D:\\Program Files (x86)\\Java\\jdk1.8.0_25

#工程名称
project.name=<span style="font-family: Arial, Helvetica, sans-serif;">xxxx</span>
#工程目录
project.dir=E:\\Projects\\JavaRoot\\Android\\xxxx.Dev
#APK输出目录
apk.out.dir=E:\\Projects\\ReleaseRoot\\MobieClient\\xxxx
#应用程序版本
project.version=1.0.9

#签名文件位置
key.store=E:\\Projects\\ReleaseRoot\\MobieClient\\android.keystore
#签名文件中的别名
key.alias=android
#签名文件密码
key.store.password=000000
#签名文件中别名的密码
key.alias.password=000000


四、打包

打开CMD,输入指令:ant release,如下图所示:

表示打包成功。


至此我们就完成了利用ant打包的整个过程。


备注:如果你的项目中用到了第三方的类库项目,那么你需要重复第二步的操作,为类库项目类库也生成一个build.xml文件,同时加入下面的代码:

    <!-- 需要忽略的东东 -->
    <property name="aapt.ignore.assets" value="<dir>crunch:!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~" />
然后再通过第四步打包即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值