上一篇(Android自动化构建之Ant多渠道打包实践(上))已经介绍了Android的apk是如何构建的,本篇博客继续Ant打包的实践过程。
这里以友盟统计为例,对各个渠道进行统计,我们需要先集成它的SDK
配置权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" >
uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
uses-permission>
渠道配置
<meta-data
android:name="UMENG_APPKEY"
android:value="56f0b1ef67e58eded700015b" >
meta-data>
<meta-data android:name="UMENG_CHANNEL" android:value="Umeng" />
使用Ant打包的时候替换的渠道号就是
将Umeng替换为具体的渠道,比如将Umeng替换为xiaomi。
这个文件定义了Ant脚本要用到的一些参数值,我们的渠道也是定义在这里,具体看代码:
#project name and version
project.name=AntBuild
project.version=4.1.4
#android platform version
android-platform=android-19
#keysore file
ketstore.file=release.keystore
key.alias=release.keystore
key.alias.password=123456
key.store.password=123456
#publish channel
channelname=Umeng
channelkey=360,QQ,xiaomi,liangxiang
key=360,QQ,xiaomi,liangxiang
#library project
library-dir=../Library
library-dir2=../Library2
# generate R.java for libraries. Separate libraries with ':'.
extra-library-packages=
#filnal out dir
out.dir=publish
<project name="iReaderApp" default="deploy" >
<property file="build.properties" />
<property environment="env" />
<property name="version" value="${project.version}" />
<property name="appName" value="${project.name}" />
<property name="sdk-folder" value="${env.ANDROID_SDK_HOME}" />
<property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-19"/>
<property name="sdk-tools" value="${sdk-folder}/tools" />
<property name="sdk-platform-tools" value="${sdk-folder}/build-tools/android-4.4.2" />
<property name="aapt" value="${sdk-platform-tools}/aapt" />
<property name="library-dir" value="${library-dir}" />
<property name="library-dir2" value="${library-dir2}" />
<property name="aapt" value="${sdk-platform-tools}/aapt" />
<property name="aidl" value="${sdk-platform-tools}/aidl" />
<property name="dx" value="${sdk-platform-tools}/dx.bat" />
<property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />
<property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner" />
<property name="zipalign" value="${sdk-tools}/zipalign" />
<property name="android-jar" value="${sdk-platform-folder}/android.jar" />
<property name="proguard-home" value="${sdk-tools}/proguard/lib" />
<property name="framework-aidl" value="${sdk-platform-folder}/framework.aidl" />
<property name="manifest-xml" value="AndroidManifest.xml" />
<property name="resource-dir" value="res" />
<property name="asset-dir" value="assets" />
<property name="srcdir" value="src" />
<property name="srcdir-ospath" value="${basedir}/${srcdir}" />
<property name="external-lib" value="libs" />
<property name="external-compile-lib" value="compile-libs" />
<property name="external-lib-ospath" value="${basedir}/${external-lib}" />
<property name="external-compile-lib-ospath" value="${basedir}/${external-compile-lib}" />
<property name="external-library-dir-lib-ospath" value="${library-dir}/${external-lib}" />
<property name="external-library-dir2-lib-ospath" value="${library-dir2}/${external-lib}" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${external-lib-ospath}/ant-contrib-1.0b3.jar" />
classpath>
taskdef>
<property name="channelname" value="${channelname}" />
<property name="channelkey" value="${channelkey}" />
<property name="key" value="${key}" />
<target name="deploy">
<foreach target="modify_manifest" list="${key}" param="nameandchannel" delimiter=",">
foreach>
target>
<target name="modify_manifest">
<propertyregex override="true" property="channelkey" input="${nameandchannel}" regexp="(.*)" select="\1" />
<replaceregexp flags="g" byline="false" encoding="UTF-8">
<regexp pattern='meta-data android:name="UMENG_CHANNEL" android:value="(.*)"' />
<substitution expression='meta-data android:name="UMENG_CHANNEL" android:value="${channelkey}"' />
<fileset dir="" includes="AndroidManifest.xml" />
replaceregexp>
<antcall target="zipalign" />
target>
<target name="init">
<echo>目录初始化....echo>
<var name="outdir-gen" value="gen" />
<var name="outdir-bin" value="${out.dir}/${channelkey}" />
<var name="outdir-classes" value="${outdir-bin}/otherfile" />
<var name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />
<var name="dex-file" value="classes.dex" />
<var name="dex-path" value="${outdir-bin}/${dex-file}" />
<var name="dex-ospath" value="${basedir}/${dex-path}" />
<var name="resources-package" value="${outdir-bin}/resources.ap_" />
<var name="resources-package-ospath" value="${basedir}/${resources-package}" />
<var name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />
<var name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
<var name="keystore-file" value="${basedir}/${ketstore.file}" />
<var name="out-signed-package" value="${outdir-bin}/${appName}_${channelkey}_${version}.apk" />
<var name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />
<delete dir="${outdir-bin}" />
<mkdir dir="${outdir-bin}" />
<mkdir dir="${outdir-classes}" />
target>
<target name="gen-R" depends="init">
<echo>生成R.java文件....echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="--auto-add-overlay" />
<arg value="-J" />
<arg value="${outdir-gen}" />
<arg value="-M" />
<arg value="${manifest-xml}" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-S" />
<arg value="${library-dir}/res" />
<arg value="-S" />
<arg value="${library-dir2}/res" />
<arg value="-I" />
<arg value="${android-jar}" />
exec>
target>
<target name="aidl" depends="gen-R">
<echo>编译aidl文件....echo>
<apply executable="${aidl}" failonerror="true">
<arg value="-p${framework-aidl}" />
<arg value="-I${srcdir}" />
<arg value="-o${outdir-gen}" />
<fileset dir="${srcdir}">
<include name="**/*.aidl" />
fileset>
apply>
target>
<target name="compile" depends="aidl">
<echo>java源文件编译成class文件....echo>
<javac encoding="UTF-8" destdir="${outdir-classes}" bootclasspath="${android-jar}">
<src path="${library-dir}/src" />
<src path="${outdir-gen}" />
<classpath>
<fileset dir="${external-library-dir-lib-ospath}" includes="*.jar" />
classpath>
javac>
<javac encoding="UTF-8" destdir="${outdir-classes}" bootclasspath="${android-jar}">
<src path="${library-dir2}/src" />
<src path="${outdir-gen}" />
<classpath>
<fileset dir="${external-library-dir2-lib-ospath}" includes="*.jar" />
classpath>
javac>
<javac encoding="UTF-8" destdir="${outdir-classes}" bootclasspath="${android-jar}" >
<compilerarg line="-encoding UTF-8 " />
<src path="${basedir}/src" />
<src path="${outdir-gen}" />
<classpath>
<fileset dir="${external-lib}" includes="*.jar" />
<fileset dir="${external-library-dir-lib-ospath}" includes="*.jar" />
classpath>
javac>
target>
<target name="dex" depends="compile" unless="do.not.compile">
<echo>将.class文件转化成.dex文件....echo>
<exec executable="${dx}" failonerror="true">
<arg value="--dex" />
<arg value="--output=${dex-ospath}" />
<arg value="${outdir-classes-ospath}" />
<arg value="${external-lib-ospath}" />
exec>
target>
<target name="package-res-and-assets">
<echo>将资源文件放进输出目录....echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg value="${manifest-xml}" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-S"/>
<arg value="${library-dir}/res"/>
<arg value="-S"/>
<arg value="${library-dir2}/res"/>
<arg value="-A" />
<arg value="${asset-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
<arg value="-F" />
<arg value="${resources-package}" />
<arg value="--auto-add-overlay" />
exec>
target>
<target name="package" depends="dex,package-res-and-assets">
<echo>打包成未签证的apk....echo>
<java classpath="${sdk-tools}/lib/sdklib.jar" classname="com.android.sdklib.build.ApkBuilderMain">
<arg value="${out-unsigned-package-ospath}" />
<arg value="-u" />
<arg value="-z" />
<arg value="${resources-package-ospath}" />
<arg value="-f" />
<arg value="${dex-ospath}" />
<arg value="-rj" />
<arg value="${external-lib-ospath}"/>
<arg value="-nf" />
<arg value="${external-lib-ospath}" />
java>
target>
<target name="jarsigner" depends="package">
<echo>Packaging signed apk for release...echo>
<exec executable="${jarsigner}" failonerror="true">
<arg value="-keystore" />
<arg value="${keystore-file}" />
<arg value="-storepass" />
<arg value="${key.store.password}" />
<arg value="-keypass" />
<arg value="${key.alias.password}" />
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}" />
<arg value="${key.alias}" />
exec>
target>
<target name="release" depends="jarsigner">
<delete file="${out-unsigned-package-ospath}" />
<echo>APK is released. path:${out-signed-package-ospath}echo>
<echo>删除其他文件,最后只保留apkecho>
<delete dir="${outdir-classes}"/>
<delete file="${dex-ospath}" />
<delete file="${resources-package-ospath}" />
<echo>生成apk完成echo>
target>
<target name="zipalign" depends="release">
<exec executable="${zipalign}" failonerror="true">
<arg value="-v" />
<arg value="4" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-signed-package-ospath}-zipaligned.apk" />
exec>
target>
project>
上面就是完整的Ant脚本,实现了自动化构建和多渠道的打包,笔者在实践的过程踩过不少坑才最终把apk包成功打出。
这里总结下可能遇到的坑:
- 生成R.java文件,一定要注意先后顺序,主项目之后才到关联项目
- 编译生成class文件,可能会遇到找不到类,一定要按照添加库的顺序来编译class文件
- 替换渠道号的时候,Ant中pattern里的内容要与mainfest文件的内容一致,包括顺序,空格),笔者试过格式化后代码之后就不能写入成功
@echo off
call ant -buildfile "build.xml" deploy
echo done
pause
exit
我们可以在项目中的publish目录下生成不同渠道的apk文件:
安装apk到设备,启动之后在友盟后台集成测试,看app发布的渠道:
https://github.com/devilWwj/Android-Tech/tree/master/AntBuildTest
实现Ant多渠道打包整个过程还是比较繁琐的,主要在Ant脚本上,比较容易出错,需要对命令比较了解,但确实能够缩短我们打渠道包的时间,基于本次实践是基于Eclipse,目前Android Studio使用gradle来实现多渠道打包,以后会把gradle进行多渠道打包的实现分享给大家,大家可以对比下这两种打包方式的区别,主要目的是更加深入的了解apk的构建过程。
欢迎关注我的公众号:wwjblog