[转]使用ant让Android自动打包的build.xml,自动生成签名的apk文件(支持android4.0以上的版本)

在android4.0以后的sdk里那个脚本就失效了,主要是因为 apkbuilder这个程序不见了;

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

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

先输一下生成key的命令

keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore android.keystore

输入keystore密码:
再次输入新密码:
您的名字与姓氏是什么?
[Unknown]: Pelephone
您的组织单位名称是什么?
[Unknown]: Pelephone
您的组织名称是什么?
[Unknown]: Pelephone
您所在的城市或区域名称是什么?
[Unknown]: 广州
您所在的州或省份名称是什么?
[Unknown]: 广东
该单位的两字母国家代码是什么
[Unknown]: 86
CN=Pelephone, OU=Pelephone, O=Pelephone, L=广州, ST=广东, C=86 正确吗?
[否]: y

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

1、build.xml

2、build.properties

build.xml的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="autoDeployAPK" default="deploy">  
  3.     <!-- 使用第三方的ant包,使ant支持for循环-->  
  4.     <taskdef resource="net/sf/antcontrib/antcontrib.properties">  
  5.         <classpath>  
  6.             <pathelement location="lib/ant-contrib-1.0b3.jar"/>  
  7.         </classpath>  
  8.     </taskdef>  
  9.   
  10.   
  11.     <property file="build.properties" />  
  12.   
  13.     <!-- The local.properties file is created and updated by the 'android' tool.  
  14.          It contains the path to the SDK. It should *NOT* be checked into  
  15.          Version Control Systems.  
  16.     <property file="local.properties" />  
  17.      -->  
  18.   
  19.     <!-- The ant.properties file can be created by you. It is only edited by the  
  20.          'android' tool to add properties to it.  
  21.          This is the place to change some Ant specific build properties.  
  22.          Here are some properties you may want to change/update:  
  23.   
  24.          source.dir  
  25.              The name of the source directory. Default is 'src'.  
  26.          out.dir  
  27.              The name of the output directory. Default is 'bin'.  
  28.   
  29.          For other overridable properties, look at the beginning of the rules  
  30.          files in the SDK, at tools/ant/build.xml  
  31.   
  32.          Properties related to the SDK location or the project target should  
  33.          be updated using the 'android' tool with the 'update' action.  
  34.   
  35.          This file is an integral part of the build system for your  
  36.          application and should be checked into Version Control Systems.  
  37.   
  38.     <property file="ant.properties" />  
  39.          -->  
  40.   
  41.     <property name="jar.libs.dir" value="${jar.libs.dir}" />  
  42.   
  43.     <!-- The project.properties file is created and updated by the 'android'  
  44.          tool, as well as ADT.  
  45.   
  46.          This contains project specific properties such as project target, and library  
  47.          dependencies. Lower level build properties are stored in ant.properties  
  48.          (or in .classpath for Eclipse projects).  
  49.   
  50.          This file is an integral part of the build system for your  
  51.          application and should be checked into Version Control Systems. -->  
  52.     <loadproperties srcFile="${project.dir}/project.properties" />  
  53.   
  54.   
  55.   
  56.     <!-- quick check on sdk.dir -->  
  57.     <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"  
  58.             unless="sdk.dir"/>  
  59.   
  60.     <property name="channelname" value="pateo" />  
  61.     <property name="channelkey" value="12347" />  
  62.   
  63.   
  64.   
  65.     <!--循环打包 -->  
  66.     <target name="deploy">    
  67.         <foreach target="modify_manifest" list="${channel.list}" param="nameandchannel" delimiter=","></foreach>  
  68.     </target>  
  69.   
  70.     <target name="modify_manifest">    
  71.         <!-- 获取渠道名字 -->  
  72.         <propertyregex override="true" property="channelname" input="${nameandchannel}" regexp="(.*):" select="1"/>  
  73.         <!-- 获取渠道号码 -->  
  74.         <propertyregex override="true" property="channelkey" input="${nameandchannel}" regexp=":(.*)" select="1"/>  
  75.         <!-- 正则匹配替换渠道号   
  76.         <replaceregexp flags="g" byline="false" encoding="UTF-8">    
  77.             <regexp pattern='meta-data android:name="CHANNEL" android:value="(.*)"' />          
  78.             <substitution expression='meta-data android:name="CHANNEL" android:value="${channelkey}"' />    
  79.             <fileset dir="" includes="AndroidManifest.xml" />    
  80.         </replaceregexp>-->  
  81.         <property name="out.final.file"    
  82.                           location="${apk.out.dir}/${project.name}_${channelname}_${project.version}.apk" />  
  83.         <antcall target="release" />  
  84.     </target>  
  85.   
  86. <!-- extension targets. Uncomment the ones where you want to do custom work  
  87.      in between standard targets -->  
  88. <!--  
  89.     <target name="-pre-build">  
  90.     </target>  
  91.     <target name="-pre-compile">  
  92.     </target>  
  93.   
  94.     /* This is typically used for code obfuscation. 
  95.        Compiled code location: ${out.classes.absolute.dir} 
  96.        If this is not done in place, override ${out.dex.input.absolute.dir} */  
  97.     <target name="-post-compile">  
  98.     </target>  
  99. -->  
  100.   
  101. <!--如果项目包含了jni代码,希望在打包时自动重新编译so库,可以修改build.xml文件。  
  102.     修改方法为,在引用sdk的build.xml文件之前添加如下target:-->  
  103. <!--  
  104.     <target name="-pre-build" depends="-ndk-build">  
  105.     </target>  
  106.     <target name="-ndk-build">  
  107.         <exec executable="ndk-build" failonerror="true">  
  108.             <arg value="clean" />  
  109.         </exec>  
  110.         <exec executable="ndk-build" failonerror="true" />  
  111.     </target>  
  112. -->  
  113.   
  114.     <!-- Import the actual build file.  
  115.   
  116.          To customize existing targets, there are two options:  
  117.          - Customize only one target:  
  118.              - copy/paste the target into this file, *before* the  
  119.                <import> task.  
  120.              - customize it to your needs.  
  121.          - Customize the whole content of build.xml  
  122.              - copy/paste the content of the rules files (minus the top node)  
  123.                into this file, replacing the <import> task.  
  124.              - customize to your needs.  
  125.   
  126.          ***********************  
  127.          ****** IMPORTANT ******  
  128.          ***********************  
  129.          In all cases you must update the value of version-tag below to read 'custom' instead of an integer,  
  130.          in order to avoid having your file be overridden by tools such as "android update project"  
  131.     -->  
  132.         <!-- version-tag: 导入anroid sdk 默认的ant写好的build.xml -->  
  133.     <import file="${sdk.dir}/tools/ant/build.xml" />  
  134.   
  135. </project>  


这个文件主要就是打包的指令,其中大家不难看出

  1. <import file="${sdk.dir}/tools/ant/build.xml" />  

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

build.properties的内容:

  1. #Save  
  2. #Wed Oct 23 15:47:27 CST 2013  
  3. project.name=MOBILE_ANDROID_PROJECT  
  4. jar.libs.dir=libs  
  5. build.first=false  
  6. key.alias=maomao  
  7. key.alias.password=maomao  
  8. channel.list=test1:100411-f91d7ad6c99688b587b299d2c507679d,test2:100411-f91d7ad6c99688b587b299d2c507679d  
  9. java.dir=C:\Program Files (x86)\Java\jdk1.6.0_10  
  10. key.store=C:\changeself\test.keystore  
  11. project.dir=C:\changeself\project_android  
  12. sdk.dir=C:\changeself\adt-bundle-windows-x86-20130729\sdk  
  13. project.version=1.0.3  
  14. key.store.password=maomao  
  15. apk.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

原文地址:https://www.cnblogs.com/pelephone/p/android-ant-signed.html