Android Tools研究探讨一:zipalign
zipalign
http://developer.android.com/tools/help/zipalign.html
什么是Zipalign?
Zipalign是一个档案整理工具,它首次被介绍是在Android 1.6版本的SDK(Software Development Kit)软件开发工具包中。它优化Android应用程序包(APK)到整合包, 以使Android操作系统与应用程序之间的交互作用更有效率,然后应用程序和整体系统的运行速度更快,发挥更大的潜能。它使Zipaligned的应用程序执行时间达到最低限度,其最终结果导致当设备运行APK应用程序时占更少的RAM(Random Access Memory)随机访问内存
Zipalign如何准备的执行(工作)呢?
在Android的操作环境中,存储在每个应用程序包的数据文件通过多个进程访问,例如,程序安装工具将读取数据列表确定相关的权限;因为包括显示通知等在内的多种原因,系统服务器可以读取这些资源;主界面应用程序要读取资源以便获取应用程序的名称与图标等。因为Android是基于一个真正的多任务操作基础架构,这些文件是不断地读取。最后但也是最重要的,应用程序本身读取体现的数据
因为Android操作系统基于Linux架构,存储单元布置(Memory Mapping)在有效的处理过程中起着一个关键的作用。从本质上而讲,为Android操作系统资源的处理代码最佳的整理是4字节界层。这个意思是说,如果APK应用程序包是存储单元布置到4字节界层,依据相应的整理,操作系统将不需要通读整个应用程序包以获取所需要的数据表,而每一个系统处理都将提前知道到哪里去寻找它所需要的资源,因此执行效率更快(运行更平滑,速度更快)
总结而讲,Zipalign一个APK应用程序的结果即是让所有的未压缩的数据以整合包的形式被整理到4字节界层,让所有的数据可以直接从存储器映象读取。而因为正访问的代码没有通读到整个应用程序包使得执行消耗的程序运行内存(RAM)降低。最终,操作系统整体上的速度会更快
UnZipalign(未整理)的APK应用程序包有什么劣势呢?
这是很容易理解的,对于未整理的应用程序包的情况,资源读取将会缓慢,程序运行内存(RAM)的使用将会处在一个较高的范围。它也取决于当前有多少未整理的应用程序。例如,如果有着较少的应用程序,然后有一个未整理的主界面程序,在启动时间上,你能观察到更慢的应用程序,这是最理想的情况。 对于一个糟糕的情况,如有着许多的未整理的应用程序,将会导致系统反复的启动和结束进程,系统运行将会滞后,电池的使用时间将会大幅度降低
我们应该怎么做呢?
当前,Zipalign档案整理工具已变成Android SDK(Software Development Kit)软件开发工具包的一部分。它可以在电脑上已安装好的Android SDK “tools” 或“platform-tools” 目录或文件夹中被查看
对于使用Zipalign档案整理工具,可以简单地输入并执行Command命令——
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
其中,infile.apk是源文件,outfile.apk是输出文件
更多的,我们也可以确认APK应用程序的整理,输入并执行Command命令——
zipalign -c -v <alignment> existing.apk
其中,existing.apk能被任何你需要的应用程序包获得确认。
另外<alignment>命令需要是一个整数值,不然,命令会返回无效的错误提示。而这个值,虽然可以是任何的整数,但必须始终是4,因为这将提供32位元的整理层,任何其他的值,它们都是有效的,但不起任何作用
对于这些Command命令的标识——
-f—重写存在的outfile.zip
-v—提供详细的输出
-c—确认一个给定的文件的整理
有什么要注意呢?
Zipalign操作必须且仅在标记APK文件附有个人加密钥之后。如果在标记之前进行Zipalign操作,标记过程将会干扰整理
原文档说明:
zipalign
zipalign is an archive alignment tool that provides important optimization to Android application (.apk) files. The purpose is to ensure that all uncompressed data starts with a particular alignment relative to the start of the file. Specifically, it causes all uncompressed data within the .apk, such as images or raw files, to be aligned on 4-byte boundaries. This allows all portions to be accessed directly with mmap()
even if they contain binary data with alignment restrictions. The benefit is a reduction in the amount of RAM consumed when running the application.
This tool should always be used to align your .apk file before distributing it to end-users. The Android build tools can handle this for you. When using Eclipse with the ADT plugin, the Export Wizard will automatically zipalign your .apk after it signs it with your private key. The build scripts used when compiling your application with Ant will also zipalign your .apk, as long as you have provided the path to your keystore and the key alias in your project ant.properties
file, so that the build tools can sign the package first.
Caution: zipalign must only be performed after the .apk file has been signed with your private key. If you perform zipalign before signing, then the signing procedure will undo the alignment. Also, do not make alterations to the aligned package. Alterations to the archive, such as renaming or deleting entries, will potentially disrupt the alignment of the modified entry and all later entries. And any files added to an "aligned" archive will not be aligned.
The adjustment is made by altering the size of the "extra" field in the zip Local File Header sections. Existing data in the "extra" fields may be altered by this process.
For more information about how to use zipalign when building your application, please read Signing Your Application.
Usage
To align infile.apk
and save it as outfile.apk
:
zipalign [-f][-v]<alignment> infile.apk outfile.apk
To confirm the alignment of existing.apk
:
zipalign -c -v <alignment> existing.apk
The <alignment>
is an integer that defines the byte-alignment boundaries. This must always be 4 (which provides 32-bit alignment) or else it effectively does nothing.
Flags:
-f
: overwrite existing outfile.zip-v
: verbose output-c
: confirm the alignment of the given file
刚认真研究发现一点:
When using Eclipse with the ADT plugin, the Export Wizard will automatically zipalign your .apk after it signs it with your private key.
也就是说用eclipse export打包的apk是已经zipalign过的了
所以这东东是不是有点鸡肋呢。。。