zl程序教程

您现在的位置是:首页 >  其它

当前栏目

ProGuard中keep到底有什么作用

什么 作用 到底 keep Proguard
2023-09-27 14:25:58 时间

一直以为keep就是不要混淆 近期发现还有另外一个作用 见官方文档

Testing your app should reveal any errors caused by inappropriately removed code, but you can also inspect what code was removed by generating a report of removed code.

To fix errors and force R8 to keep certain code, add a -keep line in the ProGuard rules file. For example:

-keep public class MyClass

Alternatively, you can add the Keep annotation to the code you want to keep. Adding Keep on a class keeps the entire class as-is. Adding it on a method or field will keep the method/field (and its name) as well as the class name intact. Note that this annotation is available only when using the AndroidX Annotations Library and when you include the ProGuard rules file that is packaged with the Android Gradle plugin, as described in the section about how to enable shrinking.

仔细读会发现keep作用是不被任何优化 除了混淆还有删除 因为ProGuard的过程中会清理没有使用的类或方法等代码 设置keep后 这部分就不会被作任何优化 包括被清理



ProGuard混淆 ProGuard技术的功能概括为以下4项: 1.压缩(shrinks) :检查并移除代码中无用的类,字段,方法,属性。 2.优化(optimizes):对字节码进行优化,移除无用的指令。 3.混淆(obfuscates):使用a,b,c,d等简短而无意义的名称,对类,字段和方法进行重名,这样即使代码被逆向工程,对方也比较难以读懂。 4.预检测(Preveirfy):在java平台上对处理后的代码进行再次检测。 (1) ProGuard 配置 -include{filename}:从给定的文件中读取配置参数。 -basedirectory{directoryname}:指定基础目录为