Android ant脚本自动打包、自动替换包名

         最近公司有个项目:要求是这样的,客户用一个网址,一键生成App。于是便用到了Ant 脚本语言。网上关于命令行通过ant脚本自动更换包名的大多不能用,不太靠谱。今天没事整理了一下,保证能用。好了废话不多说。

一、首先要配置我们的ant 运行环境,我用的是windows系统。我用的是apache-ant-1.9.6-bin,解压后在环境变量Path中加入ant的运行路径:如:D:\adt-bundle-windows-x86-20140702\apache-ant-1.9.6-bin\apache-ant-1.9.6\bin(最好加入完整的路径,因为有的时候用相对路径会出错)。

打开cmd,输入ant,如果出现这样,说明安装成功!

二、既然是一键生成apk,当然先准备一个android项目了,很简单,就是用webview打开一个网址,当然这个网址是用户提供给我们的。(前端设计个网址,用户上传网址,我们请求服务器获得这个网址!)

三、将Android的项目放在D盘的目录下面,项目要先clean下。

在项目的根目录下新建  build.xml、local.properties、parameter.properties

如下:

build.xml 如下:

    <?xml version="1.0" encoding="GBK"?>
<project name="BQPUBLIC" default="packaging">
  <property file="local.properties" />
    <property file="ant.properties" />
  <property file="parameter.properties" />
 
  <property environment="env" />
  <condition property="sdk.dir" value="${env.ANDROID_HOME}">
    <isset property="env.ANDROID_HOME" />
  </condition>


  <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 file="custom_rules.xml" optional="true" />
  <import file="${sdk.dir}/tools/ant/build.xml" />
<!--修改包名等-->
<!--相关环境及工具配置-->
 <property name="sdk.platformtools" value="${sdk.dir}/platform-tools" />
 <property name="sdk.buildtools" value="${sdk.dir}/build-tools/android-4.4W" />
 <property name="sdk.tools" value="${sdk.dir}/tools" />
 <property name="aapt" value="${sdk.buildtools}/aapt.exe" />
 <property name="adb" value="${sdk.platformtools}/adb.exe" />
 <property name="dx" value="${sdk.buildtools}/dx.bat" />
 <property name="apkbuilder" value="${sdk.tools}/android.bat" />
 <property name="android.jar" value="${sdk.dir}/platforms/android-20/android.jar" />
 <property name="old_package_name" value="com.o2osystem.lifepass" />
 <property name="new_package_name" value="${new.projectallname}" />
 <property name="project_new">packaging</property>
 <property name="reasedAPK" value="${project_new}.apk" />
 <property name="org_project.dir" value="${old.project.dir}" /><!--原工程目录-->


 <!--新工程各个目录-->
 <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="GBK" 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="GBK" byline="true">
    <regexp pattern="portrait"/>
     <substitution expression="${sreenstyle}"/>
      <fileset dir="${project.dir}" includes="AndroidManifest.xml"/>
   </replaceregexp>

<replaceregexp flags="g" encoding="GBK" byline="true">
    <regexp pattern="@android:style/Theme.NoTitleBar"/>
     <substitution expression="${theme}"/>
      <fileset dir="${project.dir}" includes="AndroidManifest.xml"/>
   </replaceregexp>

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


<move file="${project.dir}/src/com/o2osystem/lifepass" tofile="${project.dir}/src/com/o2osystem/${new.project.thirdname}"/>
 
 
  </target>

 
  
  <!--生成R.java文件 ${sdk.dir}\platform-tools\aapt.exe-->
  <target name="genRJava">
    <exec executable="${aapt}" failοnerrοr="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="GBK" 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}" failοnerrοr="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}" failοnerrοr="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="packaging" depends="init,genRJava,compile,dex,packageRes">
    <!-- 打包 -->
    <exec executable="${apkbuilder}" failοnerrοr="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" failοnerrοr="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="bahl.keystore"
     storepass="789632145"
     alias="@bahl"
     keypass="789632145"
     verbose="-verbose"/>
     -->
   </target>

</project>

local.properties 如下:sdk.dir是sdk目录

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=D:\\adt-bundle-windows-x86-20140702\\adt-bundle-windows-x86-20140702\\sdk

  parameter.properties 如下:

# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
new.project.thirdname=porject2
new.projectallname=com.o2osystem.porject2
old.project.dir=D:/Packaging/test2
sreenstyle=portrait
theme=@android:style/Theme.NoTitleBar

注:

 new.projectallname:新工程的名字(这里用的是com.o2osystem.porject2)

new.project.thirdname新工程名字的一部分(这里是porject2

sreenstyleportrait竖屏 landscape 横屏

theme=@android:style/Theme.NoTitleBar 显示标题栏   theme=@android:style/Theme.NoTitleBar.Fullscreen 满屏

四、打开cmd命令,定位到根目录:输入 ant packaging

五、执行四之后,会在根目录下生成一个新的文件夹temp,定位到temp:cd  temp

六、将temp中的 build.xml 的内容全部替换成下面:(很重要)

 <?xml version="1.0" encoding="UTF-8"?>
<project name="LauncherActivity" default="help">


<property name="aapt.ignore.assets" value="!.svn:!.git:\x3Cdir\x3E_*:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~:crunch" />


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

</project>

七、关闭cmd(一定要做这一步)

八、重新打开cmd,定位到temp文件夹,输入ant debug

执行完以后,你会发现temp/bin生成新的apk,这个apk是包名已经成功被替换了。以上的步凑都可以用命令行操作。写了这么多,如果对你有帮助的话,请点个赞!

有问题可以发私信,我们互相讨论!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值