标签:
1.博客概要
本文详细介绍了当今流行的一键生成APP技术。介绍了这种设计思想的来源,介绍了国内外的研究背景,并介绍了这个技术体系中的一些实现细节,欢迎各路大神们多提意见。一键生成技术,说的通俗点就是,要在自有平台上发布若干个代码一致,但包名,版本,引用资源都不同的App,即实现一套代码生成多个不同的APK(注意哦,这一系列的编译打包都是自动化的)。
2.认识一个Android项目
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑。
如上图所示,是一个Android项目的目录结构图,分别介绍如下:
3.Android项目是如何实现编译、打包、签名和发布的,这套流程是怎样的?
知道了一个Android项目的组成之后,我们来看一个apk是如何从Eclipse中的源文件组装成一个apk文件的。
如上图所示,apk的编译过程分为以下几个阶段:
1.用aapt命令生成R.java文件,命令“ aapt package -f -m -J ./gen -S res -M AndroidManifest.xml -I D:\android.jar ”
2.用aidl命令生成相应java文件。
3.用javac命令编译java源文件生成class文件
4.用dx.bat将class文件转换成classes.dex文件
5.用aapt命令生成资源包文件resources
6.用apkbuilder.bat打包资源和classes.dex文件,生成unsigned.apk
7.用jarsinger命令对apk认证,生成signed.apk
4.结合配置文件,详细解释一键生成APP是如何通过build.xml来自动化实现的?
从上面步骤,我们知道了通过一堆命令,可以依次来执行这七个步骤,是通过手动命令来编译打包的。一键生成APP技术则是利用了Ant的自动编译打包apk的技术,就是这么简单。相对应的有如下步骤:
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>
效果等价于,执行了aapt命令生成R.java文件。
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>
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>
下面的步骤,依次类推,详情参考:http://jojol-zhou.iteye.com/blog/729271
5.总结
<?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>
标签:
原文地址:http://www.cnblogs.com/RunForLove/p/5234706.html