分析app打包中缩减包体大小的设置

一.minifyEnabled

minifyEnabled true

作用:启用缩减、混淆处理和优化功能(Enables code shrinking, obfuscation, and optimization)

分析:minifyEnabled 开启之后,主要是包含了3个功能

1.shrinking 缩减代码,主要是移除未使用的类,成员,方法和属性

2.obfuscation 混淆,主要是缩短类和成员的名称

3.optimization 优化,它内联并合并类和类成员,并在字节码级别优化所有方法

Proguard文件:可以概括为对上述3个功能的约束

-keep 保留类不被shrink同时也不被obfuscate

-dontshrink 关闭缩减代码 (对应shrinking

Specifies not to shrink the input. By default, ProGuard shrinks the code: it removes all unused classes and class members. It only keeps the ones listed by the various -keep options, and the ones on which they depend, directly or indirectly. It also applies a shrinking step after each optimization step, since some optimizations may open up the possibility to remove more classes and class members.

-dontobfuscate 关闭混淆(对应obfuscation

Specifies not to obfuscate the input class files. By default, ProGuard obfuscates the code: it assigns new short random names to classes and class members. It removes internal attributes that are only useful for debugging, such as source files names, variable names, and line numbers.

-dontoptimize 关闭优化(对应optimization

Specifies not to optimize the input class files. By default, ProGuard optimizes all code. It inlines and merges classes and class members, and it optimizes all methods at a bytecode level.

二.shrinkResources

shrinkResources true

注意:启用资源缩减,必须设置minifyEnabled = true,否则报错(Removing unused resources requires unused code shrinking to be turned on)

为什么要同时开启minifyEnabled = true,缩减代码可以更多的暴露出未使用的res

作用:启用资源缩减

分析:shrinkResources开启之后,会删除没有应用的资源的内容,不会删除文件本身

keep.xml文件:可以理解为对shrinkResources的详细设置

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2"
    tools:shrinkMode="strict" />

keep文件不能自动合并,如果sdk中和主包中都包含keep文件,需要将sdk中的内容合并到主包中

配置中keep标签是需要保留的res,discard是需要删除的res(一般不设置这个标签),重点是shrinkMode(缩减模式)

不设置shrinkMode默认是非严格模式,编译过程中会代码中查找code中的字符串,保存为一个string pool,然后在缩减

资源的时候回将在string pool中出现的资源保留下来(匹配规则不是通过完全匹配,有一定的规则)。

如果我们设置shrinkMode="strict",严格模式下一些通过字符串方式引用的资源将被删除,容易应用异常

三.zipAlignEnabled

zipAlignEnabled true

作用:表示是否对APK包执行ZIP对齐优化,减小zip体积,增加运行效率,release和debug默认都为true。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值