android 应用 Ant脚本自动编译、打包、代码混淆、签名、安装等

时间:2022-12-31 18:40:38

build.properties  内容:

sdk.home= D:/tool/android/android-sdk_r16-windows/android-sdk-windows
#sdk/u7f16/u8bd1/u7248/u672c
sdk.platform=android-8
#Jdk/u76ee/u5f55
jdk.home=D:/tool/jdk1.6.0_01/jdk1.6.0_01
#/u6570/u5b57/u7b7e/u540d/u914d/u7f6e
keystore.password=123456
keystore.validity=10000
keystore.name=monitor.keystore
keystore.alias=monitor


#/u5e94/u7528/u5305/u540d/uff0c/u7528/u4e8eunistall
application.package=com.pch.monitor



build.xml  内容:


<?xml version="1.0" encoding="utf-8"?>
<!-- Build an Android project.                        -->
<project name="Monitor_Android" default="10zipalign" basedir=".">
    
    <property file="build.properties" />

    <!-- The absolute paths for the tools. -->
    <property name="android.framework" value="${sdk.home}/platforms/${sdk.platform}/framework.aidl"></property>
    <property name="android.core" value="${sdk.home}/platforms/${sdk.platform}/android.jar"></property>
<property name="android.proguard" value="${sdk.home}/tools/proguard/lib/proguard.jar"></property>

    <property name="jarsigner" value="${jdk.home}/bin/jarsigner.exe"></property>
    <property name="zipalign" value="${sdk.home}/tools/zipalign.exe"></property>
    <property name="dx" value="${sdk.home}/platform-tools/dx.bat"></property>
    <property name="apk-builder" value="${sdk.home}/tools/apkbuilder.bat"></property>
    <property name="aapt" value="${sdk.home}/platform-tools/aapt.exe"></property>
    <property name="aidl" value="${sdk.home}/platform-tools/aidl.exe"></property>
<property name="adb" value="${sdk.home}/platform-tools/adb.exe"></property>

    <!-- The absolute path. -->
    <property name="src.abs" value="${basedir}/src"></property>
<property name="framework_src.abs" value="${basedir}/framework_src"></property>
    <property name="lib.abs" value="${basedir}/libs"></property>
    <property name="bin.abs" value="${basedir}/bin"></property>
<property name="gen.abs" value="${basedir}/gen"></property>
<property name="res.abs" value="${basedir}/res"></property>
<property name="assets.abs" value="${basedir}/assets"></property>
<property name="dist.abs" value="${basedir}/dist"/>   

<property name="androidManifest.abs" value="${basedir}/AndroidManifest.xml"></property>
    <property name="dex.abs" value="${basedir}/bin/classes.dex"></property>
    <property name="resources.abs" value="${basedir}/bin/resources.ap_"></property>
    <property name="unsigned.abs" value="${dist.abs}/${ant.project.name}-unsigned.apk"></property>
    <property name="signed.abs" value="${dist.abs}/${ant.project.name}-signed.apk"></property>
    
  
<property name="keystore.path" location="${basedir}/bin/${keystore.name}"/>   


    <!-- 1.Cleaning the project -->
    <target name="1clean">
        <echo>Cleaning the project...</echo>
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${bin.abs}" includes="**/*">
        </fileset>
        </delete>
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${gen.abs}" includes="**/*">
        </fileset>
        </delete>
   
        <delete includeemptydirs="true" verbose="true">
            <fileset dir="${dist.abs}" includes="**/*">
        </fileset>
        </delete>
           
    </target>
    
    <!--2 Generate R.java -->
    <target name="2generate" depends="1clean">
        <echo>Generating R.java / Manifest.java from the resources...</echo>
        <exec executable="${aapt}" failonerror="true">
            <arg value="package"  /> 
        <arg value="-f" /> 
            <arg value="-m" /> 
            <arg value="-J" /> 
            <arg value="${gen.abs}" /> 
            <arg value="-M"  /> 
            <arg value="${androidManifest.abs}" /> 
            <arg value="-S" /> 
            <arg value="${res.abs}" /> 
            <arg value="-I" /> 
            <arg value="${android.core}" /> 
        </exec>
    </target>
    


    <!-- 3Convert and compile AIDL files. -->
    <target name="3aidl" depends="1clean">
        <echo>Compiling aidl files into Java classes...</echo>
        <apply executable="${aidl}" failonerror="true">
            <arg value="-p${android.framework}"/>
            <arg value="-I${src.abs}"/>
            <arg value="-o${gen.abs}"/>
            <fileset dir="${src.abs}">
                <include name="**/*.aidl">
            </include></fileset>
        </apply>
    </target>


    <!-- 4Compile 
     srcdir="${framework_src.abs}:${src.abs}:${gen.abs}" 表示编译的源文件目录有三个
    -->
    <target name="4compile" depends="1clean,2generate,3aidl">
        <javac encoding="UTF-8" target="1.6" debug="off" extdirs="" includeantruntime="false"  srcdir="${framework_src.abs}:${src.abs}:${gen.abs}" destdir="${bin.abs}"  bootclasspath="${android.core}">
            <classpath>
                <fileset dir="${lib.abs}" includes="*.jar">
            </fileset></classpath>
        </javac>
   
   
    </target>
    


