使用ANT打包Android应用1

来源:互联网 发布:碰碰聊天软件怎么样 编辑:程序博客网 时间:2024/06/18 04:29

1.文章推荐

先推荐一篇文章,关于ant 打包Android 项目的,很涨姿势的。

使用ant 打包Android 应用:http://blog.csdn.net/liuhe688/article/details/6679879


2.简单的打包脚本部署

项目中假设有三个工程:PackageAndroid1 ,和 PackageAndroid2 是两个apk ,然后appcompat_v7 项目作为lib 给这两个apk调用 。工程之间的路径如图所示。



2.1AntPackage 目录:



2.2build 目录:

2.2.1build.properties 配置打包项目共有的属性配置,比如target 目录的路径,要编译的项目的路径,签名相关的东西:

2.2.2 build.xml

打包脚本的主要流程控制文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?><project name="AndroidPackage" default="release"><property file="local.properties" /><property file="build.properties" /><target name="prepareForBuild"><echo>begin prepareForBuild...</echo><mkdir dir="${package.target.dir}" /><delete dir="${package.target.dir}" /><mkdir dir="${package.target.dir}" /></target><target name="buildToTarget"><echo>打包各个项目生成apk到target 目录下 ...</echo><ant antfile="${project.PackageAndroid1.dir}/build.xml" target="build"inheritall="false" /><ant antfile="${project.PackageAndroid2.dir}/build.xml" target="build"inheritall="false" /></target><tstamp><format pattern="yyyyMMddHHmm" property="nowtime" /></tstamp><target name="packageToRelease"><zip destfile="${package.release.dir}/release_${nowtime}.zip"><zipfileset dir="${package.target.dir}" includes="**/*.*" /></zip></target><target name="release" depends="prepareForBuild,buildToTarget, packageToRelease" /></project>

2.2.3 local.properties 基于本地设备的属性配置,sdk 路径,本地文件绝对路径等

# local var definesdk.dir=D:\\work\\android\\sdk\\sdkpackage.dir=D:\\work\\android\\workshopdabao\\PackageTest\\AntPackage

2.3 key目录:放置签名的key

2.4 target目录:临时放置编译出来的apk 文件

2.5 release目录:放置打包出来的zip文件 


3. appcompat_v7 目录:

appcompat_v7  工程作为lib 被其他工程调用,build.xml 文件和 local.properties 文件用 android update 命令生成即可 。


4.PackageAndroid1 和PackageAndroid2 工程

4.1 build.xml 

PackageAndroid1 和 PackageAndroid2 中的build.xml 基本上一样。只是project 名不一样 ,这个name 会影响最终生成的apk 名字。

<?xml version="1.0" encoding="UTF-8"?><project name="Package2">    <!-- 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" />    <property file="${package.dir}/build/build.properties" />
    <property file="${package.dir}/build/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" />        <target name = "copyToTarget"><echo>copy ${out.final.file} to ${package.target.dir}/${ant.project.name}.apk</echo><copyfile dest="${package.target.dir}/${ant.project.name}.apk" src="${out.final.file}" /></target>          <target name = "build" depends = "release ,copyToTarget"></target></project>

4.2 local.properties

配置package相对路径,package.dir=../AntPackage


每个文件中各种命令的介绍,下一篇文章继续,to be continued...








0 0
原创粉丝点击