ant自动编译打包android项目

源代码及可执行文件下载地址:https://files.cnblogs.com/rainboy2010/antdemo.zip

 

Android打包APK的流程如下:

 

 

 下面我们开始尝试使用ant进行apk的打包

 

准备工作

1.因为apkbuilder.bat在新版本的Android sdk中已经被移除,所以我们需要手动创建apkbuilder.bat文件,然后将此文件放在sdk tools目录下,apkbuilder.bat文件内容如下:

@echo off
rem Copyright (C) 2007 The Android Open Source Project
rem
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with the License.
rem You may obtain a copy of the License at
rem
rem      http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem don't modify the caller's environment
setlocal

rem Set up prog to be the path of this script, including following symlinks,
rem and set up progdir to be the fully-qualified pathname of its directory.
set prog=%~f0

rem Change current directory and drive to where the script is, to avoid
rem issues with directories containing whitespaces.
cd /d %~dp0

rem Check we have a valid Java.exe in the path.
set java_exe=
call lib\find_java.bat
if not defined java_exe goto :EOF

set jarfile=sdklib.jar
set frameworkdir=

if exist %frameworkdir%%jarfile% goto JarFileOk
    set frameworkdir=lib\

if exist %frameworkdir%%jarfile% goto JarFileOk
    set frameworkdir=..\framework\

:JarFileOk

set jarpath=%frameworkdir%%jarfile%

call "%java_exe%" -classpath %jarpath% com.android.sdklib.build.ApkBuilderMain %*

 2.创建一个签名文件如jack.keystore,然后创建ant.properties文件,将签名信息写到ant.properties文件中

#签名文件的路径
keystore=jack.keystore
#存储库口令
storepass=123456
#密匙库口令
keypass=123456
#别名
alias=jack

3.下载ant压缩包  http://ant.apache.org,下载之后将其解压到某个目录(本人解压到C:\apache-ant-1.9.5) ,然后配置环境变量(新建ANT_HOME环境变量,值为ant所在的目录,然后将ANT_HOME\bin添加到path环境变量中),如图:    


APK打包

 1.创建一个Android项目,名称为hello

2.执行命令android update project --name hello --path .,生成local.properties和build.xml文件,在执行此命令时,请确保Android SDK目录下的tools路径已经添加到Path环境变量中

 

3.修改build.xml文件,将 </condition>之后的语句全部删除,同时添加default="zipalign" basedir="."信息,修改后的build.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="hello" default="zipalign" basedir=".">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Sytems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>
    
    
</project>

 

 4.在build.xml文件中添加一些变量信息,此处我使用的Android sdk build tools版本为23.0.2

    <tstamp>
       <format property="now" pattern="yyyyMMdd_HHmmss" locale="en"/>
    </tstamp>
    
    <property name="sdk-tools" value="${sdk.dir}/tools" />
    <property name="sdk-build-tools" value="${sdk.dir}/build-tools/23.0.2" />
    <property name="sdk-platforms" value="${sdk.dir}/platforms/android-23" />
    <property name="android-jar" value="${sdk-platforms}/android.jar" />  
    <property name="framework-aidl" value="${sdk-platforms}/framework.aidl" />
    
    
    <!--编译工具-->
   <property name="aapt" value="${sdk-build-tools}/aapt.exe" />  
   <property name="aidl" value="${sdk-build-tools}/aidl.exe" />  
   <property name="dx" value="${sdk-build-tools}/dx.bat" />  
   <property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />  
   <property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner.exe" />
   <property name="zipalign" value="${sdk-build-tools}/zipalign.exe" /> 
    
     <!--输入目录-->
    <property name="assets-dir" value="${basedir}/assets" />  
    <property name="res-dir" value="${basedir}/res" />  
    <property name="src-dir" value="${basedir}/src" />  
    <property name="libs-dir" value="${basedir}/libs" />  
    
    <!--输出目录-->
    <property name="outdir-bin" value="${basedir}/bin" />  
    <property name="outdir-gen" value="${basedir}/gen" />  
    <property name="outdir-classes" value="${outdir-bin}" />
    <property name="dex-file" value="classes.dex" />
    <property name="dex-path" value="${outdir-bin}/${dex-file}" />
    <property name="resources-package" value="${outdir-bin}/resources.ap_" />
    <property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />
    <property name="out-signed-package" value="${outdir-bin}/${ant.project.name}-singed.apk" />
    <property name="out-zipalign-package" value="${outdir-bin}/${ant.project.name}-zipalign_${now}.apk" />

 

