轉[Android]APK打包過程詳解(一)

大搜車-自娛發表於2013-11-18
原文地址:http://jojol-zhou.iteye.com/blog/729271
如作者要求,請留言,博主可刪除此博文。

這篇文章對android打包過程的每個步驟做了詳細的解釋,這裡我給一個Maven簡單版本作為概括

第一步, aapt,如果pom依賴中有apklib,將會先解壓,與原檔案一起產生aidl r等檔案。
第二步, aidl,javac,編譯, 和傳統maven編譯一樣, 需要解決依賴, 包衝突等還有scope。
第三步, proguard,混淆,釋出包需要,會把所有依賴的jar包放一起處理, 形成混淆後的單一jar包
第四步, Dex, 混淆後的jar包, 處理成delvik認識的classes.dex。
第五步, apkbuilder,打APK,將把所有資原始檔, 和dex, 打包成為apk。
​第六步, 有maven外掛支援,將替換相應的資原始檔和配置項, 最後再次呼叫aapt打包。
最後 , jarsigner,ziplign就是類似簽名,對齊等操作不表

​渠道包的關鍵是在第六步, 注意是一次編譯class,一次混淆。便可打出n多個渠道包。

​每一步都是過程明確,產出目標明確,可以使用maven。

如下引用原文:


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簽名。

[img]http://dl.iteye.com/upload/attachment/288325/ee45e498-97d2-3d40-87c3-5c4247cb2ff8.png[/img]

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>


Ant配置檔案:

<?xml version="1.0" ?>

<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

相關文章