Android客户端打包方案分享

基本介绍

Android应用的自动化打包是应用持续集成以及多渠道发布的基础。当前Android客户端自动化打包的主要有两种方式,Ant和Maven。两种方式本质上都是调用Android SDK里面提供的工具,不过各自有各自的特点。

1. Ant脚本

好处:开发成本较低,android sdk默认提供ant的打包脚本,可以根据需要进行修改和扩展。

不足:不天然支持包的依赖管理,需要自己写代码控制应用的依赖。

2. Maven

好处:天然支持包的依赖管理,依赖管理不需要写过多的代码。Maven插件机制容易扩展和定制,方便实现自己的打包流程。

不足:从头编写Maven脚本,或者maven插件,大家对Maven的熟悉程度稍低,门槛稍高。

 

这里介绍一种Ant+shell脚本的自动化打包方案。Ant脚本直接复用sdk提供的脚本并稍作扩展,对应用的包依赖和打包流程自动化控制,选择使用shell实现。

 

具体实施

打包整体逻辑

Shell脚本(见源码,感谢@今为帮忙重构)

Shell脚本实现了上图中的逻辑,比较简单,这里不再细述,可以看一下源码。

Ant脚本定制

这里主要描述一下对Android自带ant脚本的定制。

Android SDK里面自带的ant脚本(在~/android-sdk-linux_x86/tools/ant/build.xml),如果你的Android主工程没有支持ant构建,那么可以在主工程根目录下通过运行

android update project -p . –n projectname

来增加ant构建支持,运行后会在根目录下生成build.xml(通过<import file="${sdk.dir}/tools/ant/build.xml" />继承自SDK中的build.xml)

SDK中的build.xml有release task,包括了android打包的完成流程,看代码如下:

<target name="release"depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"description="Builds the application in release mode."></target>

由代码可见,release任务依赖了8个其它的任务,其实核心的任务就2个:

l   -package 编译,生成未签名APK

l   -release-sign 签名

总结起来整个过程分解为:编译生成未签名包签名两个阶段。我们要做的就是在这两个阶段中增加一个替换渠道号的步骤,如上图中描述,主要过程核心流程:

  1. 编译生成未签名包
  2. 循环替换每个渠道号到未签名包
  3. 循环针对替换后的未签名包生成签名包

 

步骤1和3其实就是原来release任务的分解,代码如下:

<target name="release2"depends="-set-release-mode, -package, -post-package, -release-prompt-for-password, -release-nosign, -post-build"description="Builds the application in unsigned mode."></target>
<target name="signapk"depends="-set-release-mode,-release-sign, -post-build"description="Sign apk.">

Android应用根目录下自动生成的build继承自系统的ant脚本,该脚本中的ant task可以直接复用,因此有了上面自己写的两个任务release2和signapk,分别表示了1和3阶段的任务。

步骤2的逻辑系统ant脚本里面没有现成的任务,需要自己编写,代码如下:

<target name="modichannel"><exec executable="${aapt}" taskName="remove"><arg value="remove" /><arg value="-v" /><arg value="./bin/${appname}-release-unsigned.apk" /><arg value="assets/channel" /></exec><exec executable="${aapt}" taskName="add"><arg value="add" /><arg value="-v" /><arg value="./bin/${appname}-release-unsigned.apk" /><arg value="assets/channel" /></exec></target>

这个任务做了两件事儿,第一,调用android sdk的aapt工具先删除未签名包中的默认渠道文件,第二,添加新渠道文件到未签名包。注意这里一定要用android自带的aapt工具操作apk压缩包,如果使用zip会对raw文件进行压缩,这个似乎并不符合apk打包的规范(aapt对raw文件的压缩有处理,注:对于zip和aapt压缩的区别并没有过多深入的了解,有兴趣的可以进一步探讨),导致打包出来的apk读取raw目录下的资源失败。

 

总结

