使用Ant批量打包Android项目

来源:互联网 发布:侠客风云传内存优化 编辑:程序博客网 时间:2024/06/10 00:07

参考:http://blog.sina.com.cn/s/blog_74c22b21010173f8.html


补充遇到的问题:没有先clean项目就去ant打包,在proguard.cfg或者proguard-project.txt文件中引入了找不到的jar包导致can`t read *.jar无法打包

ant打包其实不难,看里面的错误注释,然后查找就可以

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>

Windows环境下:

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



Mac环境下:

6. 配置local.properties
    sdk.dir=/opt/adt-bundle-mac/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

可以直接使用Eclipse的ant打包项目

http://blog.csdn.net/gf771115/article/details/19478383


关于ant引用android第三方工程打包的问题

为了帮助网友解决“求教关于ant引用android第三方工”相关的问题,中国学网通过互联网对“求教关于ant引用android第三方工”相关的解决方案进行了整理,用户详细问题包括:使用ant工具:A工程想调用B工程中src下的类来进行打包,  注:不是把B工程编译成jar包,而是在A工程中的built.xml文件中指定B工程中src的路劲来打包,那位有经验的高手能给点指引吗?或者demo  或者思路!!谢谢了!第一次做这个,头有点大!!,具体解决方案如下:

解决方案1:
哥们,你的问题解决了没啊?  我也遇到这个问题了

解决方案2:

把B项目设置为Lib:
project->properties->Android->Library->Is Library  这个勾选上。
在A项目中把B项目添加进去:
project->properties->Android->Library->Add..,,里面把B项目选上。

这样之后default.properties里面就会有
android.library.reference.3=../XXXX.XXX.XXXX
的项。

这样就可以编译了,编译的时候会把B项目的代码也一起编译。

如果B项目不作为一个lib来处理,那也就没办法了。

解决方案3:
但是现在是想使用ant来打包,所以buid.xml应该怎么写

解决方案4:

引用 2 楼 hslinux 的回复:
把B项目设置为Lib:
project->properties->Android->Library->Is Library 这个勾选上。
在A项目中把B项目添加进去:
project->properties->Android->Library->Add..,,里面把B项目选上。

这样之后default.properties里面就会有
android.library.reference……

你的方法我早用过 是可以的 但不是我们需要的  还是谢谢你!!我问题解决了!   

解决方案5:
怎么解决的?能否分享下?

解决方案6:
晕。。怎么解决的都不分享下啊。。。

解决方案7:
不知道语言怎么表达  有需要加我Q626473306沟通吧

解决方案8:

   <!-- Compile this project's .java files into .class files. -->      <target name="compile" depends="dirs, resource-src, aidl">           <echo>libs_classpath..${basedir}/libs/</echo>         <javac encoding="UTF-8" target="1.6"  extdirs="" srcdir="."             destdir="${outdir-classes}" bootclasspath="${android-jar}"             includeantruntime="on" source="1.6">         <compilerarg value="-Xlint" /><!--          第三方的工程 -->         <src path="${third_lib}/src" />    <src path="src" />    <src path="gen" /><!--     第三方工程里的导入包 --><!--     <classpath refid="libs_pro"/>  -->         <classpath refid="libs_classpath"/>                   <src path="${third_lib_2}/src" />    <src path="src" />    <src path="gen" />    <classpath refid="libs_pro_2"/>             </javac>         <echo>libs_classpath..${libs_classpath}</echo>     </target>  


希望能帮助到你们,但我还是遇到个问题,这个方法可以加载第三方工程的src但无法加载第三方工程的res资源和自定义view的自定义属性,如果有用到就会编译不通过。苦恼呀...

解决方案9:

<!--            第三方工程里的导入包 --><path id="libs_pro">              <fileset dir="${third_lib}/libs/">                         <include name="*.jar"/>                     </fileset>         </path>

解决方案10:
MD,老子最烦这种人,十万火急的问,问题解决了,又不分享

解决方案11:
lz  求分享啊!

解决方案12:
我也是这问题 , 加入引用工程之后

-code-gen:[mergemanifest] Found Deleted Target File[mergemanifest] Merging AndroidManifest files into one.[mergemanifest] Manifest merger disabled. Using project manifest only.     [echo] Handling aidl files...     [aidl] No AIDL files to compile.     [echo] ----------     [echo] Handling RenderScript files...     [echo] ----------     [echo] Handling Resources...     [aapt] Generating resource IDs...     [aapt] invalid resource directory name: F:\workspace\Zlib\bin\res/crunchBUILD FAILEDD:\Android\sdk\tools\ant\build.xml:601: The following error occurred while executing this line:D:\Android\sdk\tools\ant\build.xml:653: The following error occurred while executing this line:D:\Android\sdk\tools\ant\build.xml:698: null returned: 1


这个路径老不对,怎么改啊

解决方案13:
601 653 698 同求

解决方案14:
现在也也遇到这个问题了,求解决方案

通过对数据库的索引,我们还为您准备了:

android用ant编译,怎么加入第三方包 

问:我的android.jar包是自己写的,用eclipse打包没问题,然后我就用android...

答:你修改过以后,需要重新覆盖你原来的build.xml,至少我们的android项目里,每次修改后,都需要重新生成这个配置文件。

===========================================

android工程中添加了 preject library第三方包(是... 

问:我的android.jar包是自己写的,用eclipse打包没问题,然后我就用android...

答:android工程A关联了另一工程B(可以在build path -->Link Source去关联) 用ant的时候,方法与单独build一个工程师没多大的区别 先把B工程编译打成jar包(可用Eclipse直接Export出jar包),然后放到A工程下的libs中去 再用ant去buildA工程即可~

===========================================

android如何使用ant批量打包 

问:我的android.jar包是自己写的,用eclipse打包没问题,然后我就用android...

答:ps :后期熟悉ant的话,可以使用纯ant脚本或者使用另一种更好的自动化打包工具(maven)关键代码如下:package com.cn.ant; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import...

===========================================

用ant脚本编译引用了第三方包的java类 

问:现在我有一个java类,一个jar包放在一个文件夹里,在文件夹外面是build.xm...

答:此文件为myproperties.xml 在build.xml中加入:

===========================================

用java或C#调用命令行对android工程进行编译、打包... 

问:现在我有一个java类,一个jar包放在一个文件夹里,在文件夹外面是build.xm...

答:Android本身是支持ant打包项目的,并且SDK中自带一个build.xml文件。 通过该文件,可以对文件进行编译、打包、安装等。并且支持多种方式打包,如debug或者release。 一般的,可以按照如下方法进行操作: 首先创建一个Android工程。 工程创建好后...

===========================================

android ant 是什么 

问:现在我有一个java类,一个jar包放在一个文件夹里,在文件夹外面是build.xm...

答:android的编译打包工具

===========================================

关于android开发中引用第三方jar的问题 

问:我自己建了一个java的jar文件(只有一个类abc)非常简单就3个函数(publi...

答:能new就说明jar包已经成功导入,就应该能用,请确认你的类里面的函数是public的。

===========================================

android ant 是什么 

问:我自己建了一个java的jar文件(只有一个类abc)非常简单就3个函数(publi...

答:android的编译打包工具

===========================================

android 源码环境下如何引用第三方jar中的资源。 

问:我在第三方应用中用可以,在源码环境只可以调用jar的类方法,引用资源就...

答:1、把引用的jar包放到项目的libs文件夹 2、展开libs下jar包的的右键菜单, build path -- add to build path 这样,在生成项目的时候就会自动引用libs下的jar包

===========================================
0 0
原创粉丝点击