Ant打包学习(没有继承sdk的build.xml并且不适用于引用了libraryproject的project)

打包方式有三种:
1、eclipse
2、ant
3、编写批处理主动调用adt工具打包
========================================================================

1、 进入命令行模式,并切换到项目目录,执行如下命令为ADT创建的项目添加ant build支持:

android update project -p . -t 17

2、build脚本默认target是help,所以会显示如上信息,修改target为debug或release就可以像无ant时一样编译、生成以及调试了。

3、使用release时  在local.properties添加签名信息

#keystore

key.store=hujiang.p12

key.alias=xxx

key.store.password=xxx

key.alias.password=xxx

======完整如下(没有继承sdk的build.xml并且不适用于引用了libraryproject的project)========================================

<?xml version="1.0" encoding="utf-8"?>

<!-- 步骤 


1.用aapt生成R.java


2.JAVAC编译JAVA文件


3.批处理生成classes.dex


4.aapt打包资源 


5.apkbuilder包资源和类文件为APK文件


6.jarsinger 对APK进行签名-->


<project name="Project" default="zipalign" basedir=".">

<!-- 

<property file="local.properties" /> -->


<property name="libs-dir" value="${basedir}/libs" />


<!-- 定义自己的输出目录 -->

<property name="sdk-platform-tools" value="D:/adt-bundle-windows-x86-20130219/adt-bundle-windows-x86-20130219/sdk/platform-tools" />

<property name="sdk-tools" value="D:/adt-bundle-windows-x86-20130219/adt-bundle-windows-x86-20130219/sdk/tools" />

<property name="sdk-platform" value="D:/adt-bundle-windows-x86-20130219/adt-bundle-windows-x86-20130219/sdk/platforms/android-17" />

<property name="proguard-home" value="D:/adt-bundle-windows-x86-20130219/adt-bundle-windows-x86-20130219/sdk/tools/proguard/lib" />

<property name="jdk-home" value="D:/Java/jdk1.6.0_26" />

<property name="jdk-ver" value="1.6" />

<property name="jdk-compile-encode" value="UTF-8" />

<property name="keystore" value="F:/ss.p12" />

<property name="key" value="xx" />

<property name="keypass" value="xx" />

<property name="bin-dir" value="bin" />



<!-- 定义 R.java 所有目录    <property name="out.absolute.dir" location="${bin-dir}" />-->


<property name="gen-dir" value="gen" />


<!-- 读取目录 -->


<property name="res-dir" value="res" />


<property name="assets-dir" value="assets" />


<property name="src-dir" value="src" />


<property name="src-dir-absolute" value="${basedir}/${src-dir}" />



<!-- 输出目录 -->


<property name="bin-dir-classes" value="${bin-dir}/classes" />


<property name="bin-dir-classes-absolute" value="${basedir}/${bin-dir-classes}" />


<!-- 临时类资源文件  <property name="out.classes.absolute.dir" location="${bin-dir-classes}" /> -->


<property name="dex-path-absolute" value="${basedir}/${bin-dir}/classes.dex" />


<!-- 最终生成文件 -->


<property name="out-package" value="${bin-dir}/${ant.project.name}.apk" />


<!-- 要使用的工具 -->


<property name="aapt" value="${sdk-platform-tools}/aapt.exe" />


<property name="aidl" value="${sdk-platform-tools}/aidl.exe" />


<property name="dx" value="${sdk-platform-tools}/dx.bat" />


<property name="adb" value="${sdk-platform-tools}/adb.exe" />


<property name="apk-builder" value="${sdk-tools}/apkbuilder.bat" />


<property name="android-jar" value="${sdk-platform}/android.jar" />


<!-- 最终生成各种包文件 -->


<property name="resources-package" value="${bin-dir}/${ant.project.name}" />


<property name="resources-package-ospath" value="${basedir}/${resources-package}" />


<property name="out-unsigned-package" value="${bin-dir}/${ant.project.name}-unsigned.apk" />


<property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />


<property name="out-signed-package" value="${bin-dir}/${ant.project.name}-signed.apk" />


<property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />


<property name="zipalign-package-ospath" value="${basedir}/${bin-dir}/${ant.project.name}_release.apk" />



<!--签名工具-->


<property name="jarsigner" value="${jdk-home}/bin/jarsigner.exe" />


<!--优化APK工具-->


<property name="zipalign" value="${sdk-tools}/zipalign.exe" />


<!-- 初始化 -->


<target name="init">


<echo>Creating all output directories </echo>


<delete dir="${bin-dir}" />


<delete dir="${bin-dir-classes}" />


<delete dir="${gen-dir}" />


<mkdir dir="${bin-dir}" />


<mkdir dir="${bin-dir-classes}" />


<mkdir dir="${gen-dir}" />

</target>

<!--步骤 1: aapt生成R.java depends="init"-->


<target name="generateR" >


<echo>Generating R.java...</echo>

<exec executable="${aapt}" failοnerrοr="true">


<arg value="package" />


<arg value="-m" />


<arg value="-J" />


<arg value="${gen-dir}" />


<arg value="-M" />


<arg value="AndroidManifest.xml" />


<arg value="-S" />

<arg value="${res-dir}" />


