前言
安装包性能优化为了减少应用程序安装包的大小,不仅仅可以降低用户的网络数据流量还减少了下载等待的时间。毋庸置疑,尽量减少程序安装包的大小是十分有必要的。通常来说,减少程序安装包的大小有两条规律:要么减少程序资源的大小,要么就是减少程序的代码量。下面从几个方面来介绍:
1、图片压缩:
图片:apk里面的资源图片,apk里的图片大小需要UI去进行压缩。
svg图片:svg图片中包含了一些图片的描述,svg是牺牲CPU的计算能力,达到节省空间目的,复杂的图片不建议用svg。
webp:谷歌现在非常提倡的使用。保存图片比较小。webp的无损压缩比PNG文件小45%左右,即使PNG进过其他的压缩工具压缩后,仍然可以减小到PNG的28%。
2、资源动态加载
比emoji表情、换肤、动态下载的资源、一些模块的插件化动态添加。
3.Lint工具 建议优化的点
1)检测没有用的布局 删除
2)未使用到的资源 比如 图片 —删除
3)建议String.xml有一些没有用到的字符。
4、极限压缩
7zZip工具的使用。
5.Proguard 混淆
1)可以删除注释和不用的代码。
2)将java文件名改成短名a.java,b.java
3)方法名等 CommonUtil.getDisplayMetrix();–》a.a()
6、在常规的安装包的优化之外继续压缩—资源文件再压缩(着重介绍这一点)
当我们应用经过编译打包之后,相应的文件夹有对应的映射关系:res/drawable/ic_launcher.png —– > 0x7f020000,而这种压缩是在做了代码混淆之后再做的混淆,针对的res下的资源文件进行压缩,例如:R.string.description—>R.string.a或者res/drawable/ic_launcher.png图片改成a.png,还可以更加夸张把文件夹的名字都改掉:res/drawable/ic_launcher.png图片改成r/d/a.png
实现原理:
读取resources.arsc二进制文件,然后修改某一段一段的字节。例如有一段叫做:res/drawable/ic_launcher.png 在自己数组当中的第800位-810位
将这一段第800位-810位替换成改成r/d/a.png 的字节码。
相信目前用的最多的也就是微信的资源混淆打包工具:
https://github.com/shwenzhang/AndResGuard
1、准备工作
**jar:**AndResGuard的执行jar包
签名文件test.jks: 可以通过Android studio或者eclipse创建
配置文件config.xml:重要的配置也用中文做了简单的解释,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<resproguard>
<!--defaut property to set -->
<issue id="property">
<!--whether use 7zip to repackage the signed apk, you must install the 7z command line version in window -->
<!--sudo apt-get install p7zip-full in linux -->
<!--and you must write the sign data fist, and i found that if we use linux, we can get a better result -->
<seventzip value="false"/>
<!--the sign data file name in your apk, default must be META-INF-->
<!--generally, you do not need to change it if you dont change the meta file name in your apk-->
<metaname value="META-INF"/>
<!--if keep root, res/drawable will be kept, it won't be changed to such as r/s-->
<keeproot value="false"/>
</issue>
<!--whitelist, some resource id you can not proguard, such as getIdentifier-->
<!--isactive, whether to use whitelist, you can set false to close it simply-->
<!--这里设置了那些资源不混淆的,如:友盟相关的资源就不能混淆,混淆后将会出现问题-->
<issue id="whitelist" isactive="true">
<!--you must write the full package name, such as com.tencent.mm.R -->
<!--for some reason, we should keep our icon better-->
<!--and it support *, ?, such as com.tencent.mm.R.drawable.emoji_*, com.tencent.mm.R.drawable.emoji_?-->
<path value="com.lpf.argt.R.drawable.ic_launcher"/>
<path value="com.lpf.argt.R.mipmap.ic_launcher"/>
<path value="com.lpf.argt.R.string.app_name"/>
<path value="com.lpf.argt.R.drawable.icon"/>
<path value="com.lpf.argt.R.string.umeng*"/>
<path value="com.lpf.argt.R.layout.umeng*"/>
<path value="com.lpf.argt.R.drawable.umeng*"/>
<path value="com.lpf.argt.R.anim.umeng*"/>
<path value="com.lpf.argt.R.color.umeng*"/>
<path value="com.lpf.argt.R.style.*UM*"/>
<path value="com.lpf.argt.R.style.umeng*"/>
<path value="com.lpf.argt.R.id.umeng*"/>
</issue>
<!--keepmapping, sometimes if we need to support incremental upgrade, we should keep the old mapping-->
<!--isactive, whether to use keepmapping, you can set false to close it simply-->
<!--if you use -mapping to set keepmapping property in cammand line, these setting will be overlayed-->
<!-- <issue id="keepmapping" isactive="false"> -->
<!--the old mapping path, in window use \, in linux use /, and the default path is the running location-->
<!--<path value="{your_mapping_path}"/> -->
<!--</issue> -->
<!--compress, if you want to compress the file, the name is relative path, such as resources.arsc, res/drawable-hdpi/welcome.png-->
<!--what can you compress? generally, if your resources.arsc less than 1m, you can compress it. and i think compress .png, .jpg is ok-->
<!--isactive, whether to use compress, you can set false to close it simply-->
<issue id="compress" isactive="false">
<!--you must use / separation, and it support *, ?, such as *.png, *.jpg, res/drawable-hdpi/welcome_?.png-->
<path value="*.png"/>
<path value="*.jpg"/>
<path value="*.jpeg"/>
<path value="*.gif"/>
<path value="resources.arsc"/>
</issue>
<!--sign, if you want to sign the apk, and if you want to use 7zip, you must fill in the following data-->
<!--isactive, whether to use sign, you can set false to close it simply-->
<!--if you use -signature to set sign property in cammand line, these setting will be overlayed-->
<issue id="sign" isactive="true">
<!--the signature file path, in window use \, in linux use /, and the default path is the running location-->
<!--这里设置签名文件的路径-->
<path value="F:/DATA/code/build/test.jks"/>
<!--storepass-->
<storepass value="123456789"/>
<!--keypass-->
<keypass value="123456789"/>
<!--alias-->
<alias value="test"/>
</issue>
</resproguard>
7-zip工具的安装:
bat文件:
新建一个txt文件夹,将如下指令拷贝进去,然后将.txt的后缀更换为.bat
Java -jar AndResGuard-cli-1.1.16.jar test.apk -config config.xml -out release -zipalign F:/DATA/code/software/sdk/build-tools/23.0.2/zipalign -7zip "E:/Program Files/7-Zip/7z.exe" -signature F:/DATA/code/build/test.jks 123456789 123456789 test
F:/DATA/code/software/sdk/build-tools/23.0.2/zipalign为SDK自带的工具,具体路径根据自己的实际情况配置
E:/Program Files/7-Zip/7z.exe为前一步中安装的7-zip的路径
F:/DATA/code/build/test.jks为签名文件,后面紧跟的是2个密码和alias
test.apk:测试apk,这里的apk为任意apk均可以,签名过的apk必须是使用上面bat中配置的签名文件进行签名的;同时混淆后的apk也是没有问题的。
2、开始混淆
按照上面步骤中的操作将配置文件及资源文件配齐之后,双击运行test.bat文件,出现以下日志:
同时在同目录下会出现一个“release”的文件夹,其中包含了混淆后的资源文件、源文件及混淆文件的对应关系、签名后的apk和未签名的apk,详情如下:
再次查看apk的资源文件,发现res文件夹已经变成了r,里面的各个资源也全部更换成其他的别名。
资源代码都混淆,资源和配置文件引用均无问题;为了测试包含第三方库是否会有影响,引入了ButterKnife测试,apk能正常运行。
混淆前与混淆后的大小对比
资源混淆前:
资源混淆后:
上面空项目就压缩了70KB-100KB的大小,相信在实际的项目开发中,资源文件更多,压缩率会更高。
参考链接:http://blog.csdn.net/lupengfei1009/article/details/57088682