<target name="obfuscate" depends="4compile">  


<java jar="${android.proguard}" fork="true" failonerror="true">
           <jvmarg value="-Dmaximum.inlined.code.length=16" />
           <arg value="@${basedir}/proguard.cfg" />
           <arg value="-injars ${bin.abs}" />
           <arg value="-outjars  ${dist.abs}/obfuscated.jar " />
           <arg value="-libraryjars ${android.core}" />
  <arg value="-libraryjars ${lib.abs}" />
</java>
 <unzip src="${dist.abs}/obfuscated.jar" dest="${bin.abs}"/>  
 <delete file="${dist.abs}/obfuscated.jar"/>  
 <echo>Obfuscated classes are put to "${bin.abs}".</echo>  
</target>  



    <!-- 5Convert and compress .class files to .dex file. -->
    <target name="5dex" depends="obfuscate">
        <echo>Converting compiled files and external libraries into dex format file...</echo>
        <apply executable="${dx}" failonerror="true" parallel="true">
            <arg value="--dex"/>
            <arg value="--output=${dex.abs}"/> 
            <arg path="${bin.abs}"/>
            <fileset dir="${lib.abs}" includes="*.jar">
        </fileset></apply>
    </target>



    <!-- 6Package the resources. -->
    <target name="6package">
        <echo>Packaging resources and assets...</echo>
        <exec executable="${aapt}" failonerror="true">
            <arg value="package"  /> 
            <arg value="-f" /> 
            <arg value="-M" /> 
            <arg value="${androidManifest.abs}" /> 
            <arg value="-S" /> 
            <arg value="${res.abs}" /> 
            <arg value="-A" /> 
            <arg value="${assets.abs}" /> 
            <arg value="-I" /> 
            <arg value="${android.core}" /> 
            <arg value="-F" /> 
            <arg value="${resources.abs}" /> 
        </exec>
    </target>
       
       
    <!-- 7Package an unsigned APK file. -->
    <target name="7genUnsignedApk" depends="5dex, 6package">
        <echo>Packaging an unsigned APK file...</echo>
        <exec executable="${apk-builder}" failonerror="true">
            <arg value="${unsigned.abs}"/>
            <arg value="-u"/>
            <arg value="-z"/>
            <arg value="${resources.abs}"/>
            <arg value="-f"/>
            <arg value="${dex.abs}"/>
            <arg value="-rf"/>
            <arg value="${src.abs}"/>
       
        <!--加jar库到应用包中 -->
           
            <arg value="-rj"/>
            <arg value="${lib.abs}"/>
       
        <!--加动态连接库到应用包中 -->
        <arg value="-nf"/>
        <arg value="${lib.abs}"/>
        </exec>
        <echo>It will need to be signed with jarsigner before being published.</echo>
    </target>
    

<!-- 8创建证书 -->  
<target name="8genericKey">  
 <echo message="genkey..."/>  
 <delete>  
  <fileset dir="${bin.abs}">  
      <include name="${keystore.name}"/>  
  </fileset>  
 </delete>  
 
 <genkey keystore="${keystore.path}" storepass="${keystore.password}" alias="${keystore.alias}"  
   verbose="true" validity="${keystore.validity}" dname="CN=Ant Group, OU=Jakarta Division, O=Apache.org, C=ZH">  
 </genkey>  
 <echo message="genkey... done"/>  
</target>


    <!-- 9Sign the APK. -->
    <target name="9genSignedApk" depends="7genUnsignedApk,8genericKey">
        <exec executable="${jarsigner}" failonerror="true">
            <arg value="-verbose"/>
            <arg value="-storepass"/>
            <arg value="${keystore.password}"/>
            <arg value="-keystore"/>
            <arg value="${keystore.path}"/>
            <arg value="-signedjar"/>
            <arg value="${signed.abs}"/>
            <arg value="${unsigned.abs}"/>
            <arg value="${keystore.alias}"/>
       </exec>
    </target>

    <!-- 10Zipalign -->
    <target name="10zipalign" depends="9genSignedApk">
        <tstamp>  
            <format property="time" pattern="yyyy-MM-dd-HH-mm-ss"></format>
        </tstamp>
        <exec executable="${zipalign}" failonerror="true">
            <arg value="-v"/>
            <arg value="-f"/>
            <arg value="4"/>
            <arg value="${signed.abs}"/>
            <arg value="${dist.abs}\${ant.project.name}-${time}.apk"/>
        </exec>
    </target>


<!-- 11 Uinstall the package from the default emulator -->  
<target name="11uninstall">  
       <echo>Uninstalling ${application.package} from the default emulator...</echo>  
       <exec executable="${adb}" failonerror="true">  
           <arg value="uninstall" />  
           <arg value="${application.package}" />  
       </exec>  
   </target>  



<!-- 12Install the package on the default emulator -->  
<target name="12install" depends="11uninstall,10zipalign">  
       <echo>Installing ${signed.abs} into default emulator...</echo>  
       <exec executable="${adb}" failonerror="true">  
           <arg value="install" />  
           <arg value="${signed.abs}" />  
       </exec>  
</target>  
 
 



</project>