<arg value="-I" />


<arg value="${android-jar}" />


</exec>

</target>

<!-- Generate java classes from .aidl files. 暂时没有用到-->


<target name="aidl" depends="init">


<echo>Compiling aidl files into Java classes...</echo>


<apply executable="${aidl}" failοnerrοr="true">


<arg value="-p${android-framework}" />


<arg value="-I${src-dir}" />


<fileset dir="${src-dir}">


<include name="**/*.aidl" />


</fileset>


</apply>


</target>


<!-- 步骤 2. 编译所有class文件. depends="init, generateR, aidl-->


<target name="compile" depends="init, generateR, aidl" >

    <!-- 第三方包 -->

<unjar src="${libs-dir}/android-support-v4.jar"  dest="${bin-dir-classes}" />


<!-- java的版本号 -->

<javac encoding="${jdk-compile-encode}" target="${jdk-ver}" debug="true" extdirs="" srcdir="." destdir="${bin-dir-classes}" bootclasspath="${android-jar}" />


</target>


<!-- 混淆代码-->


<target name="proguard" depends="compile">


<jar basedir="${bin-dir-classes}" destfile="temp.jar" />


<java jar="${proguard-home}/proguard.jar" fork="true" failοnerrοr="true">


<jvmarg value="-Dmaximum.inlined.code.length=32" />


<arg value="-injars temp.jar" />


<arg value="-outjars optimized.jar" />


<arg value="-libraryjars ${android-jar}" />


<arg value="-dontpreverify" />


<arg value="-dontoptimize" />


<arg value="-dontusemixedcaseclassnames" />


<arg value="-repackageclasses ''" />


<arg value="-allowaccessmodification" />


<!-- 要保留自己的主ACTIVITY类 -->

<arg value="-keep public class * extends android.app.Activity" />  

            <arg value="-keep public class * extends android.app.Service" />  

            <arg value="-keep public class * extends android.content.BroadcastReceiver" />  

            <arg value="-keep public class * extends android.content.ContentProvider" />  

            <arg value="-keep public class * extends android.view.View" /> 


<arg value="-optimizationpasses 7" />


<arg value="-verbose" />


<arg value="-dontskipnonpubliclibraryclasses" />


<arg value="-dontskipnonpubliclibraryclassmembers" />


</java>


<delete file="temp.jar" />


<delete dir="${bin-dir-classes}" />


<mkdir dir="${bin-dir-classes}" />


<unzip src="optimized.jar"  dest="${bin-dir-classes}" />


<delete file="optimized.jar" /> 


</target>


<!-- 步骤 3. 转换class文件为dex文件. -->


<target name="dex" depends="proguard">


<echo>Converting compiled files into ${dex-path-absolute}... </echo>


<exec executable="${dx}" failοnerrοr="true">


<arg value="--dex" />


<arg value="--output=${dex-path-absolute}" />


<arg path="${bin-dir-classes-absolute}" />


</exec>


</target>


<!-- 步骤 4. 将资源和清单打包. -->


<target name="package-res-and-assets">


<echo>Packaging resources and assets...</echo>


<exec executable="${aapt}" failοnerrοr="true">


<arg value="package" />


<arg value="-f" />


<arg value="-M" />


<arg value="AndroidManifest.xml" />


<arg value="-S" />


<arg value="${res-dir}" />


<arg value="-A" />


<arg value="${assets-dir}" />


<arg value="-I" />


<arg value="${android-jar}" />


<arg value="-F" />


<arg value="${resources-package}" />


</exec>


</target>


<!-- 步骤 5. 将打好的包编译为APK文件. -->


<target name="debug" depends="dex, package-res-and-assets">


<echo>release:Packaging ${out-debug-package-ospath}, and signing it with a debug key...</echo>


<exec executable="${apk-builder}" 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-path-absolute}" />


<arg value="-rf" />


<arg value="${src-dir-absolute}" />


</exec>


</target>


<!--步骤 6. 签名APK文件 -->


<target name="jarsigner" depends="debug">


<echo> jarsigner ${out-signed-package-ospath}</echo>


<exec executable="${jarsigner}" failοnerrοr="true">


<arg value="-verbose" />


<arg value="-storepass" />


<arg value="${keypass}" />


<arg value="-keystore" />


<arg value="${keystore}" />


<arg value="-signedjar" />


<arg value="${out-signed-package-ospath}" />


<arg value="${out-unsigned-package-ospath}" />


<arg value="${key}" />


</exec>


</target>


<!--zipalign优化android程序-->


<target name="zipalign" depends="jarsigner">


<echo> zipalign ${zipalign-package-ospath}</echo>


<exec executable="${zipalign}" failοnerrοr="true">


<arg value="-v" />


<arg value="-f" />


<arg value="4" />


<arg value="${out-signed-package-ospath}" />


<arg value="${zipalign-package-ospath}" />


</exec>


</target>


<!-- 安装到默认的手机或模拟器-->


<target name="install" depends="zipalign">


<echo>Installing ${out-debug-package} onto default emulator...</echo>


<exec executable="${adb}" failοnerrοr="true">


<arg value="install" />


<arg value="${out-signed-package}" />


</exec>


</target>


</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值