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,一经查实,立即删除!

相关文章

sql注入修复方法是_旧的方法是修复我们可以看到的内容。

sql注入修复方法是When envisioning the futurestate of a company or a service, we’re usually faced with the challenge of designing for a customer that doesn’t exist yet. What do we mean by this? Well, they exist in the obvious sense, they’re just not ‘t…

library听证会_听证会

library听证会My Initial Experience with a Screen Reader我对屏幕阅读器的初步体验 As a new web developer, I have been learning to make my sites visually appealing but still be accessible, and all-in-all, it’s been going decent. I’ve been including cool ic…

jsoup测试例子

1、测试代码 import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class Test { public static void main(String[] args) { Test t new Test(); t.parseUrl(); } publ…

ux和ui_他们说,以UX / UI设计师的身份加入一家初创公司。 他们说,这会很有趣。

ux和uiSure, working in a startup environment sounds fun. The stories of flexibility and freedom that it entails spark curiosity into people’s minds, making it enticing to explore a career in the startup scene. In reality, working in a startup just present…

程序员在囧途

程序员在囧途&#xff1a;http://www.shenyisyn.org/2013/05/21/cxyzhc.htm转载于:https://www.cnblogs.com/Qiaoyq/archive/2013/05/22/3092904.html

架构师之路 扩充字段_扩大您作为设计师的业务影响力的四个基础

架构师之路 扩充字段While catching up with my designer friends during these days of quarantine, a common topic surfaced in all our conversations despite the different countries, cultures, companies, seniority levels, and paths in the field: 在这些隔离日中与…

android之隐式intent调用

直接上代码 MainActivity.java 1 package com.example.test1;2 3 import android.app.Activity;4 import android.content.Intent;5 import android.net.Uri;6 import android.os.Bundle;7 import android.view.View;8 import android.view.View.OnClickListener;9 import andr…

webflow_Webflow是否适合开发人员? 我的经验

webflowThe biggest problem with site builders is the code they generate is usually garbage. As I’ve recently discovered, this isn’t the case with 网站建设者最大的问题是他们生成的代码通常是垃圾。 正如我最近发现的&#xff0c;情况并非如此 Webflow, and alth…

1.蛋疼上路

开博客了&#xff01; 感觉会是很花时间的事。。转载于:https://www.cnblogs.com/-dante-/archive/2013/05/26/3099789.html

您的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…

编译器错误 CS1026

http://technet.microsoft.com/zh-cn/library/tc5zwdf7%28vvs.80%29转载于:https://www.cnblogs.com/Peter-Youny/archive/2013/05/26/3099808.html

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

交互设计精髓重点 (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…

软件过程软件Scrum敏捷开发

在写这篇文章之前&#xff0c;xxx已经写过了几篇关于改软件过程软件主题的文章,想要了解的朋友可以去翻一下之前的文章 1、什么是软件进程? 软件进程是为了建造高质量软件所需实现的任务的框架,即形成软件产品的一系列步骤,它划定了实现各项任务任务步骤,包括了中间产品、资源…

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.许多动…

HDU 3068 最长回文

Manacher算法练笔&#xff0c;O(n)求最长回文子串。 参考资料&#xff1a;http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 http://www.felix021.com/blog/read.php?2040 后缀数组和拓展KMP也可以求&#xff0c;不过时间复杂度都是O(nlogn)。 1 #include <cstd…

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?)你们中的某些…

说说godaddy

俗称狗他爹&#xff0c;是全世界最大的一级域名注册和服务商&#xff0c;中国只有国家是一级&#xff0c;万网等都是2级&#xff0c;如果你的域名是在万网注册的&#xff0c;那你就out啦&#xff0c;因为有诸多使用的限制&#xff0c;比如我之前为了试试万网的域名&#xff0c;…

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

分步表单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…