[转]使用ant让Android自动打包的build.xml,自动生成签名的apk文件(支持android4.0以上的版本)

在android4.0以后的sdk里那个脚本就失效了,主要是因为 apkbuilder这个程序不见了;

人家sdk升级,我们的脚本也要跟上趟,修改一下喽。

上网一查,大家的文章还停留在我去年的脚本程度,算了,自己动手查阅了资料之后,具体实现如下:

在工程的根目录 创建2个文件,分别:

1、build.xml

2、build.properties

build.xml的内容:

[java] view plaincopyprint?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project name="autoDeployAPK" default="deploy">
  3. <!-- 使用第三方的ant包,使ant支持for循环-->
  4. <taskdef resource="net/sf/antcontrib/antcontrib.properties">
  5. <classpath>
  6. <pathelement location="lib/ant-contrib-1.0b3.jar"/>
  7. </classpath>
  8. </taskdef>
  9. <property file="build.properties" />
  10. <!-- The local.properties file is created and updated by the ‘android‘ tool.
  11. It contains the path to the SDK. It should *NOT* be checked into
  12. Version Control Systems.
  13. <property file="local.properties" />
  14. -->
  15. <!-- The ant.properties file can be created by you. It is only edited by the
  16. ‘android‘ tool to add properties to it.
  17. This is the place to change some Ant specific build properties.
  18. Here are some properties you may want to change/update:
  19. source.dir
  20. The name of the source directory. Default is ‘src‘.
  21. out.dir
  22. The name of the output directory. Default is ‘bin‘.
  23. For other overridable properties, look at the beginning of the rules
  24. files in the SDK, at tools/ant/build.xml
  25. Properties related to the SDK location or the project target should
  26. be updated using the ‘android‘ tool with the ‘update‘ action.
  27. This file is an integral part of the build system for your
  28. application and should be checked into Version Control Systems.
  29. <property file="ant.properties" />
  30. -->
  31. <property name="jar.libs.dir" value="${jar.libs.dir}" />
  32. <!-- The project.properties file is created and updated by the ‘android‘
  33. tool, as well as ADT.
  34. This contains project specific properties such as project target, and library
  35. dependencies. Lower level build properties are stored in ant.properties
  36. (or in .classpath for Eclipse projects).
  37. This file is an integral part of the build system for your
  38. application and should be checked into Version Control Systems. -->
  39. <loadproperties srcFile="${project.dir}/project.properties" />
  40. <!-- quick check on sdk.dir -->
  41. <fail message="sdk.dir is missing. Make sure to generate local.properties using ‘android update project‘ or to inject it through an env var"
  42. unless="sdk.dir"/>
  43. <property name="channelname" value="pateo" />
  44. <property name="channelkey" value="12347" />
  45. <!--循环打包 -->
  46. <target name="deploy">
  47. <foreach target="modify_manifest" list="${channel.list}" param="nameandchannel" delimiter=","></foreach>
  48. </target>
  49. <target name="modify_manifest">
  50. <!-- 获取渠道名字 -->
  51. <propertyregex override="true" property="channelname" input="${nameandchannel}" regexp="(.*):" select="\1"/>
  52. <!-- 获取渠道号码 -->
  53. <propertyregex override="true" property="channelkey" input="${nameandchannel}" regexp=":(.*)" select="\1"/>
  54. <!-- 正则匹配替换渠道号
  55. <replaceregexp flags="g" byline="false" encoding="UTF-8">
  56. <regexp pattern=‘meta-data android:name="CHANNEL" android:value="(.*)"‘ />
  57. <substitution expression=‘meta-data android:name="CHANNEL" android:value="${channelkey}"‘ />
  58. <fileset dir="" includes="AndroidManifest.xml" />
  59. </replaceregexp>-->
  60. <property name="out.final.file"
  61. location="${apk.out.dir}/${project.name}_${channelname}_${project.version}.apk" />
  62. <antcall target="release" />
  63. </target>
  64. <!-- extension targets. Uncomment the ones where you want to do custom work
  65. in between standard targets -->
  66. <!--
  67. <target name="-pre-build">
  68. </target>
  69. <target name="-pre-compile">
  70. </target>
  71. /* This is typically used for code obfuscation.
  72. Compiled code location: ${out.classes.absolute.dir}
  73. If this is not done in place, override ${out.dex.input.absolute.dir} */
  74. <target name="-post-compile">
  75. </target>
  76. -->
  77. <!--如果项目包含了jni代码,希望在打包时自动重新编译so库,可以修改build.xml文件。
  78. 修改方法为,在引用sdk的build.xml文件之前添加如下target:-->
  79. <!--
  80. <target name="-pre-build" depends="-ndk-build">
  81. </target>
  82. <target name="-ndk-build">
  83. <exec executable="ndk-build" failonerror="true">
  84. <arg value="clean" />
  85. </exec>
  86. <exec executable="ndk-build" failonerror="true" />
  87. </target>
  88. -->
  89. <!-- Import the actual build file.
  90. To customize existing targets, there are two options:
  91. - Customize only one target:
  92. - copy/paste the target into this file, *before* the
  93. <import> task.
  94. - customize it to your needs.
  95. - Customize the whole content of build.xml
  96. - copy/paste the content of the rules files (minus the top node)
  97. into this file, replacing the <import> task.
  98. - customize to your needs.
  99. ***********************
  100. ****** IMPORTANT ******
  101. ***********************
  102. In all cases you must update the value of version-tag below to read ‘custom‘ instead of an integer,
  103. in order to avoid having your file be overridden by tools such as "android update project"
  104. -->
  105. <!-- version-tag: 导入anroid sdk 默认的ant写好的build.xml -->
  106. <import file="${sdk.dir}/tools/ant/build.xml" />
  107. </project>

