自己代码目录修改如下
<?xml version="1.0" encoding="UTF-8"?> <project name="HelloCpp" default="help"> <!--自己定义的外部的库的目录 --> <property name="external-lib" value="/Users/jiangxf/dev/cocos2d-2.0-x-2.0.4/samples/HelloCpp/91.android/extern-libs/" /> <property name="external-jar-file1" value="umeng-sdk-analytics_v5.2.2.jar"/> <property name="external-jar-file2" value="NdComPlatform.jar"/> <property name="external-jar-file3" value=""/> <property name="external-jar-file4" value=""/> <property name="external-jar-file5" value=""/> <property file="local.properties" /> <property file="ant.properties" /> <loadproperties srcFile="project.properties" /> <import file="${sdk.dir}/tools/ant/build.xml" /> </project>
修改adt/tools/ant/build.xml
<javac encoding="${java.encoding}" source="${java.source}" target="${java.target}" debug="true" extdirs="" includeantruntime="false" destdir="${out.classes.absolute.dir}" bootclasspathref="project.target.class.path" verbose="${verbose}" classpathref="project.javac.classpath" fork="${need.javac.fork}"> <src path="${source.absolute.dir}" /> <src path="${gen.absolute.dir}" /> <compilerarg line="${java.compilerargs}" /> <classpath> <fileset dir="${external-lib}" includes="*.jar"/> </classpath> </javac>
<!-- Converts this project's .class files into .dex files --> <target name="-dex" depends="-compile, -post-compile, -obfuscate"> <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping..."> <!-- only convert to dalvik bytecode is *not* a library --> <do-only-if-not-library elseText="Library project: do not convert bytecode..." > <dex-helper nolocals="true"> <external-libs> <fileset file="${external-lib}/${external-jar-file1}" /> <fileset file="${external-lib}/${external-jar-file2}" /> </external-libs> </dex-helper> </do-only-if-not-library> </do-only-if-manifest-hasCode> </target>
参考
<project name="ant" default="release">
<!-- ANT环境变量 -->
<property environment="env" />
<!-- 使用第三方的ant包,使ant支持for循环-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<!-- 应用名称 -->
<property name="appName" value="${ant.project.name}"/>
<!-- SDK目录(获取操作系统环境变量ANDROID_SDK_HOME的值) -->
<!--<property name="sdk-folder" value="${env.ANDROID}" /> -->
<property name="sdk-folder" value="/Applications/adt/sdk/" />
<!-- SDK指定平台目录 -->
<property name="sdk-platform-folder" value="/Applications/adt/sdk//platforms/android-14/"/>
<!-- SDK中tools目录 -->
<property name="sdk-tools" value="${sdk-folder}/tools" />
<!-- SDK指定平台中tools目录 -->
<property name="sdk-platform-tools" value="/Applications/adt/sdk/build-tools/android-4.3/" />
<!-- 使用到的命令(当前系统为windows,如果系统为linux,可将.bat文件替换成相对应的命令) -->
<property name="aapt" value="${sdk-platform-tools}/aapt" />
<property name="aidl" value="${sdk-platform-tools}/aidl" />
<property name="dx" value="${sdk-platform-tools}/dx" />
<property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />
<property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner" />
<property name="zipalign" value="${sdk-tools}/zipalign" />
<!--/Applications/adt/sdk//platforms/android-14/android.jar 14 == android4.0 -->
<!-- 编译需要的jar; 如果项目使用到地图服务则需要maps.jar -->
<property name="android-jar" value="${sdk-platform-folder}/android.jar" />
<property name="android-maps-jar" value="${sdk-folder}/add-ons/addon_google_apis_google_inc_8/libs/maps.jar"/>
<!-- -->
<property name="channelname" value="" />
<property name="channelkey" value="" />
<!-- 编译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="extern-libs" />
<property name="external-lib-ospath" value="${basedir}/${external-lib}" />
<!-- 渠道名:渠道号 -->
<property name="key" value="nameA:aaaaaa"/>
<!-- 版本 -->
<property name="version" value="3.0" />
<!-- 初始化工作 -->
<target name="init">
<echo>目录初始化....</echo>
<!-- 生成R文件的相对目录 -->
<property name="outdir-gen" value="gen" />
<!-- 编译后的文件放置目录 -->
<property name="outdir-bin" value="bin-${channelname}" />
<!-- 生成class目录 -->
<property name="outdir-classes" value="${outdir-bin}" />
<property name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />
<!-- classes.dex相关变量 -->
<property name="dex-file" value="classes.dex" />
<property name="dex-path" value="${outdir-bin}/${dex-file}" />
<property name="dex-ospath" value="${basedir}/${dex-path}" />
<!-- 经过aapt生成的资源包文件 -->
<property name="resources-package" value="${outdir-bin}/resources.ap_" />
<property name="resources-package-ospath" value="${basedir}/${resources-package}" />
<!-- 未认证apk包 -->
<property name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />
<property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
<!-- 证书文件 -->
<property name="keystore-file" value="${basedir}/G3_key" />
<!-- 已认证apk包 -->
<property name="out-signed-package" value="${outdir-bin}/${appName}-${channelname}-${version}.apk" />
<property 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="deploy">
<foreach target="modify_manifest" list="${key}" 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:value="(.*)" android:name="app_key"' />app_key<substitution expression='meta-data android:value="${channelkey}" android:name="app_key"' />
<fileset dir="" includes="AndroidManifest.xml" />
</replaceregexp>
<antcall target="zipalign" />
</target>
<!-- 根据工程中的资源文件生成R.java文件 -->
<target name="gen-R" depends="init">
<echo>生成R.java文件....</echo>
<exec executable="${aapt}" failοnerrοr="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}" failοnerrοr="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.7"
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}" failοnerrοr="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}" failοnerrοr="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>
<!-- 打包成未签证的apk -->
<target name="package" depends="dex, package-res-and-assets">
<echo>打包成未签证的apk....</echo>
<exec executable="${apkbuilder}" failοnerrοr="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}" />
</exec>
</target>
<!-- 对apk进行签证 -->
<target name="jarsigner" depends="package">
<echo>Packaging signed apk for release...</echo>
<exec executable="${jarsigner}" failοnerrοr="true">
<arg value="-keystore" />
<arg value="${keystore-file}" />
<arg value="-storepass" />
<arg value="xxxxxx" />
<arg value="-keypass" />
<arg value="xxxxxx" />
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}"/>
<!-- 不要忘了证书的别名 -->
<arg value="xxxxxx"/>
</exec>
</target>
<!-- 发布 -->
<target name="release" depends="jarsigner">
<!-- 删除未签证apk -->
<delete file="${out-unsigned-package-ospath}"/>
<echo>APK is released. path:${out-signed-package-ospath}</echo>
</target>
<target name="zipalign" depends="release">
<exec executable="${zipalign}" failοnerrοr="true">
<arg value="-v" />
<arg value="4" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-signed-package-ospath}-zipaligned.apk" />
</exec>
</target>
</project>