Ant自动打包(可动态修改包名、资源等)生成不同签名不同包名的APP

上一篇我们介绍了Ant自动生成APK的例子:http://blog.csdn.net/up1up2up3/article/details/19558699

由于公司项目需要,需要实现Ant自动打包(可动态修改包名、资源等)生成不同签名不同包名的APP。

当然是先搜搜网上有木有相关的实现,发现复制来复制去都是同一个,但是不够详细,基本上就是说几句,然后贴了个build.xml(叫我们这些新手、很少搞脚本的人情何以堪 = =!),我主要参考了这个:http://www.zdyc.net/html/diary/newWrite/showlog_vm/sid=1/cat_id=-1/log_id=283,要了解的就是利用ant执行编写的脚本程序

脚本实现的主要步骤如下:

1、拷贝当前项目到临时目录

2、修改 AndroidManifest.xml包名以及java文件涉及到的包名(import、package等)


3、生成新的R文件(androidSDK/platform-tools\aapt.exe)

4、项目编译

5、生成dex文件(androidSDK\platform-tools\dx.bat)


6、打包资源文件(打包资源文件 androidSDK\platform-tools\aapt.exe)


7、APK打包(androidSDK\apkbuilder.bat)

PS:建议先行去了解下Ant脚本执行

先看下下面的build.xml(各个步骤都有进行说明):

<?xml version="1.0" encoding="UTF-8"?>
<project name="BQPUBLIC" default="ZWTH">



    <!-- 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>

    <!-- The project.properties file is created and updated by the ‘android‘
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using ‘android update project‘ or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read ‘custom‘ instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->
    <!-- version-tag: 1 -->
    <import file="${sdk.dir}/tools/ant/build.xml" />


<!--修改包名等-->
<!--相关环境及工具配置-->
 <property name="sdk.platformtools" value="${sdk.dir}/platform-tools" />
 <property name="sdk.tools" value="${sdk.dir}/tools" />
 <property name="aapt" value="${sdk.platformtools}/aapt.exe" />
  <property name="adb" value="${sdk.platformtools}/adb.exe" />
 <property name="dx" value="${sdk.platformtools}/dx.bat" />
 <property name="apkbuilder" value="${sdk.tools}/apkbuilder.bat" />
 <property name="android.jar" value="${sdk.dir}/platforms/android-10/android.jar" />



 <property name="old_package_name" value="com.zzx.xxxxxxic" />
 <property name="new_package_name" value="com.zwtx.xxxxxxic" />
 <property name="project_new">ZWTH</property>
 <property name="reasedAPK" value="${project_new}.apk" />
 <property name="org_project.dir" value="D:/test/xxPUBLIC" /><!--原工程目录-->


 <!--新工程各个目录-->
  <property name="build.dir" value="${org_project.dir}/build" />
 <property name="project.dir" value="${org_project.dir}/temp" /><!--新工程目录-->
 <property name="classes.dir" value="${project.dir}/bin/classes" />
 <property name="buildtemp.dir" value="${project.dir}/build" />
 <property name="src.dir" value="${project.dir}/src" />
 <property name="res.dir" value="${project.dir}/res" />
 <property name="gen.dir" value="${project.dir}/gen" />
 <property name="asset.dir" value="${project.dir}/asset" />

 <target name="init">
     <delete dir="${build.dir}"></delete>
     <mkdir dir="${build.dir}"/>
     <delete dir="${project.dir}"></delete>
     <mkdir dir="${project.dir}"/>

	  <copy todir="${project.dir}">
         <fileset dir="${org_project.dir}" includes="**/*"/>
     </copy>


     <!--替换工程中出现的包名 -->
  <replaceregexp flags="g" encoding="UTF-8" byline="true">
      <regexp pattern="package(.*)${old_package_name}"/>
         <substitution expression="package="${new_package_name}"/>
            <fileset dir="${project.dir}" includes="AndroidManifest.xml"/>
     </replaceregexp>

  <replaceregexp flags="g" encoding="UTF-8" byline="true">
      <regexp pattern="import(.*)${old_package_name}.R"/>
         <substitution expression="import ${new_package_name}.R"/>
            <fileset dir="${project.dir}/src" includes="**/*.java"/>
     </replaceregexp>
    </target>


  <!--生成R.java文件 ${sdk.dir}\platform-tools\aapt.exe-->
    <target name="genRJava">
        <exec executable="${aapt}" failonerror="true">
            <arg value="package"/>
            <arg value="-m"/>
            <arg value="-J"/>
            <arg value="${project.dir}/gen"/>
            <arg value="-M"/>
            <arg value="${project.dir}/AndroidManifest.xml"/>
            <arg value="-S"/>
            <arg value="${res.dir}"/>
            <arg value="-I"/>
            <arg value="${android.jar}"/>
        </exec>
    </target>
    <!-- 项目编译 -->
    <target name="compile" >
        <javac encoding="UTF-8" target="1.5" debug="true" extdirs="" 
         srcdir="${gen.dir};${src.dir}" 
         destdir="${classes.dir}" bootclasspath="${android.jar}">
            <classpath>
                <fileset dir="${project.dir}/libs" includes="*.jar">
                </fileset>
            </classpath>
        </javac>
    </target>