这个文件主要就是打包的指令,其中大家不难看出

[java] view plaincopyprint?

  1. <import file="${sdk.dir}/tools/ant/build.xml" />

自动引用并且执行了你的sdk目录下tools/ant/build.xml,大家有精力可以自己看看这个文件.

build.properties的内容:

[java] view plaincopyprint?

  1. #Save
  2. #Wed Oct 23 15:47:27 CST 2013
  3. project.name=MOBILE_ANDROID_PROJECT
  4. jar.libs.dir=libs
  5. build.first=false
  6. key.alias=maomao
  7. key.alias.password=maomao
  8. channel.list=test1\:100411-f91d7ad6c99688b587b299d2c507679d,test2\:100411-f91d7ad6c99688b587b299d2c507679d
  9. java.dir=C\:\\Program Files (x86)\\Java\\jdk1.6.0_10
  10. key.store=C\:\\changeself\\test.keystore
  11. project.dir=C\:\\changeself\\project_android
  12. sdk.dir=C\:\\changeself\\adt-bundle-windows-x86-20130729\\sdk
  13. project.version=1.0.3
  14. key.store.password=maomao
  15. apk.out.dir=apk

这个文件里面,

project.name 就是你的工程名,其实最后体现在APK的名字

key.alias和key.alias.password是你的 签名文件里的东西,自己先生成去

channel.list就是你的渠道名字,支持多个渠道,test1和test2,......testn,自己按照格式去填写,你写了几个渠道就会生成几个apk

java.dir    自己的java本机的安装目录

key.store  自己签名文件的本机存放目录

project.dir 你的工程本机存放目录

sdk.dir  你的android sdk本机存放目录

project.version  工程的版本号,最后也会体现在apk的名字里

key.store.password

apk.out.dir     在你的工程根目录存放apk生成的目录

ok,配置好上述2个文件后,在cmd命令行下,键入你的工程目录,执行 ant命令

最后会在你的apk.out.dir生成你需要的apk文件

转自:http://weibo.com/changeself

时间: 2024-08-13 18:11:00

[转]使用ant让Android自动打包的build.xml,自动生成签名的apk文件(支持android4.0以上的版本)的相关文章

Android 生成签名及APK 文件

Overview Android apk 我相信每个Android开发者都知道他是干啥的: APK(全称:Android application package,Android应用程序包)是Android操作系统使用的一种应用程序包文件格式,用于分发和安装移动应用及中间件. 了解了Android APK 的概念接着就让我们看一下如何去生成我们的APK文件以及我们的文件签名吧. 环境配置 Windows系统 Android Studio 3.5.3 创建签名文件 如果你之前有创建好签名文件,那么你

Android 生成正式签名的APK文件

