记一次shrink代码减包调研方案
// 会删减
if (false) {
AsyncPLog.i(TAG, "this is a test[1] on release on releasePlayControl");
}
// 不会优化
if (true) {
AsyncPLog.i(TAG, "this is a test[2] on release on releasePlayControl");
}
// 会删减
if (AbUtil.debuggleable()) {
AsyncPLog.i(TAG, "this is a test[3] on release on releasePlayControl");
}
// 不会优化
if (AbUtil.debuggleable2()) {
AsyncPLog.i(TAG, "this is a test[4] on release on releasePlayControl");
}
// 会删减
if (AbUtil.isDebuggable) {
AsyncPLog.i(TAG, "this is a test[5] on release on releasePlayControl");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上述其中 AbUtil 的几个方法定义如下:
public static final boolean isDebuggable = false;
// isDebuggable2 未采用 final 进行修饰
private static boolean isDebuggable2 = false;
public static boolean debuggleable() {
return isDebuggable;
}
public static boolean debuggleable2() {
return isDebuggable2;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
在编译阶段, final 类型内敛,确认不会执行的代码 scope, 会被删除
# smali 分析
将打包的 release 包采用 apktool 反解成 smali, apktool d -r xxx.apk. 搜索查看相关字符串。 发现指有在明确 if(false) 中的 scope 代码会主动去处, 其他所有 case 都不会去除
# 思考
其他在运行时可以确定不需要的如:
- logger.d level 的日志
上次更新: 2025/10/09, 23:53:03