5.初始化输出目录,在build.xml中添加以下信息:

     <!-- 初始化 -->  
    <target name="init">  
       <echo>init all output directories...</echo>
       <delete dir="${outdir-bin}" />  
       <delete dir="${outdir-gen}" />  
       <mkdir dir="${outdir-bin}" />  
       <mkdir dir="${outdir-gen}" />  
    </target>  

 

此时执行ant init命令,就会在程序根目录下生成bin和gen文件夹

6.生成R.java类文件

在build.xml中添加以下信息:

    <!--生成R.java类文件-->
    <target name="gen-R" depends="init">
       <echo>Generating R.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="${res-dir}" />
           <arg value="-A" />
           <arg value="${assets-dir}" />
           <arg value="-I" />
           <arg value="${android-jar}" />
      </exec>
    </target>

 

执行命令ant gen-R命令,就会生成R.java文件

7.编译aidl文件

在build.xml文件中添加如下信息:

    <!-- 编译aidl文件 -->
    <target name="aidl" depends="gen-R">
        <echo>Compiling .aidl into java files...</echo>
        <apply executable="${aidl}" failonerror="true">
            <arg value="-p${framework-aidl}" />
            <arg value="-I${src-dir}" />
            <arg value="-o${outdir-gen}" />
            <fileset dir="${src-dir}">
                <include name="**/*.aidl" />
            </fileset>
        </apply>
    </target>

 

 执行命令ant aidl命令,就会编译相关的aidl文件

 

8.编译源文件生成对应的class文件

在build.xml文件中添加如下信息:

     <!--编译源文件生成对应的class文件 -->
    <target name="compile" depends="aidl">
         <echo>Compiling java source code...</echo>
         <javac encoding="utf-8" target="1.6" source="1.6" destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="false" >
         <compilerarg value="-nowarn"/>    
         <src path="${src-dir}" />
         <src path="${outdir-gen}" />
         <classpath>
              <fileset dir="${libs-dir}" includes="*.jar" />
         </classpath>
     </javac>
   </target>

 

执行命令ant compile命令,就会编译生成.class文件

9.将.class文件转化成.dex文件

在build.xml文件中添加如下信息:

    <!--将.class文件转化成.dex文件 -->
   <target name="dex" depends="compile">
      <echo>Converting compiled files and external libraries into a .dex file...</echo>
      <exec executable="${dx}" failonerror="true">
        <arg value="--dex"  />
        <arg value="--output=${dex-path}" />
        <arg value="${outdir-classes}" />
        <arg value="${libs-dir}" />
      </exec>
   </target>

 

执行命令ant dex命令,就会编译生成classes.dex文件

10.打包资源文件,生成resources.ap_

在build.xml文件中添加如下信息:

   <!--打包资源文件(包括res、assets、AndroidManifest.xml)-->
  <target name="package-res-and-assets" depends="dex"> 
       <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="${res-dir}" />
               
            <arg value="-A" />
            <arg value="${assets-dir}" />
               
            <arg value="-I" />  
           <arg value="${android-jar}" />  
           
           <arg value="-F" />
           <arg value="${resources-package}" />
     </exec>
     
  </target> 

 

执行命令ant package-res-and-assets命令,就会生成resources.ap_文件

11.生成未签名的apk包

在build.xml文件中添加如下信息:

 

 

 <!--根据classes.dex文件和resources.ap_生成未签名的apk包 -->
  <target name="package" depends="package-res-and-assets" >
        <echo>Packaging ${out-unsigned-package} for release...</echo>
        <exec executable="${apkbuilder}" failonerror="true">

            <arg value="${out-unsigned-package}" />
              <arg value="-u" />
              
              <arg value="-z" />
              <arg value="${resources-package}" />
              
              <arg value="-f" />
              <arg value="${dex-path}" />
        
              <arg value="-rf" />
              <arg value="${src-dir}" />
             
              
              <arg value="-rj"/>
          <arg value="${libs-dir}" />
                
          <arg value="-nf"/>
          <arg value="${libs-dir}" />
        </exec>    
  </target> 

 

 

 