<!-- 生成dex文件 ${sdk.dir}\platform-tools\dx.bat -->
    <target name="dex" >
        <apply executable="${dx}" failonerror="true" parallel="true">
            <arg value="--dex"/>
            <arg value="--output=${buildtemp.dir}/classes.dex"/>
            <arg path="${classes.dir}"/>
            <fileset dir="${project.dir}/libs" includes="*.jar"/>
        </apply>
    </target>

 <!-- 打包资源文件 ${sdk.dir}\platform-tools\aapt.exe -->
    <target name="packageRes">
        <exec executable="${aapt}" failonerror="true">
            <arg value="package"/>
            <arg value="-f"/>
            <arg value="-M"/>
            <arg value="${project.dir}/AndroidManifest.xml"/>
            <arg value="-S"/>
            <arg value="${res.dir}"/>
            <arg value="-A"/>
            <arg value="${asset.dir}"/>
            <arg value="-I"/>
            <arg value="${android.jar}"/>
            <arg value="-F"/>
            <arg value="${buildtemp.dir}/resources.ap_"/>
        </exec>
    </target>

 <!-- 打包Apk -->
    <target name="ZWTH" depends="init,genRJava,compile,dex,packageRes">
        <!-- 打包 -->
        <exec executable="${apkbuilder}" failonerror="true">
            <arg value="${buildtemp.dir}/unsigntest.apk"/>
            <arg value="-u"/>
            <arg value="-z"/>
            <arg value="${buildtemp.dir}/resources.ap_"/>
            <arg value="-f"/>
            <arg value="${buildtemp.dir}/classes.dex"/>
            <arg value="-rf"/>
            <arg value="${src.dir}"/>
            <arg value="-rj"/>
            <arg value="${project.dir}/libs"/>
        </exec>
        <!-- 签名 -->
        <!--java -jar signapk.jar platform.x509.pem platform.pk8 TVJU.apk new.apk -->
       <!-- <exec executable="java" failonerror="true">
            <arg value="-jar"/>
            <arg value="${project.dir}/STBsign/signapk.jar"/>
            <arg value="${project.dir}/STBsign/platform.x509.pem"/>
            <arg value="${project.dir}/STBsign/platform.pk8"/>
            <arg value="${buildtemp.dir}/unsigntest.apk"/>
            <arg value="${buildtemp.dir}/TVJUSIGN.apk"/>
        </exec> -->
     <!-- -->
    <signjar
         jar="${buildtemp.dir}/unsigntest.apk"
         signedjar="${build.dir}/${reasedAPK}"
         keystore="${key.store}"
         storepass="${key.store.password}"
         alias="${key.alias}"
         keypass="${key.alias.password}"
         verbose="-verbose"/>
   </target>


</project>


接下来说下编译过程中的出现的一些问题:

1、Ant编译utf-8非法字符

    

   这个问题可以百度下,很多人出现,也有各式各样的解决方法,不过这只是广告,一般不会有什么问题,我这边是忽略掉了

2、value for ‘keystore‘ is not valid,这个百度下了,没有出现啊!!!坑爹了,搞了我很久

    
   后来发现这根本就是个sb的错误啊!!TM的,原来我在引用local.properties(或ant.properties)的时候,其中的keystore的path写错了

   key.store=D:/test/BPUBLIC/android.keystore写成key.store=D\:/test/BPUBLIC/android.keystore(刚开始转义了下,忘记去掉了)

  

3、编译的时候我执行 ant release发现总是生成原项目的apk,根本就没有修改完后的新包名的apk,甚至临时目录都木有啊!!!

    后来查询了这篇文章《Ant Buildfile中project标签的default属性作用》才发现<project name="BPUBLIC" default="ZWTH">中default以及子标签<target>标签的含义我没注意到,<target>可以由命令行进行显示的调用,也可以在内部使用如可以直接调用ant init、ant compile等。如果不写参数,则默认的build文件是build.xml,默认的目标是<project>的default属性定义的目标。

  我们执行的时候可以一步步执行,也可以一次性执行目标,如:ant ZWTH

  根据上面的ant文件及步骤,可以一步步编译生成:

 

 

其余的就不再一一列举了。

一次性编译是用如下命令(根据<target name="ZWTH" depends="init,genRJava,compile,dex,packageRes">的依赖关系):

 

build success!!!

Ant自动打包(可动态修改包名、资源等)生成不同签名不同包名的APP,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。