Can we shrink all classes but only obfuscate some with proguard?

百般思念 提交于 2021-01-03 14:39:36

问题


Typically, excluding a class with -keep prevents the class from being obfuscated

However it also prevents it from being shrunk.

Is it possible to define a proguard-project.txt that will shrink all classes except those that are excluded with -keep, but also obfuscate only a specific subset of the classes?

The aim is to use proguard to keep below the android 65k method limit, while also obfuscating first party code ONLY within the APK.

Thanks


回答1:


Yes, you can add the modifier allowshrinking to the -keep options that should only apply to the obfuscation (and optimization) steps. For instance:

-keep,allowshrinking class com.example.SomeClass

The specified class may be removed if it appears unused in the shrinking step, but otherwise, its name will be preserved in the obfuscation step.




回答2:


Eric's answer is good, there is also another way.

First, there is shorthand for:

-keep,allowshrinking

You can use:

-keepnames

You can also do it using the inverse:

-keepnames class !com.example.apackage.** {*;}

So this will obfuscate all classes inside apackage and nothing else, while still allowing dead code stripping on everything.

Another note is if you have obfuscation turned on, it will strip all of the meta data like file names and line numbers which will break debuggers and stack traces. If you want them to work you can add these lines:

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable


来源:https://stackoverflow.com/questions/24809514/can-we-shrink-all-classes-but-only-obfuscate-some-with-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!