包体积压缩

AndroidBuildProcess

Shrink your code

To shrink your app’s code, R8 first determines all entry points into your app’s code based on the combined set of configuration files. These entry points include all classes that the Android platform may use to open your app’s Activities or services. Starting from each entry point, R8 inspects your app’s code to build a graph of all methods, member variables, and other classes that your app might access at runtime. Code that is not connected to that graph is considered unreachable and may be removed from the app.

Strip native libraries

Shrink your resources

Resource shrinking works only in conjunction with code shrinking. After the code shrinker removes all unused code, the resource shrinker can identify which resources the app still uses. This is especially true when you add code libraries that include resources—you must remove unused library code so the library resources become unreferenced and, thus, removable by the resource shrinker.

Obfuscate your code

The purpose of obfuscation is to reduce your app size by shortening the names of your app’s classes, methods, and fields. The following is an example of obfuscation using R8

Code optimization

In order to shrink your app even further, R8 inspects your code at a deeper level to remove more unused code or, where possible, rewrite your code to make it less verbose.


10536027字节压缩到4926912字节,压缩了将近53%

总结

\1. 脚本中开启资源混淆和资源压缩

\2. 用7zip代替zip

3.gradle脚本中开启代码混淆优化和无用资源删除

\4. 用更小的图,使用压缩工具压缩图片大小

\5. 去除无用的资源,语言,本地so库,二方三方库和分辨率

\6. 用更小的库

\7. 尝试将android support库彻底踢出你的项目

\8. 定期清理代码

\9. 尝试用H5编写界面,图片云端获取

\10. 尝试插件化业务模块

\11. 寻找到zip文件夹中所有用STORE形式存储的文件(不限于raw目录下),尝试压缩,以及替代方案加载这些资源

12. 尝试webp的图片加载方案,寻求突破

最后,继续学习和尝试新的优化方案

参考

APK 瘦身记,如何实现高达 53% 的压缩效果

Android App包瘦身优化实践

https://github.com/shwenzhang/AndResGuard

http://tools.android.com/tech-docs/new-build-system/build-workflow

https://developer.android.com/studio/build/shrink-code