应用已经开发出来了,下一步我们需要思考推广方面的工作.那么如何才能让更多的用户知道并使用我们的应用程序呢?在手机----领域,最常见的做法就是将程序发布到某个应用商店中,这样用户就可以通过商店找到我们的应用程序,然后轻松地进行下载和安装. 说到应用商店,在Android领域真的可以称得上是百家争鸣,除了谷歌官方推出的Google Play之外,在国内还有91.豌豆荚.机锋.360等知名应用商店.当然,这些商店所提供的功能都是比较类似的,发布应用的方法也大同小异,因此这里我们就只学习如何将应用发布

Android项目实战(三十一):异步下载apk文件并安装(非静默安装)

原文:Android项目实战(三十一):异步下载apk文件并安装(非静默安装) 前言: 实现异步下载apk文件 并 安装.(进度条对话框显示下载进度的展现方式) 涉及技术点: 1.ProgressDialog   进度条对话框  用于显示下载进度 2.AsyncTask         异步任务的使用    耗时操作不能再主线程中进行      安卓开发_浅谈AsyncTask 3.File                   文件相关操作    将文件的字节数据生成文件 4.自动打开安装应用操

Android Studio 生成签名的APK

打开项目以后,点击项目,选择 Build 菜单, 然后选择 Generate Signed APK. 如下图所示: 打开生成对话框: 选择 Create new... 按钮, 生成新的Key, 弹出新生成对话框: 首先选择 Key 存储的路径. 可能是有BUG, 在弹出的选择 keystore file 对话框中,需要先输入 File name, 如 ``, 文件后缀名 保持默认的 jks 不变.然后再改变保存的路径(否则输入不了名字). 最后的路径可能是这样的: E:\CODE_ALL\02_

Android之apk文件签名——keytool 和 jarsigner

一.生成密钥库将位置定位在jdk的bin文件中,输入以下命名行: keytool -genkey -alias ChangeBackgroundWid get.keystore -keyalg RSA -validity 20000 -keystore ChangeBackgroundWidget.keystore 上面的命令中间不换行,按下"Enter"键,并根据提示填写相关信息,详细信息如下及图1(validity为有效期,这里有效期为20000天):输入keystore密码:再次

ant工具-多渠道自动打包android项目

(一)ant介绍 ant是自动化拷贝.编译.发布的构建工具,简单跨平台. (二)ant使用前奏 1.安装jdk并配合环境变量 2.安装sdk并配合环境变量 3.新版的android sdk已经自带了ant在/eclipse/plugins目录下,如需下载到http://ant.apache.org,新建环境变量ANT_HOME为ant目录,path为%ANT_HOME%/lib (三)编译发布android项目 1.生成build.xml文件 运行android update project -

Eclipse下配置Ant脚本 自动打包带签名的Android apk

虽然eclipse很少用了,但是在古老的项目上还是会用到.一个麻烦事是打带签名包的时候,非常不方便.下边纪录下配置ant,自动打包带签名apk的过程,作为备忘.(PC环境为MAC) 1,第一步得安ant,下载对应安装包,解压后配置环境变量: export ANT_HOME="/Users/yanzi/work/apache-ant-1.9.4" export PATH=${PATH}:${ANT_HOME}/bin 通过which ant检查是否安装成功. 2,在项目目录下运行:and

【转】Android项目使用Ant打包,生成build.xml

记不住,于是原帖转过来,请看原帖:http://blog.csdn.net/ms03001620/article/details/8490238 一.生成build.xml Eclipse中使用Ant为Android打包并且签名 SDK自带文件 在<sdk>tools/ant目录下这3个文件 其中build.xml和uibuild.xml中定义了大量基础构建方法和打包策略.我们只需要建立我们自己的build.xml并且存放在项目的根目录下,然后引用一下<sdk>tools/ant/

Mac中用Ant实现Android的批量打包碰到的一些问题以及解决方法

Hello!各位新年好! 昨天下午开始上班,开始整理以前的笔记,留个印记,以免遗忘,当然若能对别人有帮助也再好不过了,今日深圳天气好的不像话,阳光透过玻璃洒在脸上,舒爽! 首先打包需要准备的情况: 1,Ant和JDK得安装好了,Ant去官网下http://ant.apache.org/ 测试java环境,在控制台输入java -version 返回java版本号则表示成功 java version "1.6.0_65" Java(TM) SE Runtime Environment (