整个打包流程,代码量不大,用shell实现打包流程控制,扩展了三个ant task。用这种方案,可以做到针对多渠道发布,只需要一次编译,多次渠道替换和签名,因为android编译很耗时,这样可以大大减少渠道的打包时间。

 

源码

http://code.taobao.org/p/android_build/src/

问题

总的来说还是比较简单的,当然整个过程也遇到了一些问题和雷区,列举一下,避免再次犯错,也算经验积累。

  1. 需要了解ant的继承机制,和任务依赖机制,这个是这次改造的基础,避免重复劳动,直接复用android自带ant脚本中的任务。
  2. 最纠结的过程是如何拆分release流程,插入渠道替换逻辑。这个过程遇到的问题最多。其实最后总结下来,拆分ant任务最简单的方式,在于看这个任务的子任务是否有共同的上下文依赖(比如运行时的环境变量依赖,等等),拆分到两个任务中的子任务之间最好不要有这种依赖,否则运行起来会出现变量值错误的问题,当然如果搞清楚整个流程也可以修改代码,但是工作量比较大。
  3. 对于apk压缩包的操作请使用android自带的aapt,避免直接使用zip,直接使用zip会将apk里面的raw资源压缩,导致有些rom下读取raw文件失败。

其实生成未签名包之后无需解压替换,直接aapt操作压缩包替换就好了,这样可以避免使用zip。

编写shell时当前路径要控制好,否则会很麻烦错误不断。

转载于:https://www.cnblogs.com/youngerbaby/archive/2013/05/18/3085548.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274240.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

您的UX库不只是书籍

hp ux 密码不过期Looking back on past self, one thing I wish I’d realised is the importance of keeping notes of everything.回顾过去的自我&#xff0c;我希望我意识到的一件事是记录所有事情的重要性。 This means everything interesting I’ve read and written; e…

交互设计精髓_设计空间的精髓

交互设计精髓重点 (Top highlight)什么是空间&#xff1f; (What is Space?) Space is the dimension of height, depth and width within which all things exist and move. Space or Empty space or White space or Negative space are alias given to describe intensional…

ux和ui_UI和UX设计人员的47个关键课程

ux和ui重点 (Top highlight)This is a mega-list of the most critical knowledge for UI, UX, interaction, or product designers at any level.这是所有级别的UI&#xff0c;UX&#xff0c;交互或产品设计人员最关键的知识的大清单。 Many of these lessons are also appli…

深入理解Java内存模型(七)——总结

处理器内存模型 顺序一致性内存模型是一个理论参考模型&#xff0c;JMM和处理器内存模型在设计时通常会把顺序一致性内存模型作为参照。JMM和处理器内存模型在设计时会对顺序一致性模型做一些放松&#xff0c;因为如果完全按照顺序一致性模型来实现处理器和JMM&#xff0c;那么…

沉浸式ui设计_有助于沉浸的视频游戏UI —武器轮

沉浸式ui设计Many action-adventure games rely on the feeling of thrills via bullets, fire, grenade, more bullets, and gigantic booms. The way to enable all these is to offer a massive arsenal, from machetes to assault rifles all the way till bazookas.许多动…

ux设计师薪水_客户现在也是UX设计师

ux设计师薪水Some of you probably know by now, I’m not too fond of the monster the UX industry has become. It’s overblown, overcomplicated and often dishonest towards the clients. It’s also in itself undefined. (where is the E in Experience?)你们中的某些…

分步表单_角色创建分步指南

分步表单The first thing most of us designers are taught is the concept of personas and the necessity of them when it comes to UX and product design. However, knowing is different from applying and it can be difficult to know where to begin when we’re aske…

svg配合css3动画_带有Adobe Illustrator,HTML和CSS的任何网站的SVG动画

svg配合css3动画A top trend in web design for 2020 is the increased use of SVG animations on web pages and in logo design. In this article, we will implement a simple and straight forward method to create relatively complex animation. We will use Adobe Illu…

基于pt100温度计仿真_基于8pt网格的设计系统

