Ant打包APK

来源:互联网 发布:标准型矩阵是什么 编辑:程序博客网 时间:2024/06/04 00:44

环境背景:

系统:ubuntu 14.10

Eclipse:4.4.1

ADT:23.4

Ant:1.9.4


1.首先当然还是先去安装ant。网上有很多教程,这里就不废话了,自己去下载。安装完毕还需要配置环境变量。


2.在终端(windows下cmd)打开到android sdk的tools目录,例如我的项目是在/home/arjinmc/Example,那么在终端中输入 

android update project -p /home/arjinmc/Example -t "android-17"  

//这里的t是target 相对于项目中的target sdk的版本号,我这里的是4.2.2,所以是要写android-17

此时就会生成出来两个文件,一个是build.xml,一个是local.properties

如果你的项目还引用到了别的项目library,那么也需要在这些项目生成一下ant必要的文件。


3.需要在自己的项目目录中新建一个文件叫ant.properties,在里面填写必要的内容,这个就是我们用来生成签名包,渠道等必要的配置

key.store=/home/eminem/my.keystore
key.alias=com.arjinmc.example
key.store.password=111111
key.alias.password=111111

然后保存。


4.在终端打开到自己的项目的绝对路径,然后执行ant release

此时有可能会出现类似这样的错误:

>      [aapt] invalid resource directory name: C:\Users\Paul\git\XXXXXXXX\bin\res/crunch> > BUILD FAILED> C:\Android\android-sdk\tools\ant\build.xml:601: The following error occurred while executing this line:> C:\Android\android-sdk\tools\ant\build.xml:653: The following error occurred while executing this line:> C:\Android\android-sdk\tools\ant\build.xml:698: null returned: 1
这个是因为android sdk 的ant 和我们自己安装的ant两个ant都生成了用于aapt的crunch配置,导致ant会不知道到底用哪一个。如果遇到这中情况,打包的时候只要执行
ant clean release
就可以顺利打包了。但是打包完毕刷新,发现eclipse会出现项目引用的libirary项目报错的信息,不要慌。其实只要重新启动eclipse这些丢失的jar包就会重新回来。
我在网上搜了很多方法,但是都不管用。所以,使用ant打包完了,直接重启eclipse就可以了。
多渠道打包
在Manifest的<application>内添加
   <meta-data             android:name="UMENG_CHANNEL"            android:value="${channel}"/>
在build.xml 加上
 <!-- 渠道包打包脚本  ant deploy-->    <taskdef resource="net/sf/antcontrib/antcontrib.properties">      <classpath>          <pathelement location="/usr/share/ant/lib/ant-contrib-1.0b3.jar"/>      </classpath>  </taskdef>    <target name="deploy">     <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">          </foreach>  </target>  <target name="modify_manifest">      <replaceregexp flags="g" byline="false">          <!-- 匹配的内容是 android:value="*****" android:name="UMENG_CHANNEL" -->          <regexp pattern='android:value="(.*)" android:name="UMENG_CHANNEL"' />          <!-- 匹配之后将其替换为 android:value="渠道名" android:name="UMENG_CHANNEL" -->          <substitution expression='android:value="${channel}" android:name="UMENG_CHANNEL"' />         <!-- 正则表达式需要匹配的文件为AndroidManifest.xml -->           <fileset dir="" includes="AndroidManifest.xml" />      </replaceregexp>      <property name="out.release.file" location="${out.absolute.dir}/${ant.project.name}_${channel}.apk" />       <!--包 -->       <antcall target="release" />       <!--输出渠道包到bin/out目录下 -->      <copy tofile="${out.absolute.dir}/out/${ant.project.name}v${version}-${channel}.apk" file="bin/${ant.project.name}-release.apk"/>  </target>
在ant.properties中添加一些渠道信息和版本号
market_channels=360,wandoujia,baiduversion=1.2.1
在终端输入ant deploy,有可能会出现缺少ant-contrib-1.0b3.jar,需要下载一个。各大网络有。下载完了放在ant的安装目录的下:linux下是lib目录,windows下是bin目录。

如果出现类似下面内容:
/home/arjinmc/software/android-sdk-linux/tools/ant/build.xml:1121: if doesn't support the "condition" attribute
需要修改ant-contrib-1.0b3.jar的的配置,依次打开net/sf/antcontrib/antcontrib.properties,找到if=net.sf.antcontrib.logic.IfTask,用#注释掉。然后保存。


0 0
原创粉丝点击