Android开发如何有效减小APK的体积


ProGuard

Proguard可以删除无效的java code,显著地减小apk体积,但是要注意使用proguard之后要测试app,有时会导致一些bug。

Split Apk

Split Apk可以为根据设备密度和ABI创建特定的Apk包,大大减少每个apk的体积。

ReDex

使用Facebook开源的ReDex,ReDex在压缩代码的同时,还可以提高性能。

ShrinkResources

在gradle中使用ShrinkResources可以去除无效的资源文件,从而减少最终的apk大小,在build.gradle中使用shrinkResources:

1
2
3
4
5
release {
...
shrinkResources true
...
}
resConfigs

使用resConfigs来去除非必要的本地化资源:

1
2
3
4
5
defaultConfig {
...
resConfigs "en", "hi"
...
}
Vector Drawables

Vector Drawables相比图片资源,要小很多。

debugCompile

使用debugCompile可以避免把debug期间用到的library打包到release apk中。

WebP file

使用WebP格式的图片资源,相比PNG和JPEG,WebP在保证质量的同时,压缩率更好。

  • 在需要的时候使用9-patch资源

  • 优化图片资源

可以使用TinyPNG, OptiPNG等压缩图片。