执行命令ant package命令,就会生成未签名的apk包hello-unsigned.apk

 

12.对apk包进行签名

在build.xml文件中添加如下信息:

 

<!--对上面生成的apk包进行签名-->
  <target name="jarsigner" depends="package">
       <echo>sign for apk...</echo> 
       <exec executable="${jarsigner}" failonerror="true">
           
           <arg value="-verbose" /> 
        
        <arg value="-digestalg"/>  
        <arg value="SHA1"/>  
        
        <arg value="-sigalg"/>  
        <arg value="SHA1withRSA"/>  
               
           <arg value="-keystore" />  
        <arg value="${keystore}" />  
        

        <arg value="-storepass" />  
        <arg value="${storepass}" />  
        
        <arg value="-keypass" />  
        <arg value="${keypass}" />  
        
        <arg value="-signedjar" />  
        <arg value="${out-signed-package}" />  
       
        <arg value="${out-unsigned-package}" />  
        <arg value="${alias}" />  
        
     </exec>    
      
  </target>       

 

 

 

执行命令ant jarsigner命令,就会生成签名的apk包hello-signed.apk

 

13.对签名的apk包进行字节对齐

在build.xml文件中添加如下信息:

 

 <!--对签名的apk包进行字节对齐-->
  <target name="zipalign" depends="jarsigner">
       <echo>zipalign for signed apk:${out-signed-package}</echo> 
       <exec executable="${zipalign}" failonerror="true">
             <arg value="-f" />
             <arg value="-v" />
             <arg value="4" />
             <arg value="${out-signed-package}"  />
             <arg value="${out-zipalign-package}" />
     </exec>    
     <echo>++++++++++++++++finished,the last output file is:${out-zipalign-package}+++++++++++++</echo>
  </target> 

 

 

 

执行命令ant zipalign命令,就会生成签名并且字节对齐的apk包

 

