ant build is library project

需求:

android project编译时,需要引用另一个android project项目,另一个android project为is library。现需要通过ant 把两个项目同时打包。

原理:

使用统一build.xml把android project与is library project 的gen生成到android project/gen,把class生成到android project/bin/class中。

方法:

把build.xml放到android project中,修改build.properties中的配置

build.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="APPNAME" default="release" basedir=".">
<property file="config.properties"/>
<property file="build.properties"/>
<!-- ANT环境变量 -->
<property environment="env" />
<property name="aidl" value="${sdk-platform-tools}/aidl" />
<property name="android-dx" value="${sdk-platform-tools}/dx.bat" />
<property name="android-apkbuilder" value="${sdk-tools}/apkbuilder.bat" />
<property name="android-zipalign" value="${sdk-tools}/zipalign" />
<!-- 生成R文件的相对目录 -->
<property name="outdir-gen" value="${project.home}/gen" />
<!-- 编译后的文件放置目录 -->
<property name="outdir-bin" value="${project.home}/bin" />
<!-- 输出目录 -->
<property name="outdir" value="${project.outdir}" />
<!-- 清单文件 -->
<property name="manifest-xml" value="${project.home}/AndroidManifest.xml" />



<!-- 编译aidl文件所需的预处理框架文件framework.aidl -->
<property name="framework-aidl" value="${sdk-platform-folder}/framework.aidl" />
<!-- 源文件目录 -->
<property name="resource-dir" value="res" />
<property name="asset-dir" value="assets" />
<property name="library-manifest-xml" value="${library-dir}/AndroidManifest.xml" />
<property name="android-jarsigner" value="${env.JAVA_HOME}/bin/jarsigner" />

<!-- 资源文件目录 -->
<property name="drawable-dir" value="${resource-dir}/drawable" />
<property name="drawable-hdpi-dir" value="${resource-dir}/drawable-hdpi"/>
<property name="drawable-ldpi-dir" value="${resource-dir}/drawable-ldpi"/>
<property name="drawable-mdpi-dir" value="${resource-dir}/drawable-mdpi"/>
<property name="drawable-nodpi" value="${resource-dir}/drawable-nodpi"/>
<property name="drawable-xhdpi" value="${resource-dir}/drawable-xhdpi"/>
<property name="drawable-xxhdpi" value="${resource-dir}/drawable-xxhdpi"/>
<!-- java源文件目录 -->
<property name="srcdir" value="src" />
<property name="srcdir-ospath" value="${project.home}/${srcdir}" />
<!-- 动态库so文件目录 -->
<property name="library" value="${project.home}/libs" />
<!-- 第三方library -->
<property name="library-dir" value="${library-dir}" />

<!-- 外部类库所在目录 -->
<property name="external-lib" value="libs" />
<property name="external-lib-ospath" value="${project.home}/${external-lib}" />
<!-- 生成class目录 -->
<property name="outdir-classes" value="${outdir-bin}/classes" />
<property name="outdir-classes-ospath" value="${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="${dex-path}" />
<!-- 经过aapt生成的资源包文件 -->
<property name="resources-package" value="${outdir-bin}/resources.ap_" />
<property name="resources-package-ospath" value="${resources-package}" />
<!-- 未认证apk包 -->
<property name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />
<property name="out-unsigned-package-ospath" value="${out-unsigned-package}" />
<!-- 证书文件 -->
<property name="keystore-file" value="${keystore}" />
<!-- 已认证apk包 -->
<property name="out-signed-package" value="${outdir-bin}/${appName}.apk" />
<property name="out-signed-package-ospath" value="${out-signed-package}" />
<!-- 已优化apk包 -->
<property name="zipalign-signed-package" value="${outdir-bin}/${appName}${version}_release.apk" />
<!-- 输出路径的apk包 -->
<property name="outdir-package" value="${project.outdir}/${appName}.apk" />
<!-- 初始化工作 -->
<target name="init">
<echo>Initializing all output directories...</echo>
<delete dir="${outdir-bin}" />
<delete dir="${outdir-gen}" />
<mkdir dir="${outdir-gen}" />
<mkdir dir="${outdir-bin}" />
<mkdir dir="${outdir-classes}" />
</target>
<!-- 根据工程中的资源文件生成R.java文件 -->
<target name="GenRJAVA" depends="init">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<!-- 生成liibrary project的 R.java-->
<exec
executable="${android-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="--extra-packages" />
<arg value="com.XXXX.XXXXX" /><!-- Library Project的包名 -->
<arg value="-A" />
<arg value="${project.home}/assets" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
<!-- 编译aidl文件 -->
<target name="aidl" depends="GenRJAVA">
<echo>Compile aidl....</echo>
<apply executable="${aidl}" failonerror="true">
<!-- 指定预处理文件 -->
<arg value="-p${framework-aidl}" />
<!-- aidl声明的目录 -->
<arg value="-I${project.home}/src" />
<!-- 目标文件目录 -->
<arg value="-o${outdir-gen}" />
<!-- 指定哪些文件需要编译 -->
<fileset dir="${project.home}/src">
<include name="**/*.aidl" />
</fileset>
</apply>
</target>
<!-- 将工程及库应用中的java源文件编译成class文件 -->
<target name="compile" depends="aidl" >
<echo>Compiling java source code...</echo>
<javac encoding="UTF-8" destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="on" verbose="false">
<src path="${library-dir}/src" /><!-- 库应用源码 -->
<src path="${outdir-gen}" /><!-- 生成的class文件全部保存到bin/classes目录下 -->
<classpath>
<fileset dir="${library-dir}/libs" includes="*.jar" /><!-- 第三方jar包需要引用,用于辅助编译 -->
</classpath>
</javac>
<javac encoding="UTF-8" srcdir="./src" destdir="${outdir-classes}" bootclasspath="${project.home}/reference/framework_classes.jar" includeantruntime="on" verbose="false" >
<src path="${outdir-gen}" /><!-- 生成的class文件全部保存到bin/classes目录下 -->
<!--classpath>
<fileset dir="${sdk-platform-folder}" includes="android.jar" />
</classpath-->
<classpath>
<fileset dir="${project.home}/reference/" includes="common_classes.jar" />
<fileset dir="${project.home}/reference/" includes="core_classes.jar" />
<fileset dir="${sdk-platform-folder}/" includes="android.jar" />
<fileset dir="${library-dir}/libs" includes="*.jar" /><!-- 需要用到类库 -->
<fileset dir="${library}" includes="*.jar" /><!-- 第三方jar包需要引用,用于辅助编译 -->
</classpath>

