android 命令行打包
引用:http://jojol-zhou.iteye.com/blog/729271
Author:Jojol
Data:2010.08.04
1.Android程序编译、打包、签名、发布的三种方式:
方式一:命令行手动编译打包
方式二:使用ant自动编译打包
方式三:使用eclipse+ADT编译打包
2.Android编译、打包的步骤:
2.1第一步 生成R.java类文件:
Eclipse中会自动生成R.java,ant和命令行使用android SDK提供的aapt.ext程序生成R.java。
2.2第二步 将.aidl文件生成.java类文件:
Eclipse中自动生成,ant和命令行使用android SDK提供的aidl.exe生成.java文件。
2.3第三步 编译.java类文件生成class文件:
Eclipse中自动生成,ant和命令行使用jdk的javac编译java类文件生成class文件。
2.4第四步 将class文件打包生成classes.dex文件:
Eclipse中自动生成,ant和命令行使用android SDK提供的dx.bat命令行脚本生成classes.dex文件。
2.5第五步 打包资源文件(包括res、assets、androidmanifest.xml等):
Eclipse中自动生成,ant和命令行使用Android SDK提供的aapt.exe生成资源包文件。
2.6第六步 生成未签名的apk安装文件:
Eclipse中自动生成debug签名文件存放在bin目录中,ant和命令行使用android SDK提供的apkbuilder.bat命令脚本生成未签名的apk安装文件。
2.7第七步 对未签名的apk进行签名生成签名后的android文件:
Eclipse中使用Android Tools进行签名,ant和命令行使用jdk的jarsigner对未签名的包进行apk签名。
3.命令行手动编译打包详解
详见:http://jojol-zhou.iteye.com/blog/729254
4.ant自动打包android程序详解
4.1生成R.java类文件:
<!-- Generate the R.java file for this project's resources. -->
<target name="resource-src" depends="copy">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg value="${outdir-gen}" />
<arg value="-M" />
<arg value="AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
4.2将.aidl文件生成.java类文件:
<!-- Generate java classes from .aidl files. -->
<target name="aidl" depends="copy">
<echo>Compiling aidl files into Java classes...</echo>
<apply executable="${aidl}" failonerror="true">
<arg value="-p${android-framework}" />
<arg value="-I${srcdir}" />
<fileset dir="${srcdir}">
<include name="**/*.aidl"/>
</fileset>
</apply>
</target>
4.3编译.java类文件生成class文件:
<!-- Compile this project's .java files into .class files. -->
<target name="compile" depends="copy, resource-src, aidl">
<javac encoding="GB18030" target="1.5" debug="true" extdirs=""
srcdir="."
destdir="${outdir-classes}"
bootclasspath="${android-jar}">
<classpath>
<fileset dir="${external-libs}" includes="*.jar"/>
</classpath>
</javac>
</target>
4.4第四步 将class文件打包生成classes.dex文件:
<!-- Convert this project's .class files into .dex files. -->
<target name="dex" depends="compile">
<echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
<apply executable="${dx}" failonerror="true" parallel="true">
<arg value="--dex" />
<arg value="--output=${intermediate-dex-ospath}" />
<arg path="${outdir-obfuscate-classes-ospath}" />
<fileset dir="${external-libs}" includes="*.jar"/>
</apply>
</target>
4.5第五步 打包资源文件(包括res、assets、androidmanifest.xml等):
<!-- Put the project's resources into the output package file. -->
<target name="package-res-and-assets">
<echo>Packaging resources and assets...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg value="AndroidManifest.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>
4.6第六步 生成未签名的apk安装文件:
<target name="package" depends="dex, package-res">
<echo>Packaging ${out-unsigned-package} for release...</echo>
<exec executable="${apk-builder}" failonerror="true">
<arg value="${out-unsigned-package-ospath}" />
<arg value="-u" />
<arg value="-z" />
<arg value="${resources-package-ospath}" />
<arg value="-f" />
<arg value="${intermediate-dex-ospath}" />
<arg value="-rf" />
<arg value="${srcdir-ospath}" />
<arg value="-rj" />
<arg value="${external-libs-ospath}" />
</exec>
<echo>It will need to be signed with jarsigner before being published.</echo>
</target>
4.7第七步 对未签名的apk进行签名生成签名后的android文件:
<target name="jarsigner" depends="package">
<echo>Packaging ${out-unsigned-package} for release...</echo>
<exec executable="${jarsigner}" failonerror="true">
<arg value="-verbose" />
<arg value="-storepass" />
<arg value="byread002" />
<arg value="-keypass" />
<arg value="byread002" />
<arg value="-keystore" />
<arg value="bbyread.keystore" />
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}" />
<arg value="byread" />
</exec>
</target>
- <?xml version="1.0" ?>
- <!--
- byread Package Utility
- Author: Jojol Zhou
- Date: 20100804
- -->
- <project name="Byread" default="debug">
- <!-- SDK Locations -->
- <property name="sdk2.2-folder" value="F:\explorer\android-sdk-windows2.2" />
- <property name="sdk-folder" value="${sdk2.2-folder}/platforms/android-3" />
- <property name="sdk-tools" value="${sdk-folder}/tools" />
- <property name="android-tools" value="${sdk2.2-folder}/tools" />
- <property name="proguardpath" location="${wtkhome}/lib/proguard" />
- <!-- step 1.generate R.java by aapt 2.compile java to class by javac。exe 3.generate classes.dex by dx.bat -->
- <!-- 4.package resources by aapt 5. package resource and classes.dex by apkbuilder.bat 6.sign apk by jarsinger -->
- <!-- Tools -->
- <property name="aapt" value="${sdk-tools}/aapt.exe" />
- <property name="dx" value="${sdk-tools}/dx.bat" />
- <property name="apk-builder" value="${android-tools}/apkbuilder.bat" />
- <property name="aidl" value="${android-tools}/aidl.exe" />
- <property name="adb" value="${android-tools}/adb.exe" />
- <property name="android-jar" value="${sdk-folder}/android.jar" />
- <property name="jarsigner" value="C:\Program Files\Java\jdk1.6.0_07\bin\jarsigner.exe" />
- <!-- Application Package Name -->
- <property name="application-package" value="com.byread.reader" />
- <property name="useragent" value="byAndroidWeb" />
- <property name="version" value="1.02" />
- <!-- The intermediates directory -->
- <!-- Eclipse uses "bin" for its own output, so we do the same. -->
- <property name="outdir-bin" value="bin" />
- <property name="outdir-gen" value="gen" />
- <!-- source directories -->
- <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-libs" value="libs" />
- <property name="external-libs-ospath" value="${basedir}/${external-libs}" />
- <!-- dest directories -->
- <property name="des-resource-dir" value="${outdir-bin}/res" />
- <property name="des-asset-dir" value="${outdir-bin}/assets" />
- <property name="des-srcdir" value="${outdir-bin}/src" />
- <property name="des-srcdir-ospath" value="${basedir}/${outdir-bin}/${srcdir}" />
- <property name="des-external-libs" value="${outdir-bin}/libs" />
- <property name="des-external-libs-ospath" value="${basedir}/${outdir-bin}/${external-libs}" />
- <!-- Output directories -->
- <property name="outdir-classes" value="${outdir-bin}/src" />
- <property name="outdir-obfuscate-classes" value="${outdir-bin}/classes" />
- <property name="outdir-obfuscate-classes-ospath" value="${basedir}/${outdir-obfuscate-classes}" />
- <!-- Intermediate files -->
- <property name="dex-file" value="classes.dex" />
- <property name="intermediate-dex" value="${outdir-bin}/${dex-file}" />
- <property name="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" />
- <!-- The final package file to generate -->
- <property name="resources-package" value="${outdir-bin}/${ant.project.name}" />
- <property name="resources-package-ospath" value="${basedir}/${resources-package}" />
- <property name="out-debug-package" value="${outdir-bin}/${ant.project.name}-debug.apk" />
- <property name="out-debug-package-ospath" value="${basedir}/${out-debug-package}" />
- <property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />
- <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
- <property name="out-signed-package" value="${useragent}\${ant.project.name}.apk" />
- <property name="out-signed-package-ospath" value="${basedir}\${out-signed-package}" />
- <!-- init -->
- <target name="init">
- <echo>Creating all output directories </echo>
- <delete dir="${outdir-bin}" />
- <delete dir="${useragent}" />
- <mkdir dir="${outdir-bin}" />
- <mkdir dir="${outdir-classes}" />
- <mkdir dir="${useragent}" />
- </target>
- <!-- copy original strings and modify useragent -->
- <target name="copy" depends="init">
- <echo>copy files to output folder</echo>
- <delete file="${resource-dir}\values\strings.xml"/>
- <copy file="strings.xml" todir="${resource-dir}\values" />
- <replace file="${resource-dir}\values\strings.xml" token="@USERAGENT@" value="${useragent}" encoding="utf-8"/>
- </target>
- <!-- Generate the R.java file for this project's resources. -->
- <target name="resource-src" depends="copy">
- <echo>Generating R.java / Manifest.java from the resources...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-m" />
- <arg value="-J" />
- <arg value="${outdir-gen}" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <arg value="-I" />
- <arg value="${android-jar}" />
- </exec>
- </target>
- <!-- Generate java classes from .aidl files. -->
- <target name="aidl" depends="copy">
- <echo>Compiling aidl files into Java classes...</echo>
- <apply executable="${aidl}" failonerror="true">
- <arg value="-p${android-framework}" />
- <arg value="-I${srcdir}" />
- <fileset dir="${srcdir}">
- <include name="**/*.aidl"/>
- </fileset>
- </apply>
- </target>
- <!-- Compile this project's .java files into .class files. -->
- <target name="compile" depends="copy, resource-src, aidl">
- <javac encoding="GB18030" target="1.5" debug="true" extdirs=""
- srcdir="."
- destdir="${outdir-classes}"
- bootclasspath="${android-jar}">
- <classpath>
- <fileset dir="${external-libs}" includes="*.jar"/>
- </classpath>
- </javac>
- </target>
- <!-- Convert this project's .class files into .dex files. -->
- <target name="dex" depends="compile">
- <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
- <apply executable="${dx}" failonerror="true" parallel="true">
- <arg value="--dex" />
- <arg value="--output=${intermediate-dex-ospath}" />
- <arg path="${outdir-obfuscate-classes-ospath}" />
- <fileset dir="${external-libs}" includes="*.jar"/>
- </apply>
- </target>
- <!-- Put the project's resources into the output package file. -->
- <target name="package-res-and-assets">
- <echo>Packaging resources and assets...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-M" />
- <arg value="AndroidManifest.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>
- <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->
- <target name="package-res-no-assets">
- <echo>Packaging resources...</echo>
- <exec executable="${aapt}" failonerror="true">
- <arg value="package" />
- <arg value="-f" />
- <arg value="-M" />
- <arg value="AndroidManifest.xml" />
- <arg value="-S" />
- <arg value="${resource-dir}" />
- <!-- No assets directory -->
- <arg value="-I" />
- <arg value="${android-jar}" />
- <arg value="-F" />
- <arg value="${resources-package}" />
- </exec>
- </target>
- <!-- Invoke the proper target depending on whether or not
- an assets directory is present. -->
- <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument
- only when the assets dir exists. -->
- <target name="package-res">
- <available file="${asset-dir}" type="dir"
- property="res-target" value="and-assets" />
- <property name="res-target" value="no-assets" />
- <antcall target="package-res-${res-target}" />
- </target>
- <!-- Package the application and sign it with a debug key.
- This is the default target when building. It is used for debug. -->
- <target name="debug" depends="dex, package-res">
- <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
- <exec executable="${apk-builder}" failonerror="true">
- <arg value="${out-debug-package-ospath}" />
- <arg value="-z" />
- <arg value="${resources-package-ospath}" />
- <arg value="-f" />
- <arg value="${intermediate-dex-ospath}" />
- <arg value="-rf" />
- <arg value="${srcdir-ospath}" />
- <arg value="-rj" />
- <arg value="${external-libs-ospath}" />
- </exec>
- </target>
- <!-- Package the application without signing it.
- This allows for the application to be signed later with an official publishing key. -->
- <target name="package" depends="dex, package-res">
- <echo>Packaging ${out-unsigned-package} for release...</echo>
- <exec executable="${apk-builder}" failonerror="true">
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="-u" />
- <arg value="-z" />
- <arg value="${resources-package-ospath}" />
- <arg value="-f" />
- <arg value="${intermediate-dex-ospath}" />
- <arg value="-rf" />
- <arg value="${srcdir-ospath}" />
- <arg value="-rj" />
- <arg value="${external-libs-ospath}" />
- </exec>
- <echo>It will need to be signed with jarsigner before being published.</echo>
- </target>
- <target name="jarsigner" depends="package">
- <echo>Packaging ${out-unsigned-package} for release...</echo>
- <exec executable="${jarsigner}" failonerror="true">
- <arg value="-verbose" />
- <arg value="-storepass" />
- <arg value="byread002" />
- <arg value="-keypass" />
- <arg value="byread002" />
- <arg value="-keystore" />
- <arg value="bbyread.keystore" />
- <arg value="-signedjar" />
- <arg value="${out-signed-package-ospath}" />
- <arg value="${out-unsigned-package-ospath}" />
- <arg value="byread" />
- </exec>
- </target>
- <target name="release" depends="jarsigner">
- <echo>release for release...</echo>
- </target>
- <!-- Install the package on the default emulator -->
- <target name="install" depends="debug">
- <echo>Installing ${out-debug-package} onto default emulator...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg value="install" />
- <arg value="${out-debug-package}" />
- </exec>
- </target>
- <target name="reinstall" depends="debug">
- <echo>Installing ${out-debug-package} onto default emulator...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg value="install" />
- <arg value="-r" />
- <arg value="${out-debug-package}" />
- </exec>
- </target>
- <!-- Uinstall the package from the default emulator -->
- <target name="uninstall">
- <echo>Uninstalling ${application-package} from the default emulator...</echo>
- <exec executable="${adb}" failonerror="true">
- <arg value="uninstall" />
- <arg value="${application-package}" />
- </exec>
- </target>
- </project>
5.eclipse打包签名
详见:http://jojol-zhou.iteye.com/blog/719428
参考:http://sinfrancis.iteye.com/blog/721582
http://asantoso.wordpress.com/2009/09/15/how-to-build-android-application-package-apk-from-the-command-line-using-the-sdk-tools-continuously-integrated-using-cruisecontrol/