Android自动打包、签名、优化、上传ANT脚本

此脚本是针对于android在线更新的SDK,以前android有离线安装包,后来改为直接在线安装进行,两者的成本在于sdk的路径和一些工具类的路径不同。

 

 

 

此脚本用于进行自动编译用户需要的APK安装包,使用时候,请将签名用的*.keystore放在和build.xml同一目录,并命名为android.keystore,(如想更改,请在build.xml 中的jarsigner target进行更改)

 

build.properties

Java代码 复制代码
  1. android.tools=${sdk.folder}tools   
  2. android_version=2.1  
  3. apk.sdk.home=D/:/androidsdktools/android-sdk_r04-windows/android-sdk-windows/   
  4. apk.tools=${apk.sdk.home}tools/   
  5. application.package=com.test.testname   
  6. bin.dir=${jde.home}bin   
  7. app.name=NewAndroid   
  8. app.source.path=G/:/test/   
  9. jdk.home=C/:/Java_1.6_U18/jdk1.6.0_18/   
  10. output.dir=../outputDir   
  11. password=123456  
  12. sdk.folder=D/:/androidsdktools/android-sdk_r04-windows/android-sdk-windows/platforms/android-2.0.1/   
  13. temp.dir=127980239039628  

 

 

 

Build.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="Test" default="debug" basedir=".">  
  3.     <property file="build.properties">  
  4.     </property>  
  5.   
  6.     <!-- The intermediates directory -->  
  7.     <!-- Eclipse uses "bin" for its own output, so we do the same. -->  
  8.     <property name="outdir" value="bin" />  
  9.   
  10.     <!-- ************************************************************************************* -->  
  11.     <!-- No user servicable parts below. -->  
  12.   
  13.     <property name="android-framework" value="${sdk.folder}/framework.aidl" />  
  14.   
  15.     <!-- Input directories -->  
  16.     <property name="resource-dir" value="res" />  
  17.     <property name="asset-dir" value="assets" />  
  18.     <property name="srcdir" value="src" />  
  19.     <condition property="srcdir-ospath" value="${basedir}/${srcdir}" else="${basedir}/${srcdir}">  
  20.         <os family="windows" />  
  21.     </condition>  
  22.   
  23.     <property name="external-libs" value="libs" />  
  24.     <condition property="external-libs-ospath" value="${basedir}/${external-libs}" else="${basedir}/${external-libs}">  
  25.         <os family="windows" />  
  26.     </condition>  
  27.   
  28.     <!-- Output directories -->  
  29.     <property name="outdir-classes" value="${outdir}/classes" />  
  30.     <condition property="outdir-classes-ospath" value="${basedir}/${outdir-classes}" else="${basedir}/${outdir-classes}">  
  31.         <os family="windows" />  
  32.     </condition>  
  33.   
  34.     <condition property="zipalign-package-ospath" value="${basedir}/${output.dir}/${app.name}_for_android_${android_version}_${temp.dir}.apk" else="${basedir}/${output.dir}">  
  35.         <os family="windows" />  
  36.     </condition>  
  37.     <!-- Create R.java in the source directory -->  
  38.     <property name="outdir-r" value="src" />  
  39.   
  40.     <!-- Intermediate files -->  
  41.     <property name="dex-file" value="classes.dex" />  
  42.     <property name="intermediate-dex" value="${outdir}/${dex-file}" />  
  43.     <condition property="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" else="${basedir}/${intermediate-dex}">  
  44.         <os family="windows" />  
  45.     </condition>  
  46.   
  47.     <!-- The final package file to generate -->  
  48.     <property name="resources-package" value="${outdir}/${ant.project.name}.ap_" />  
  49.     <condition property="resources-package-ospath" value="${basedir}/${resources-package}" else="${basedir}/${resources-package}">  
  50.         <os family="windows" />  
  51.     </condition>  
  52.   
  53.     <property name="out-debug-package" value="${outdir}/${ant.project.name}-debug.apk" />  
  54.     <condition property="out-debug-package-ospath" value="${basedir}/${out-debug-package}" else="${basedir}/${out-debug-package}">  
  55.         <os family="windows" />  
  56.     </condition>  
  57.   
  58.     <property name="out-unsigned-package" value="${outdir}/${ant.project.name}-unsigned.apk" />  
  59.     <property name="out-signed-package" value="${outdir}/${ant.project.name}-signed.apk" />  
  60.     <condition property="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" else="${basedir}/${out-unsigned-package}">  
  61.         <os family="windows" />  
  62.     </condition>  
  63.     <condition property="out-signed-package-ospath" value="${basedir}/${out-signed-package}" else="${basedir}/${out-signed-package}">  
  64.         <os family="windows" />  
  65.     </condition>  
  66.   
  67.     <!-- Tools -->  
  68.     <condition property="aapt" value="${android.tools}/aapt.exe" else="${android.tools}/aapt">  
  69.         <os family="windows" />  
  70.     </condition>  
  71.     <condition property="zipalign" value="${apk.tools}/zipalign.exe" else="${apk.tools}/zipalign">  
  72.         <os family="windows" />  
  73.     </condition>  
  74.     <condition property="jarsigner" value="${jdk.home}/bin/jarsigner.exe" else="${jdk.home}/bin/jarsigner">  
  75.         <os family="windows" />  
  76.     </condition>  
  77.     <condition property="aidl" value="${android.tools}/aidl.exe" else="${android.tools}/aidl">  
  78.         <os family="windows" />  
  79.     </condition>  
  80.     <condition property="adb" value="${apk.tools}/adb.exe" else="${apk.tools}/adb">  
  81.         <os family="windows" />  
  82.     </condition>  
  83.     <condition property="dx" value="${android.tools}/dx.bat" else="${android.tools}/dx">  
  84.         <os family="windows" />  
  85.     </condition>  
  86.     <condition property="apk-builder" value="${apk.tools}/apkbuilder.bat" else="${apk.tools}/apkbuilder">  
  87.         <os family="windows" />  
  88.     </condition>  
  89.   
  90.     <property name="android-jar" value="${sdk.folder}/android.jar" />  
  91.   
  92.     <!-- Rules -->  
  93.   
  94.     <!-- Create the output directories if they don't exist yet. -->  
  95.     <target name="dirs" depends="init">  
  96.         <echo>Creating output directories if needed...</echo>  
  97.         <mkdir dir="${outdir}" />  
  98.         <mkdir dir="${outdir-classes}" />  
  99.     </target>  
  100.   
  101.     <!-- Generate the R.java file for this project's resources. -->  
  102.     <target name="resource-src" depends="dirs">  
  103.         <echo>Generating R.java / Manifest.java from the resources...</echo>  
  104.         <exec executable="${aapt}" failonerror="true">  
  105.             <arg value="package" />  
  106.             <arg value="-m" />  
  107.             <arg value="-J" />  
  108.             <arg value="${outdir-r}" />  
  109.             <arg value="-M" />  
  110.             <arg value="AndroidManifest.xml" />  
  111.             <arg value="-S" />  
  112.             <arg value="${resource-dir}" />  
  113.             <arg value="-I" />  
  114.             <arg value="${android-jar}" />  
  115.         </exec>  
  116.     </target>  
  117.   
  118.     <!-- Generate java classes from .aidl files. -->  
  119.     <target name="aidl" depends="dirs">  
  120.         <echo>Compiling aidl files into Java classes...</echo>  
  121.         <apply executable="${aidl}" failonerror="true">  
  122.             <arg value="-p${android-framework}" />  
  123.             <arg value="-I${srcdir}" />  
  124.             <fileset dir="${srcdir}">  
  125.                 <include name="**/*.aidl" />  
  126.             </fileset>  
  127.         </apply>  
  128.     </target>  
  129.   
  130.     <!-- Compile this project's .java files into .class files. -->  
  131.     <target name="compile" depends="dirs, resource-src, aidl">  
  132.         <javac encoding="ascii" target="1.5" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}">  
  133.             <classpath>  
  134.                 <fileset dir="${external-libs}" includes="*.jar" />  
  135.             </classpath>  
  136.         </javac>  
  137.     </target>  
  138.   
  139.     <!-- Convert this project's .class files into .dex files. -->  
  140.     <target name="dex" depends="compile">  
  141.         <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>  
  142.         <apply executable="${dx}" failonerror="true" parallel="true">  
  143.             <arg value="--dex" />  
  144.     <script src="/javascripts/tinymce/themes/advanced/langs/zh.js" type="text/javascript"><!--mce:0--></script><script src="/javascripts/tinymce/plugins/javaeye/langs/zh.js" type="text/javascript"><!--mce:1--></script>      <arg value="--output=${intermediate-dex-ospath}" />  
  145.             <arg path="${outdir-classes-ospath}" />  
  146.             <fileset dir="${external-libs}" includes="*.jar" />  
  147.         </apply>  
  148.     </target>  
  149.   
  150.     <!-- Put the project's resources into the output package file. -->  
  151.     <target name="package-res-and-assets">  
  152.         <echo>Packaging resources and assets...</echo>  
  153.         <exec executable="${aapt}" failonerror="true">  
  154.             <arg value="package" />  
  155.             <arg value="-f" />  
  156.             <arg value="-M" />  
  157.             <arg value="AndroidManifest.xml" />  
  158.             <arg value="-S" />  
  159.             <arg value="${resource-dir}" />  
  160.             <arg value="-A" />  
  161.             <arg value="${asset-dir}" />  
  162.             <arg value="-I" />  
  163.             <arg value="${android-jar}" />  
  164.             <arg value="-F" />  
  165.             <arg value="${resources-package}" />  
  166.         </exec>  
  167.     </target>  
  168.   
  169.     <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->  
  170.     <target name="package-res-no-assets">  
  171.         <echo>Packaging resources...</echo>  
  172.         <exec executable="${aapt}" failonerror="true">  
  173.             <arg value="package" />  
  174.             <arg value="-f" />  
  175.             <arg value="-M" />  
  176.             <arg value="AndroidManifest.xml" />  
  177.             <arg value="-S" />  
  178.             <arg value="${resource-dir}" />  
  179.             <!-- No assets directory -->  
  180.             <arg value="-I" />  
  181.             <arg value="${android-jar}" />  
  182.             <arg value="-F" />  
  183.             <arg value="${resources-package}" />  
  184.         </exec>  
  185.     </target>  
  186.   
  187.     <!-- Invoke the proper target depending on whether or not   
  188.              an assets directory is present. -->  
  189.     <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument   
  190.              only when the assets dir exists. -->  
  191.     <target name="package-res">  
  192.         <available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />  
  193.         <property name="res-target" value="no-assets" />  
  194.         <antcall target="package-res-${res-target}" />  
  195.     </target>  
  196.   
  197.     <!-- Package the application and sign it with a debug key.   
  198.              This is the default target when building. It is used for debug. -->  
  199.     <target name="debug" depends="dex, package-res">  
  200.         <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>  
  201.         <exec executable="${apk-builder}" failonerror="true">  
  202.             <arg value="${out-debug-package-ospath}" />  
  203.             <arg value="-z" />  
  204.             <arg value="${resources-package-ospath}" />  
  205.             <arg value="-f" />  
  206.             <arg value="${intermediate-dex-ospath}" />  
  207.             <arg value="-rf" />  
  208.             <arg value="${srcdir-ospath}" />  
  209.             <arg value="-rj" />  
  210.             <arg value="${external-libs-ospath}" />  
  211.         </exec>  
  212.     </target>  
  213.   
  214.     <!-- Package the application without signing it.   
  215.              This allows for the application to be signed later with an official publishing key. -->  
  216.     <target name="release" depends="dex, package-res">  
  217.         <echo>Packaging ${out-unsigned-package} for release...</echo>  
  218.         <exec executable="${apk-builder}" failonerror="true">  
  219.             <arg value="${out-unsigned-package-ospath}" />  
  220.             <arg value="-u" />  
  221.             <arg value="-z" />  
  222.             <arg value="${resources-package-ospath}" />  
  223.             <arg value="-f" />  
  224.             <arg value="${intermediate-dex-ospath}" />  
  225.             <arg value="-rf" />  
  226.             <arg value="${srcdir-ospath}" />  
  227.             <arg value="-rj" />  
  228.             <arg value="${external-libs-ospath}" />  
  229.         </exec>  
  230.         <echo>It will need to be signed with jarsigner before being published.</echo>  
  231.     </target>  
  232.   
  233.     <!-- Install the package on the default emulator -->  
  234.     <target name="install" depends="debug">  
  235.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
  236.         <exec executable="${adb}" failonerror="true">  
  237.             <arg value="install" />  
  238.             <arg value="${out-debug-package}" />  
  239.         </exec>  
  240.     </target>  
  241.   
  242.     <target name="reinstall" depends="debug">  
  243.         <echo>Installing ${out-debug-package} onto default emulator...</echo>  
  244.         <exec executable="${adb}" failonerror="true">  
  245.             <arg value="install" />  
  246.             <arg value="-r" />  
  247.             <arg value="${out-debug-package}" />  
  248.         </exec>  
  249.     </target>  
  250.   
  251.     <!-- Uinstall the package from the default emulator -->  
  252.     <target name="uninstall">  
  253.         <echo>Uninstalling ${application.package} from the default emulator...</echo>  
  254.         <exec executable="${adb}" failonerror="true">  
  255.             <arg value="uninstall" />  
  256.             <arg value="${application.package}" />  
  257.         </exec>  
  258.     </target>  
  259.   
  260.     <!--初始化目录-->  
  261.     <target name="init" depends="Copy_Ressource">  
  262.         <echo message="Init output directory.....">  
  263.         </echo>  
  264.         <mkdir dir="${output.dir}" />  
  265.     </target>  
  266.     <!--拷贝资源-->  
  267.     <target name="Copy_Ressource">  
  268.         <echo message="Copy app resource. ">  
  269.         </echo>  
  270.         <copy todir="${asset-dir}" overwrite="true" failonerror="false">  
  271.             <fileset dir="${app.source.path}">  
  272.                 <include name="*.*" />  
  273.             </fileset>  
  274.         </copy>  
  275.        
  276.     </target>  
  277.     <!--进行签名-->  
  278.     <target name="jarsigner" depends="release">  
  279.         <exec executable="${jarsigner}" failonerror="true">  
  280.             <arg value="-verbose" />  
  281.             <arg value="-storepass" />  
  282.             <arg value="${password}" />  
  283.             <arg value="-keystore" />  
  284.             <arg value="android.keystore" />  
  285.             <arg value="-signedjar" />  
  286.             <arg value="${out-signed-package-ospath}" />  
  287.             <arg value="${out-unsigned-package-ospath}" />  
  288.             <arg value="android.keystore" />  
  289.         </exec>  
  290.     </target>  
  291.     <!--进行优化-->  
  292.     <target name="zipalign" depends="jarsigner">  
  293.         <exec executable="${zipalign}" failonerror="true">  
  294.             <arg value="-v" />  
  295.             <arg value="-f" />  
  296.             <arg value="4" />  
  297.             <arg value="${out-signed-package-ospath}" />  
  298.             <arg value="${zipalign-package-ospath}" />  
  299.         </exec>  
  300.     </target>  
  301.     <!--直接上传到手机中去-->  
  302.     <target name="adb" depends="zipalign">  
  303.             <exec executable="${adb}" failonerror="true">  
  304.                 <arg value="install" />  
  305.                 <arg value="-r" />  
  306.                 <arg value="${zipalign-package-ospath}" />  
  307.             </exec>  
  308.         </target>  
  309. </project>  
  310. 如果keystore不在此目录,
    你就修改 :

       <target name="jarsigner" depends="release"> 
            <exec executable="${jarsigner}" failonerror="true"> 
                <arg value="-verbose" /> 
                <arg value="-storepass" /> 
                <arg value="${password}" /> 
                <arg value="-keystore" /> 
                <arg value="android.keystore" /> 
                <arg value="-signedjar" /> 
                <arg value="${out-signed-package-ospath}" /> 
                <arg value="${out-unsigned-package-ospath}" /> 
                <arg value="android" /> 
            </exec> 
        </target> 

    中的
    <arg value="android.keystore" /> 
      <arg value="android" /> 
    路径,

    这个两个参数值要一样即可

你可能感兴趣的:(android,优化,windows,脚本,encoding,parallel)