最终的build.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="hello" default="zipalign" basedir=".">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>
    
     <tstamp>
       <format property="now" pattern="yyyyMMdd_HHmmss" locale="en"/>
    </tstamp>
    
    <property name="sdk-tools" value="${sdk.dir}/tools" />
    <property name="sdk-build-tools" value="${sdk.dir}/build-tools/23.0.2" />
    <property name="sdk-platforms" value="${sdk.dir}/platforms/android-23" />
    <property name="android-jar" value="${sdk-platforms}/android.jar" />  
    <property name="framework-aidl" value="${sdk-platforms}/framework.aidl" />
    
    
    <!--编译工具-->
   <property name="aapt" value="${sdk-build-tools}/aapt.exe" />  
   <property name="aidl" value="${sdk-build-tools}/aidl.exe" />  
   <property name="dx" value="${sdk-build-tools}/dx.bat" />  
   <property name="apkbuilder" value="${sdk-tools}/apkbuilder.bat" />  
   <property name="jarsigner" value="${env.JAVA_HOME}/bin/jarsigner.exe" />
   <property name="zipalign" value="${sdk-build-tools}/zipalign.exe" /> 
    
     <!--输入目录-->
    <property name="assets-dir" value="${basedir}/assets" />  
    <property name="res-dir" value="${basedir}/res" />  
    <property name="src-dir" value="${basedir}/src" />  
    <property name="libs-dir" value="${basedir}/libs" />  
    
    <!--输出目录-->
    <property name="outdir-bin" value="${basedir}/bin" />  
    <property name="outdir-gen" value="${basedir}/gen" />  
    <property name="outdir-classes" value="${outdir-bin}" />
    <property name="dex-file" value="classes.dex" />
    <property name="dex-path" value="${outdir-bin}/${dex-file}" />
    <property name="resources-package" value="${outdir-bin}/resources.ap_" />
    <property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />
    <property name="out-signed-package" value="${outdir-bin}/${ant.project.name}-singed.apk" />
    <property name="out-zipalign-package" value="${outdir-bin}/${ant.project.name}-zipalign_${now}.apk" />

 
    <!-- 初始化 -->  
    <target name="init">  
       <echo>init all output directories...</echo>
       <delete dir="${outdir-bin}" />  
       <delete dir="${outdir-gen}" />  
       <mkdir dir="${outdir-bin}" />  
       <mkdir dir="${outdir-gen}" />  
    </target>  
    
    <!--生成R.java类文件-->
    <target name="gen-R" depends="init">
       <echo>Generating R.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="${res-dir}" />
           <arg value="-A" />
           <arg value="${assets-dir}" />
           <arg value="-I" />
           <arg value="${android-jar}" />
      </exec>
    </target>

    <!-- 编译aidl文件 -->
    <target name="aidl" depends="gen-R">
        <echo>Compiling .aidl into java files...</echo>
        <apply executable="${aidl}" failonerror="true">
            <arg value="-p${framework-aidl}" />
            <arg value="-I${src-dir}" />
            <arg value="-o${outdir-gen}" />
            <fileset dir="${src-dir}">
                <include name="**/*.aidl" />
            </fileset>
        </apply>
    </target>
    
    <!--编译源文件生成对应的class文件 -->
    <target name="compile" depends="aidl">
         <echo>Compiling java source code...</echo>
         <javac encoding="utf-8" target="1.6" source="1.6" destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="false" >
         <compilerarg value="-nowarn"/>    
         <src path="${src-dir}" />
         <src path="${outdir-gen}" />
         <classpath>
              <fileset dir="${libs-dir}" includes="*.jar" />
         </classpath>
     </javac>
   </target>
   
    <!--将.class文件转化成.dex文件 -->
   <target name="dex" depends="compile">
      <echo>Converting compiled files and external libraries into a .dex file...</echo>
      <exec executable="${dx}" failonerror="true">
        <arg value="--dex"  />
        <arg value="--output=${dex-path}" />
        <arg value="${outdir-classes}" />
        <arg value="${libs-dir}" />
      </exec>
   </target>
   
    <!--打包资源文件(包括res、assets、AndroidManifest.xml)-->
  <target name="package-res-and-assets" depends="dex"> 
       <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="${res-dir}" />
               
            <arg value="-A" />
            <arg value="${assets-dir}" />
               
            <arg value="-I" />  
           <arg value="${android-jar}" />  
           
           <arg value="-F" />
           <arg value="${resources-package}" />
     </exec>
     
  </target> 
  
   <!--根据classes.dex文件和resources.ap_生成未签名的apk包 -->
  <target name="package" depends="package-res-and-assets" >
        <echo>Packaging ${out-unsigned-package} for release...</echo>
        <exec executable="${apkbuilder}" failonerror="true">

            <arg value="${out-unsigned-package}" />
              <arg value="-u" />
              
              <arg value="-z" />
              <arg value="${resources-package}" />
              
              <arg value="-f" />
              <arg value="${dex-path}" />
        
              <arg value="-rf" />
              <arg value="${src-dir}" />
             
              
              <arg value="-rj"/>
          <arg value="${libs-dir}" />
                
          <arg value="-nf"/>
          <arg value="${libs-dir}" />
        </exec>    
  </target> 

  <!--对上面生成的apk包进行签名-->
  <target name="jarsigner" depends="package">
       <echo>sign for apk...</echo> 
       <exec executable="${jarsigner}" failonerror="true">
           
           <arg value="-verbose" /> 
        
        <arg value="-digestalg"/>  
        <arg value="SHA1"/>  
        
        <arg value="-sigalg"/>  
        <arg value="SHA1withRSA"/>  
               
           <arg value="-keystore" />  
        <arg value="${keystore}" />  
        

        <arg value="-storepass" />  
        <arg value="${storepass}" />  
        
        <arg value="-keypass" />  
        <arg value="${keypass}" />  
        
        <arg value="-signedjar" />  
        <arg value="${out-signed-package}" />  
       
        <arg value="${out-unsigned-package}" />  
        <arg value="${alias}" />  
        
     </exec>    
      
  </target>       
  
   <!--对签名的apk包进行字节对齐-->
  <target name="zipalign" depends="jarsigner">
       <echo>zipalign for signed apk:${out-signed-package}</echo> 
       <exec executable="${zipalign}" failonerror="true">
             <arg value="-f" />
             <arg value="-v" />
             <arg value="4" />
             <arg value="${out-signed-package}"  />
             <arg value="${out-zipalign-package}" />
     </exec>    
     <echo>++++++++++++++++finished,the last output file is:${out-zipalign-package}+++++++++++++</echo>
  </target> 
  
</project>

 

posted @ 2016-04-21 21:37  南极冰川雪  阅读(737)  评论(0编辑  收藏  举报