zoukankan      html  css  js  c++  java
  • 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> 

    Java代码  收藏代码
    1. <?xml version="1.0" ?>  
    2.     <!--  
    3.   byread Package Utility  
    4.   Author: Jojol Zhou  
    5.   Date: 20100804  
    6.     -->  
    7. <project name="Byread" default="debug">  
    8.     <!-- SDK Locations -->  
    9.     <property name="sdk2.2-folder" value="F:\explorer\android-sdk-windows2.2" />  
    10.     <property name="sdk-folder" value="${sdk2.2-folder}/platforms/android-3" />  
    11.     <property name="sdk-tools" value="${sdk-folder}/tools" />  
    12.     <property name="android-tools" value="${sdk2.2-folder}/tools" />  
    13.     <property name="proguardpath" location="${wtkhome}/lib/proguard" />  
    14.   
    15.         <!-- step 1.generate R.java by aapt  2.compile java to class by javac。exe 3.generate classes.dex by dx.bat  -->  
    16.         <!-- 4.package resources by aapt 5package resource and classes.dex by apkbuilder.bat 6.sign apk by jarsinger -->  
    17.     <!-- Tools -->  
    18.     <property name="aapt" value="${sdk-tools}/aapt.exe" />  
    19.     <property name="dx" value="${sdk-tools}/dx.bat" />  
    20.     <property name="apk-builder" value="${android-tools}/apkbuilder.bat" />  
    21.     <property name="aidl" value="${android-tools}/aidl.exe" />  
    22.     <property name="adb" value="${android-tools}/adb.exe" />  
    23.     <property name="android-jar" value="${sdk-folder}/android.jar" />  
    24.     <property name="jarsigner" value="C:\Program Files\Java\jdk1.6.0_07\bin\jarsigner.exe" />  
    25.   
    26.     <!-- Application Package Name -->  
    27.     <property name="application-package" value="com.byread.reader" />  
    28.     <property name="useragent" value="byAndroidWeb" />  
    29.     <property name="version" value="1.02" />  
    30.     <!-- The intermediates directory -->  
    31.     <!-- Eclipse uses "bin" for its own output, so we do the same. -->  
    32.     <property name="outdir-bin" value="bin" />  
    33.     <property name="outdir-gen" value="gen" />  
    34.     <!-- source directories -->  
    35.         <property name="resource-dir" value="res" />  
    36.     <property name="asset-dir" value="assets" />  
    37.     <property name="srcdir" value="src" />  
    38.     <property name="srcdir-ospath" value="${basedir}/${srcdir}" />  
    39.     <property name="external-libs" value="libs" />  
    40.     <property name="external-libs-ospath" value="${basedir}/${external-libs}" />  
    41.       
    42.     <!-- dest directories -->  
    43.     <property name="des-resource-dir" value="${outdir-bin}/res" />  
    44.     <property name="des-asset-dir" value="${outdir-bin}/assets" />  
    45.     <property name="des-srcdir" value="${outdir-bin}/src" />  
    46.     <property name="des-srcdir-ospath" value="${basedir}/${outdir-bin}/${srcdir}" />  
    47.     <property name="des-external-libs" value="${outdir-bin}/libs" />  
    48.     <property name="des-external-libs-ospath" value="${basedir}/${outdir-bin}/${external-libs}" />  
    49.       
    50.     <!-- Output directories -->  
    51.     <property name="outdir-classes" value="${outdir-bin}/src" />  
    52.     <property name="outdir-obfuscate-classes" value="${outdir-bin}/classes" />  
    53.     <property name="outdir-obfuscate-classes-ospath" value="${basedir}/${outdir-obfuscate-classes}" />  
    54.   
    55.     <!-- Intermediate files -->  
    56.     <property name="dex-file" value="classes.dex" />  
    57.     <property name="intermediate-dex" value="${outdir-bin}/${dex-file}" />  
    58.     <property name="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" />  
    59.   
    60.     <!-- The final package file to generate -->  
    61.     <property name="resources-package" value="${outdir-bin}/${ant.project.name}" />  
    62.     <property name="resources-package-ospath" value="${basedir}/${resources-package}" />  
    63.     <property name="out-debug-package" value="${outdir-bin}/${ant.project.name}-debug.apk" />  
    64.     <property name="out-debug-package-ospath" value="${basedir}/${out-debug-package}" />  
    65.     <property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />  
    66.     <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />  
    67.         <property name="out-signed-package" value="${useragent}\${ant.project.name}.apk" />  
    68.         <property name="out-signed-package-ospath" value="${basedir}\${out-signed-package}" />  
    69.           
    70.   
    71.     <!-- init -->  
    72.         <target name="init">  
    73.         <echo>Creating all output directories </echo>  
    74.         <delete dir="${outdir-bin}" />  
    75.         <delete dir="${useragent}" />  
    76.         <mkdir dir="${outdir-bin}" />  
    77.         <mkdir dir="${outdir-classes}" />  
    78.         <mkdir dir="${useragent}" />  
    79.     </target>  
    80.       
    81.     <!-- copy original strings and modify useragent -->  
    82.     <target name="copy" depends="init">  
    83.         <echo>copy files to output folder</echo>  
    84.         <delete file="${resource-dir}\values\strings.xml"/>  
    85.         <copy file="strings.xml" todir="${resource-dir}\values" />  
    86.         <replace file="${resource-dir}\values\strings.xml" token="@USERAGENT@" value="${useragent}" encoding="utf-8"/>  
    87.     </target>  
    88.   
    89.     <!-- Generate the R.java file for this project's resources. -->  
    90.     <target name="resource-src" depends="copy">  
    91.         <echo>Generating R.java / Manifest.java from the resources...</echo>  
    92.         <exec executable="${aapt}" failonerror="true">  
    93.                     <arg value="package" />  
    94.                     <arg value="-m" />  
    95.                     <arg value="-J" />  
    96.                     <arg value="${outdir-gen}" />  
    97.                     <arg value="-M" />  
    98.                     <arg value="AndroidManifest.xml" />  
    99.                     <arg value="-S" />  
    100.                     <arg value="${resource-dir}" />  
    101.                     <arg value="-I" />  
    102.                     <arg value="${android-jar}" />  
    103.                 </exec>  
    104.     </target>  
    105.   
    106.     <!-- Generate java classes from .aidl files. -->  
    107.     <target name="aidl" depends="copy">  
    108.         <echo>Compiling aidl files into Java classes...</echo>  
    109.         <apply executable="${aidl}" failonerror="true">  
    110.             <arg value="-p${android-framework}" />  
    111.             <arg value="-I${srcdir}" />  
    112.             <fileset dir="${srcdir}">  
    113.                 <include name="**/*.aidl"/>  
    114.             </fileset>  
    115.         </apply>  
    116.     </target>  
    117.   
    118.     <!-- Compile this project's .java files into .class files. -->  
    119.     <target name="compile" depends="copy, resource-src, aidl">  
    120.         <javac encoding="GB18030" target="1.5" debug="true" extdirs=""  
    121.                 srcdir="."  
    122.                 destdir="${outdir-classes}"  
    123.                 bootclasspath="${android-jar}">  
    124.             <classpath>  
    125.                 <fileset dir="${external-libs}" includes="*.jar"/>  
    126.             </classpath>  
    127.          </javac>  
    128.     </target>  
    129.       
    130.   
    131.   
    132.   
    133.     <!-- Convert this project's .class files into .dex files. -->  
    134.     <target name="dex" depends="compile">  
    135.         <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>  
    136.         <apply executable="${dx}" failonerror="true" parallel="true">  
    137.             <arg value="--dex" />  
    138.             <arg value="--output=${intermediate-dex-ospath}" />  
    139.             <arg path="${outdir-obfuscate-classes-ospath}" />  
    140.             <fileset dir="${external-libs}" includes="*.jar"/>  
    141.         </apply>  
    142.     </target>  
    143.   
    144.     <!-- Put the project's resources into the output package file. -->  
    145.     <target name="package-res-and-assets">  
    146.         <echo>Packaging resources and assets...</echo>  
    147.         <exec executable="${aapt}" failonerror="true">  
    148.             <arg value="package" />  
    149.             <arg value="-f" />  
    150.             <arg value="-M" />  
    151.             <arg value="AndroidManifest.xml" />  
    152.             <arg value="-S" />  
    153.             <arg value="${resource-dir}" />  
    154.             <arg value="-A" />  
    155.             <arg value="${asset-dir}" />  
    156.             <arg value="-I" />  
    157.             <arg value="${android-jar}" />  
    158.             <arg value="-F" />  
    159.             <arg value="${resources-package}" />  
    160.         </exec>  
    161.     </target>  
    162.   
    163.     <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->  
    164.     <target name="package-res-no-assets">  
    165.         <echo>Packaging resources...</echo>  
    166.         <exec executable="${aapt}" failonerror="true">  
    167.             <arg value="package" />  
    168.             <arg value="-f" />  
    169.             <arg value="-M" />  
    170.             <arg value="AndroidManifest.xml" />  
    171.             <arg value="-S" />  
    172.             <arg value="${resource-dir}" />  
    173.             <!-- No assets directory -->  
    174.             <arg value="-I" />  
    175.             <arg value="${android-jar}" />  
    176.             <arg value="-F" />  
    177.             <arg value="${resources-package}" />  
    178.         </exec>  
    179.     </target>  
    180.   
    181.     <!-- Invoke the proper target depending on whether or not  
    182.          an assets directory is present. -->  
    183.     <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument  
    184.          only when the assets dir exists. -->  
    185.     <target name="package-res">  
    186.         <available file="${asset-dir}" type="dir"  
    187.                 property="res-target" value="and-assets" />  
    188.         <property name="res-target" value="no-assets" />  
    189.         <antcall target="package-res-${res-target}" />  
    190.     </target>  
    191.   
    192.     <!-- Package the application and sign it with a debug key.  
    193.          This is the default target when building. It is used for debug. -->  
    194.     <target name="debug" depends="dex, package-res">  
    195.         <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>  
    196.         <exec executable="${apk-builder}" failonerror="true">  
    197.             <arg value="${out-debug-package-ospath}" />  
    198.             <arg value="-z" />  
    199.             <arg value="${resources-package-ospath}" />  
    200.             <arg value="-f" />  
    201.             <arg value="${intermediate-dex-ospath}" />  
    202.             <arg value="-rf" />  
    203.             <arg value="${srcdir-ospath}" />  
    204.             <arg value="-rj" />  
    205.             <arg value="${external-libs-ospath}" />  
    206.         </exec>  
    207.     </target>  
    208.   
    209.     <!-- Package the application without signing it.  
    210.          This allows for the application to be signed later with an official publishing key. -->  
    211.     <target name="package" depends="dex, package-res">  
    212.         <echo>Packaging ${out-unsigned-packagefor release...</echo>  
    213.         <exec executable="${apk-builder}" failonerror="true">  
    214.             <arg value="${out-unsigned-package-ospath}" />  
    215.             <arg value="-u" />  
    216.             <arg value="-z" />  
    217.             <arg value="${resources-package-ospath}" />  
    218.             <arg value="-f" />  
    219.             <arg value="${intermediate-dex-ospath}" />  
    220.             <arg value="-rf" />  
    221.             <arg value="${srcdir-ospath}" />  
    222.             <arg value="-rj" />  
    223.             <arg value="${external-libs-ospath}" />  
    224.         </exec>  
    225.         <echo>It will need to be signed with jarsigner before being published.</echo>  
    226.     </target>  
    227.       
    228.         <target name="jarsigner" depends="package">  
    229.             <echo>Packaging ${out-unsigned-packagefor release...</echo>  
    230.             <exec executable="${jarsigner}" failonerror="true">  
    231.                 <arg value="-verbose" />  
    232.                 <arg value="-storepass" />  
    233.                 <arg value="byread002" />  
    234.                 <arg value="-keypass" />  
    235.                 <arg value="byread002" />  
    236.                 <arg value="-keystore" />  
    237.                 <arg value="bbyread.keystore" />  
    238.                 <arg value="-signedjar" />  
    239.                 <arg value="${out-signed-package-ospath}" />  
    240.                 <arg value="${out-unsigned-package-ospath}" />  
    241.                 <arg value="byread" />  
    242.             </exec>  
    243.         </target>  
    244.           
    245.         <target name="release" depends="jarsigner">  
    246.         <echo>release for release...</echo>  
    247.     </target>  
    248.       
    249.     <!-- Install the package on the default emulator -->  
    250.     <target name="install" depends="debug">  
    251.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
    252.         <exec executable="${adb}" failonerror="true">  
    253.             <arg value="install" />  
    254.             <arg value="${out-debug-package}" />  
    255.         </exec>  
    256.     </target>  
    257.   
    258.     <target name="reinstall" depends="debug">  
    259.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
    260.         <exec executable="${adb}" failonerror="true">  
    261.             <arg value="install" />  
    262.             <arg value="-r" />  
    263.             <arg value="${out-debug-package}" />  
    264.         </exec>  
    265.     </target>  
    266.   
    267.     <!-- Uinstall the package from the default emulator -->  
    268.     <target name="uninstall">  
    269.         <echo>Uninstalling ${application-package} from the default emulator...</echo>  
    270.         <exec executable="${adb}" failonerror="true">  
    271.             <arg value="uninstall" />  
    272.             <arg value="${application-package}" />  
    273.         </exec>  
    274.     </target>  
    275.   
    276. </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/ 

  • 相关阅读:
    druid-1.0.13 数据库配置文件密码加密
    PostConstruct注解
    easyui formatter 返回easyui组件
    小师妹问 easyUI mergeCells 行合并后表头和内容对不齐
    Java Split以竖线作为分隔符
    Integer比较值的时候小心使用
    js 关键字 in
    Asp.net中防止用户多次登录的方法
    C#取得站点跟目录
    解读支付宝接口实现步骤
  • 原文地址:https://www.cnblogs.com/sode/p/2329982.html
Copyright © 2011-2022 走看看