必要的java、android、ant文件及循环打包用到的ant的jar

下载Ant(这里的Ant不是eclipse和android SDk里面自带的ant)
     官方下载地址:http://ant.apache.org/
循环打包用jar  ant-contrib-1.0b3.jar  FQ进   http://ant-contrib.sourceforge.net/   1.03b里的ant-contrib-1.0b3-bin.zip   放到ant的lib目录下
至于java、android的不解释,干这活告诉我没环境?
 

配置android、java、ant的path

如果ant要求的java版本不符

可修改注册表(当时没修改,一般没问题)将java的path放在system32之前,并将java中的java、javax、javaw三个文件复制替换system32中对应的三个文件,再次运行ant -version则ant正常

使用ant编译必须让所有编译中的项目(包括作为lib的项目)中存在ant环境文件(build.xml等)使用android update project(lib-project) -p 文件绝对路径 -t SDK版本号(版本号可使用android list target命令查看API等级在本地对应的版本号)进行初始化

 

使用ant编译需要项目中有ant.properties文件作为ant编译时的配置文件,其中主要规定了build.xml中需要用到的一些配置如输出路径、Keystore信息、渠道代号等。

通过直接新建文本文档,更改文件名称及后缀名来创建ant.properties文件即可,之后在文件中写入各种信息。

--------------------------------------------------------------------ant.properties     start---------------------------------------------------------------

# the config file for batch package.
application.package=com.weixueji.app                              包名
ant.project.name=tol-android                                          项目名
java.encoding=utf-8                                                       编码格式

out.absolute.dir=C:/compile                                            输出文件的绝对路径(中转)
gos.path=e:/Users/weishiji/Desktop/apks                          最终打包文件的绝对路径

key.store=D:/tol-android/keystore/weixueji.keystore          证书位置
key.store.password=123456                                            证书密码
key.alias=wxj                                                                 用到的证书别称(eclipse里就是第二次选的那个)
key.alias.password=123456                                             别称密码


app_version=2.1                                                            版本号
market_channels=1,2                                                     渠道名(必须在同一行,用英文逗号分隔)

--------------------------------------------------------------------ant.properties          end---------------------------------------------------------------

以下build代码 注意替换黑字部分 其中 BaiduMobAd_CHANNEL 是渠道代码,因为我的项目中有百度统计,所以这里不一样,这里有些特殊说明后面马上讲到。

<?xml version="1.0" encoding="UTF-8"?>
<project name="项目名称" 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 -->
 <taskdef resource="net/sf/antcontrib/antcontrib.properties">
 <classpath>
   <pathelement location="D:/apache-ant-1.9.4-bin/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar"/>  
 </classpath>
</taskdef> 
<property name="aapt.ignore.assets" value="!.svn:!.git:\x3Cdir\x3E_*:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~:crunch" />
<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 flags="g" byline="false">  
   <regexp pattern="android:name=&quot;BaiduMobAd_CHANNEL&quot; android:value=&quot;(.*)&quot;" />  
   <substitution expression="android:name=&quot;BaiduMobAd_CHANNEL&quot; android:value=&quot;${channel}&quot;" />  
   <fileset dir="" includes="AndroidManifest.xml" />  
   </replaceregexp>
   <antcall target="clean" />
   <antcall target="release"/>
    <copy tofile="${gos.path}/打包后的名称前缀${channel}.apk">
        <fileset dir="${out.absolute.dir}/" includes="打包后的名称前缀-release.apk" />
    </copy>
    <delete includeEmptyDirs="true">  
        <fileset dir="${out.absolute.dir}" includes="**/*"/>           
    </delete>
    <echo message="==========================="/>
</target> 
</project>

以上为项目的build.xml文件

 

刚刚说过渠道id

下面讲下到底要换成什么

项目中有<meta-data android:name="BaiduMobAd_CHANNEL" android:value="yingyonghui" />来区别不同的渠道

注意由于ant循环打包的时候会用正则去匹配并替换这段值所以一定要保证AndroidManifest里这段代码为同一行 并且BaiduMobAd_CHANNEL处与build中的BaiduMobAd_CHANNEL对应

当然如果你能自己理解并写好这段替换代码可以自己去写,我是个渣渣还是用查好的吧。

 

下面就可以再cmd中进入到项目根目录 执行编译   ant deploy

报错什么 几  字节  utf-8 未识别啥的 可能是由于ant在循环打包时会读写AndroidManifest文件,所以如果其中有中文(包括注释)便会出问题,去掉中文注释即可。

报错引用的项目build可能是没有update的原因,使用android 的update命令去让他自动生成对应的build文件后再次执行编译

<property name="aapt.ignore.assets" value="!.svn:!.git:\x3Cdir\x3E_*:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~:crunch" />

当初没有加这行代码就报错sdk中的ant build 第六百一十几行?有问题,在项目的build文件中加入即可。原理不造~有造的请指教。

 

暂时就能记住这么多了。