参考文献:http://blog.csdn.net/junfeng120125/article/details/9234651
开发环境:win764位,android-ndk-r9,cocos2d-x-3.2,contrib-1.0b3,android-4.4
直接上代码
class="xml">
<?xml version="1.0" encoding="UTF-8"?>
<project name="Test" default="deploy">
<!-- ANT环境变量 -->
<property environment="env" />
<!-- 使用第三方的ant包,使ant支持for循环-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${env.ANT_ROOT}/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<property file="local.properties" />
<!-- 应用名称 -->
<property name="appName" value="${ant.project.name}" />
<!-- SDK目录(获取操作系统环境变量ANDROID_SDK_HOME的值) -->
<property name="sdk-folder" value="${env.ANDROID_SDK_ROOT}" />
<!-- SDK指定平台目录 -->
<property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-19" />
<!-- SDK中tools目录 -->
<property name="sdk-tools" value="${sdk-folder}/tools" />
<!-- SDK指定平台中tools目录 -->
<property name="sdk-build-tools" value="${sdk-folder}/build-tools/android-4.4" />
<!-- 使用到的命令(当前系统为windows,如果系统为linux,可将.bat文件替换成相对应的命令) -->
<property name="aapt" value="${sdk-build-tools}/aapt" />
<property name="aidl" value="${sdk-build-tools}/aidl" />
<property name="dx" value="${sdk-build-tools}/dx.bat" />
<!-- 在android3.0这个apkbuilder被废弃了,可以copy一个放在如下目录下-->
<property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />
<property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner" />
<property name="zipalign" value="${sdk-tools}/zipalign" />
<!-- 编译需要的jar; 如果项目使用到地图服务则需要maps.jar -->
<property name="android-jar" value="${sdk-platform-folder}/android.jar" />
<property name="proguard-home" value="${sdk-tools}/proguard/lib" />
<!--
<property name="android-maps-jar" value="${sdk-folder}/add-ons/addon_google_apis_google_inc_8/libs/maps.jar" />
-->
<!--
-->
<!-- 编译aidl文件所需的预处理框架文件framework.aidl -->
<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" />
<!-- java源文件目录 -->
<property name="srcdir" value="src" />
<property name="srcdir-ospath" value="${basedir}/${srcdir}" />
<!-- 外部类库所在目录 -->
<property name="external-lib" value="libs" />
<property name="external-lib-ospath" value="${basedir}/${external-lib}" />
<property name="ndk.dir" value="${evn.NDK_ROOT}" />
<!-- 版本 -->
<property name="version" value="3.0" />
<property name="channelname" value="" />
<property name="channelkey" value="" />
<property name="key" value="${channels}" />
<!--循环打包 -->
<target name="deploy">
<!---->
<antcall target="native" />
<foreach target="modify_manifest" list="${key}" param="nameandchannel" delimiter=",">
</foreach>
</target>
<target name="modify_manifest">
<echo>----------------------------------------------------</echo>
<!-- 获取渠道名字 -->
<!--
<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="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>
<!-- 生成R文件的相对目录 -->
<var name="outdir-gen" value="gen" />
<!-- 编译后的文件放置目录 -->
<var name="outdir-bin" value="publish/${channelkey}" />
<!-- 生成class目录 -->
<var name="outdir-classes" value="${outdir-bin}/otherfile" />
<var name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />
<!-- classes.dex相关变量 -->
<var name="dex-file" value="classes.dex" />
<var name="dex-path" value="${outdir-bin}/${dex-file}" />
<var name="dex-ospath" value="${basedir}/${dex-path}" />
<!-- 经过aapt生成的资源包文件 -->
<var name="resources-package" value="${outdir-bin}/resources.ap_" />
<var name="resources-package-ospath" value="${basedir}/${resources-package}" />
<!-- 未认证apk包 -->
<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}/testant.keystore" />
<!-- 已认证apk包 -->
<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>
<!-- 根据工程中的资源文件生成R.java文件 -->
<target name="gen-R" depends="init">
<echo>生成R.java文件....</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-m" />
<arg value="-J" />
<arg value="${outdir-gen}" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-M" />
<arg value="${manifest-xml}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
<!-- 编译aidl文件 -->
<target name="aidl" depends="gen-R">
<echo>编译aidl文件....</echo>
<apply executable="${aidl}" failonerror="true">
<!-- 指定预处理文件 -->
<arg value="-p${framework-aidl}" />
<!-- aidl声明的目录 -->
<arg value="-I${srcdir}" />
<!-- 目标文件目录 -->
<arg value="-o${outdir-gen}" />
<!-- 指定哪些文件需要编译 -->
<fileset dir="${srcdir}">
<include name="**/*.aidl" />
</fileset>
</apply>
</target>
<!-- 将工程中的java源文件编译成class文件 -->
<target name="compile" depends="aidl">
<echo>java源文件编译成class文件....</echo>
<javac encoding="utf-8" target="1.5" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}" verbose="false">
<compilerarg line="-encoding GBK " />
<classpath>
<fileset dir="${external-lib}" includes="*.jar" />
</classpath>
</javac>
</target>
<!-- 将.class文件转化成.dex文件 -->
<target name="dex" depends="compile">
<echo>将.class文件转化成.dex文件....</echo>
<exec executable="${dx}" failonerror="true">
<arg value="--dex" />
<!-- 输出文件 -->
<arg value="--output=${dex-ospath}" />
<!-- 要生成.dex文件的源classes和libraries -->
<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="-A" />
<arg value="${asset-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
<arg value="-F" />
<arg value="${resources-package}" />
</exec>
</target>
<!---->
<target name="native">
<echo>编译native包......</echo>
<!--<exec executable="${ndk.dir}/ndk-build.cmd" failonerror="true" />-->
<!--<exec executable="D:\devtools\android-ndk-r10\ndk-build.cmd" failonerror="true" />-->
<exec executable="${env.NDK_ROOT}/ndk-build.cmd" failonerror="true">
<arg value="-D" />
</exec>
<echo message="DONE (Building native libraries)" />
</target>
<!-- 打包成未签证的apk -->
<target name="package" depends="dex, package-res-and-assets">
<echo>打包成未签证的apk....</echo>
<exec executable="${apkbuilder}" failonerror="true">
<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="-rf" />
<arg value="${srcdir-ospath}" />
<arg value="-nf"/>
<arg value="${external-lib-ospath}" />
</exec>
</target>
<!-- 对apk进行签证 -->
<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="111111" />
<arg value="-keypass" />
<arg value="111111" />
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}" />
<!-- 不要忘了证书的别名 -->
<arg value="testant" />
</exec>
</target>
<!-- 发布 -->
<target name="release" depends="jarsigner">
<!-- 删除未签证apk -->
<delete file="${out-unsigned-package-ospath}" />
<echo>APK is released. path:${out-signed-package-ospath}</echo>
<delete dir="${outdir-classes}"/>
<delete file="${dex-ospath}" />
<delete file="${resources-package-ospath}" />
</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>
<target name="install" depends="deploy"
description="install debug.apk">
<!-- target:debug调用过后,out.filal.file才会作为常量被初始化。所以单独调用install是无效的 -->
<echo message="adb install -r ${out.absolute.dir}/${ant.project.name}-debug.apk"/>
<apply executable="${sdk-folder}/platform-tools/adb.exe" failonerror="true">
<arg value="install" />
<arg value="-r" />
<file file="${outdir-bin}/${appName}_${channelkey}_${version}.apk"/>
</apply>
</target>
<target name="startActivity" depends="install"
description="Start Activity: app_package/app_launch_activity">
<echo>安装apk</echo>
<echo message="adb shell am start -n ${app_package}/${app_launch_activity}"/>
<exec executable="adb">
<arg value="shell"/>
<arg value="am"/>
<arg value="start"/>
<arg value="-n"/>
<arg value="${app_package}/${app_launch_activity}"/>
</exec>
</target>
</project>
local.properties文件
sdk.dir=D:\\devtools\\adt-bundle-windows-x86_64-20131030\\sdk
### 定义apk的包名
app_package=cn.hy.test
### 定义apk的启动入口Activity
app_launch_activity=org.cocos2dx.cpp.AppActivity
channels=11,22,33,44,55,66,77,88,99
在AndroidManifest.xml中加入
<meta-data android:name="UMENG_CHANNEL" android:value="99"></meta-data>
如果出现如下
错误: *** Android NDK: Aborting . Stop.
将Application.mk中
APP_STL := c++_static
NDK_TOOLCHAIN_VERSION=clang
APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char
APP_LDFLAGS := -latomic
替换为:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=1 -std=c++11 -Wno-literal-suffix -fsigned-char