</javac>
</target>
<!-- 将.class文件转化成.dex文件 -->
<target name="dex" depends="compile">
<echo>Converting compiled files and external libraries into ${outdir-dx}test.dex...</echo>
<exec executable="${android-dx}" failonerror="true">
<arg value="--dex" />
<arg value="--output=${dex-ospath}" /><!-- 输出 -->
<arg path="${outdir-classes}" /><!-- classes文件位置 -->
<arg value="${library}" /> <!-- 把libs下所有jar打包 -->
<arg value="${library-dir}/libs" /> <!-- 把libs下所有jar打包 -->
</exec>
</target>
<!-- 将资源文件放进输出目录 -->
<target name="package-res-and-assets">
<echo>Packaging resources and assets...</echo>
<exec executable="${android-aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" /><!-- 资源覆盖重写 -->
<arg value="-M" /><!-- M指定AndroidManifest.xml文件-->
<arg value="${manifest-xml}" />
<arg value="-S"/>
<arg value="./res"/>
<arg value="-S"/>
<arg value="${library-dir}/res"/>
<arg value="-A" /><!-- asset目录也打包 -->
<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>
<!-- 打包成未签证的apk -->
<target name="package" depends="dex,package-res-and-assets">
<echo>Packaging unsigned apk for release...</echo>
<exec executable="${android-apkbuilder}" >
<arg value="${out-unsigned-package-ospath}" /><!-- 输出 -->
<arg value="-u" /><!-- u指创建未签名的包-->
<arg value="-z" /><!-- 资源压缩包 -->
<arg value="${resources-package-ospath}" />
<arg value="-f" /><!-- dex文件 -->
<arg value="${dex-ospath}" />
<arg value="-rj"/>
<arg value="${library}" />
<arg value="-rj"/><!-- 添加jar文件 -->
<arg value="${library-dir}/libs" />
</exec>
<echo>It will need to be signed with jarsigner before being published.</echo>
</target>
<!-- 对apk进行签证 -->
<target name="jarsigner" depends="package">
<echo>Packaging signed apk for release...</echo>
<exec executable="${android-jarsigner}" failonerror="true">
<arg value="-keystore" />
<arg value="${keystore-file}" />
<arg value="-storepass" />
<arg value="111111" /><!-- 验证密钥完整性的口令,创建时建立的 -->
<arg value="-keypass" />
<arg value="111111" /> <!-- 专用密钥的口令,就是key密码 -->
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" /><!-- 输出 -->
<arg value="${out-unsigned-package-ospath}" /><!-- 未签名的apk -->
<!-- 不要忘了证书的别名 -->
<arg value="aaaa" />
</exec>
</target>
<!-- 优化 -->
<target name="optimize-app" depends="jarsigner">
<echo> optimizing the application...</echo>
<exec executable="${android-zipalign}">
<arg value="-v" />
<arg value="-f" />
<arg value="4" />
<arg value="${out-signed-package-ospath}" />
<arg value="${zipalign-signed-package}" />
</exec>
</target>
<!-- 发布 -->
<target name="release" depends="optimize-app">
<!-- 删除未签证apk -->
<delete file="${out-unsigned-package-ospath}" />
<echo>APK is released. path:${out-signed-package-ospath}</echo>
<copy file="${out-signed-package-ospath}" tofile="${outdir-package}"></copy>
<echo>APK is copy to path:${outdir-package}</echo>
</target>
</project>

相关推荐