android使用ant編譯(rem)

l_serein發表於2012-06-05
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.javaeye.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>

相關文章