Android ant修改包名打包 (轉)

yangxi_001發表於2014-03-19

http://blog.zdyc.net/html/diary/newWrite/showlog_vm/sid=1/cat_id=-1/log_id=283

<?xml version="1.0" encoding="UTF-8"?>
<!--
1、拷貝當前專案到臨時目錄
2、修改 AndroidManifest_ZYY.xml包名
3、生成新的R檔案
4、編譯
5、生成dex檔案
6、打包資原始檔
7、APK打包
-->
<project name="build" default="packageAPK" basedir=".">
 
    <property name="sdk.dir" location="D:/DeveloperPlatform/android-sdk-windows"/>
 <property name="project_org.dir" value="D:/androidWork/TVJU_ZYY" />
 <property name="reasedAPK" value="TVJU.APK" />
 <property name="old_package_name" value="com.babao.tvju" />
 <property name="new_package_name" value="com.jrm.zyy2" />
 
 <property name="keystore" value="${project_org.dir}/keystore/mstar" />
 <property name="storepass" value="111111" />
 <property name="storealias" value="mstar" />
 <property name="storekeypass" value="111111" />
  
 
 <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="build.dir" value="${project_org.dir}/build" />
 <property name="project.dir" value="${project_org.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" />
 
 
    <!-- 設定路徑和初始化環境 -->
    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar"/>
        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar"/>
        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar"/>
    </path>
 <!--android 環境的建立-->
   <taskdef name="setup" classname="com.android.ant.SetupTask" classpathref="android.antlibs"/>
   
 <!-- 初始化 -->
    <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="${project_org.dir}" includes="**/*"/>
     </copy>
     
  <replaceregexp flags="g" encoding="UTF-8" byline="true">
      <regexp pattern="package(.*)${old_package_name}"/>
         <substitution expression="package=&quot;${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="packageAPK" 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="${keystore}"
         storepass="${storepass}"
         alias="${storealias}"
         keypass="${storekeypass}"
         verbose="-verbose"/>
   </target>

</project>


相關文章