基于pt100温度计仿真重点 (Top highlight)This article is the 2nd in a two part series — to the previous chapter in which I demonstrate how to establish an 8pt grid.本文是该系列文章的第二部分 &#xff0c;这是上一章 的第二部分 &#xff0c;在上一章中&#xff0…

利用 k8s 建立软件商店_为企业建立应用商店

利用 k8s 建立软件商店It’s June 2019. I’m sitting in a conference room in Research Triangle Park in North Carolina. At the end of the table are the two executives that have been tapped to lead a new endeavor on behalf of IBM’s $34 billion acquisition of …

苹果复兴_类型复兴的故事:来自Type West的经验教训

苹果复兴Last Fall, I began the 去年秋天&#xff0c;我开始 在旧金山的Type West program at the Letterform档案库中Letterform Archive in San Francisco. For those of you who don’t know, the Letterform Archive is creative heaven — a type nerd’s letter art co…

C#调用ATL COM

作者&#xff1a;朱金灿 来源&#xff1a;http://blog.csdn.net/clever101 简单介绍C#程序如何调用ATL编写的COM组件。 首先新建一个ATL工程&#xff0c;具体如下&#xff1a; 1. 填写工程名称和路径&#xff0c;如下图&#xff1a; 2. 选择工程的服务器类型为动态链接库&a…

浪潮世科和浪潮软件什么关系_社交图形浪潮

浪潮世科和浪潮软件什么关系Nowadays, the cornucopia of graphics seems like a given. However, it was not so long ago that infographics were scarce and lived in closed ecosystems. The majority of graphics were published in newspapers, magazines, or books, and…

PHP图形图像的典型应用 --常用图像的应用(验证码)

php生成动态的验证码&#xff0c;是php防止恶意登陆或者注册等常规手段-废话不多说&#xff0c;直接看例子。&#xff08;只是一个简单的应用&#xff0c;如果要安全或者更复杂的&#xff0c;请期待我以后的文章&#xff09; PHP生成验证码核心文件 (checks.php): <?php/*成…

写saas创业的书_我在SaaS创业公司担任UX设计师的第一个月中学到的三件事

写saas创业的书I recently transitioned from being a copywriter at an ad agency to a UX Designer at a SaaS startup. To add more multidisciplinary skills into the mix, I graduated with a Bachelor in Accountancy.我最近从一名广告代理商的撰稿人过渡到了SaaS初创公…

ui项目答辩中学到了什么_我在UI设计9年中学到的12件事

ui项目答辩中学到了什么重点 (Top highlight)I know these can seem a bit clich but I will try to explain everything from my own experience.我知道这些内容似乎有些陈词滥调&#xff0c;但我会尝试根据自己的经验来解释所有内容。 第一名 (No.1 Never assume) The first…

ux的重要性_UX中清晰的重要性

ux的重要性重点 (Top highlight)Times, since the very first occurrences of web design in the 90’s, have changed a lot design-wise. The particular technology and its applications got more stable. Human-computer interaction (HCI) was deeply researched, design…

可靠消息最终一致性设计_如何最终启动您的设计产品组合

可靠消息最终一致性设计It’s not a secret that most designers procrastinate on their portfolios whether it is to update them or to create them in the first place.大多数设计师在更新产品组合时还是拖延产品组合并不是秘密。 首先创建它们 。 Hopefully, by the e…

游戏用户体验指标_电子游戏如何超越游戏化的用户体验

游戏用户体验指标游戏UX (GAMES UX) During a time when the time spent on video games has reached record breaking heights, due to excessive time indoors, gamification has more of a place now than ever before.d uring的时候花在视频游戏的时间已经达到了 破纪录的高…

JAVA编程心得-JAVA实现CRC-CCITT(XMODEM)算法

CRC即循环冗余校验码&#xff08;Cyclic Redundancy Check&#xff09;&#xff1a;是数据通信领域中最常用的一种差错校验码&#xff0c;其特征是信息字段和校验字段的长度可以任意选定。 1 byte checksum CRC-16 CRC-16 (Modbus) CRC-16 (Sick) …