记得去年自己写过一个ant脚本,但是在android4.0以后的sdk里那个脚本就失效了,主要是因为 apkbuilder这个程序不见了;

人家sdk升级,我们的脚本也要跟上趟,修改一下喽。


上网一查,大家的文章还停留在我去年的脚本程度,算了,自己动手查阅了资料之后,具体实现如下:


在工程的根目录 创建2个文件,分别:

1、build.xml

2、build.properties


build.xml的内容:

<?xml version="1.0" encoding="UTF-8"?><project name="autoDeployAPK" default="deploy"><!-- 使用第三方的ant包,使ant支持for循环--><taskdef resource="net/sf/antcontrib/antcontrib.properties">    <classpath><pathelement location="lib/ant-contrib-1.0b3.jar"/>    </classpath></taskdef><property file="build.properties" />    <!-- 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" />         -->    <property name="jar.libs.dir" value="${jar.libs.dir}" />    <!-- 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.dir}/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"/><property name="channelname" value="pateo" /><property name="channelkey" value="12347" /><!--循环打包 --><target name="deploy">      <foreach target="modify_manifest" list="${channel.list}" param="nameandchannel" delimiter=","></foreach></target><target name="modify_manifest">  <!-- 获取渠道名字 --><propertyregex override="true" property="channelname" input="${nameandchannel}" regexp="(.*):" select="\1"/><!-- 获取渠道号码 --><propertyregex override="true" property="channelkey" input="${nameandchannel}" regexp=":(.*)" select="\1"/><!-- 正则匹配替换渠道号 <replaceregexp flags="g" byline="false" encoding="UTF-8">  <regexp pattern='meta-data android:name="CHANNEL" android:value="(.*)"' /><substitution expression='meta-data android:name="CHANNEL" android:value="${channelkey}"' />  <fileset dir="" includes="AndroidManifest.xml" />  </replaceregexp>--><property name="out.final.file"                    location="${apk.out.dir}/${project.name}_${channelname}_${project.version}.apk" /><antcall target="release" /></target><!-- 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>    /* 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} */    <target name="-post-compile">    </target>--><!--如果项目包含了jni代码,希望在打包时自动重新编译so库,可以修改build.xml文件。修改方法为,在引用sdk的build.xml文件之前添加如下target:--><!--<target name="-pre-build" depends="-ndk-build"></target><target name="-ndk-build">    <exec executable="ndk-build" failonerror="true">        <arg value="clean" />    </exec>    <exec executable="ndk-build" failonerror="true" /></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: 导入anroid sdk 默认的ant写好的build.xml -->    <import file="${sdk.dir}/tools/ant/build.xml" /></project>

这个文件主要就是打包的指令,其中大家不难看出
 <import file="${sdk.dir}/tools/ant/build.xml" />

自动引用并且执行了你的sdk目录下tools/ant/build.xml,大家有精力可以自己看看这个文件.


build.properties的内容:

#Save#Wed Oct 23 15:47:27 CST 2013project.name=MOBILE_ANDROID_PROJECTjar.libs.dir=libsbuild.first=falsekey.alias=maomaokey.alias.password=maomaochannel.list=test1\:100411-f91d7ad6c99688b587b299d2c507679d,test2\:100411-f91d7ad6c99688b587b299d2c507679djava.dir=C\:\\Program Files (x86)\\Java\\jdk1.6.0_10key.store=C\:\\changeself\\test.keystoreproject.dir=C\:\\changeself\\project_androidsdk.dir=C\:\\changeself\\adt-bundle-windows-x86-20130729\\sdkproject.version=1.0.3key.store.password=maomaoapk.out.dir=apk

这个文件里面,

project.name 就是你的工程名,其实最后体现在APK的名字

key.alias和key.alias.password是你的 签名文件里的东西,自己先生成去

channel.list就是你的渠道名字,支持多个渠道,test1和test2,......testn,自己按照格式去填写,你写了几个渠道就会生成几个apk

java.dir 自己的java本机的安装目录

key.store 自己签名文件的本机存放目录

project.dir 你的工程本机存放目录

sdk.dir 你的android sdk本机存放目录

project.version 工程的版本号,最后也会体现在apk的名字里

key.store.password

apk.out.dir 在你的工程根目录存放apk生成的目录


ok,配置好上述2个文件后,在cmd命令行下,键入你的工程目录,执行 ant命令


最后会在你的apk.out.dir生成你需要的apk文件


大家如果有何疑问可以关注我的微博:http://weibo.com/changeself 跟我交流








更多相关文章

  1. Android工程 引用另外一个Android工程
  2. Android 事件全局监听(二)需要root权限 ,使用getevent监听Android输
  3. Android工程 引用另外一个Android工程(zz)
  4. android 依赖工程 Android 工程引用其他Library工程
  5. android 调用系统文件管理器
  6. [Android Studio 权威教程]打包、生成jks密钥、签名Apk、多渠道打

随机推荐

  1. Android多渠道SDK开发心得(5)——多渠道s
  2. android在物联网的应用
  3. android使用include调用内部组件的方法
  4. Android emulator 常用快捷键
  5. android 判断应用程序是否已安装(附带常用
  6. Android使用Ant自动编译签名打包详解
  7. android 控件跟随手指移动
  8. Android发送HTTP POST请求示范
  9. Android 进阶的小技巧整理(整理自第一行代
  10. Android(安卓)View中的setMeasuredDimens