[转]使用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的内容:
- <?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 2013
- project.name=MOBILE_ANDROID_PROJECT
- jar.libs.dir=libs
- build.first=false
- key.alias=maomao
- key.alias.password=maomao
- channel.list=test1\:100411-f91d7ad6c99688b587b299d2c507679d,test2\:100411-f91d7ad6c99688b587b299d2c507679d
- java.dir=C\:\\Program Files (x86)\\Java\\jdk1.6.0_10
- key.store=C\:\\changeself\\test.keystore
- project.dir=C\:\\changeself\\project_android
- sdk.dir=C\:\\changeself\\adt-bundle-windows-x86-20130729\\sdk
- project.version=1.0.3
- key.store.password=maomao
- 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