java Proguard 不会保留班级成员的枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6285623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:12:57  来源:igfitidea点击:

Proguard won't keep a class member's enums

javaenumsproguard

提问by Jesse

I'm working on a library that is distributed as a java jar, and I'm running proguard on it in such a way as to only leave the required interfaces exposed. I have a configuration class with a bunch of member variables and some enum defines. My proguard script preserves the member variables fine, however, the enum definitions are being obfuscated. I've tried everything I can think of to force proguard to retain these internally defined and public enums, but I can't get it to work.

我正在开发一个作为 java jar 分发的库,并且我正在以这样的方式运行 proguard,以便只暴露所需的接口。我有一个包含一堆成员变量和一些枚举定义的配置类。我的 proguard 脚本很好地保留了成员变量,但是,枚举定义被混淆了。我已经尝试了所有我能想到的方法来强制 proguard 保留这些内部定义和公共枚举,但我无法让它工作。

Right now I'm using:

现在我正在使用:

-keep public class com.stuff.MyConfigObject {
    public *;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

If I try:

如果我尝试:

-keep public enum com.stuff.MyConfigObject.MyEnum

I get an ambiguous error: "Note: the configuration refers to the unknown class 'com.stuff.MyConfigObject.MyEnum'"

我收到一个模棱两可的错误:“注意:配置指的是未知类 'com.stuff.MyConfigObject.MyEnum'”

Thanks for the help!

谢谢您的帮助!

回答by Kevin K

Try com.stuff.MyConfigObject$MyEnuminstead. The Proguard class specificationexpects $as the separator for inner classes.

试试吧com.stuff.MyConfigObject$MyEnum。Proguard类规范期望$作为内部类的分隔符。

Actually, for what you want maybe the best option is something like this:

实际上,对于您想要的,最好的选择可能是这样的:

-keep public enum com.stuff.MyConfigObject$** {
    **[] $VALUES;
    public *;
}

This will keep only the required members for all enums nested within MyConfigObject- the required members being the $VALUES[]array (see this questionfor an explanation) and any publicmembers of the enum. Any other members (e.g. private fields methods) will not be kept.

这将只保留所有enum嵌套在MyConfigObject其中的必需成员 - 必需成员是$VALUES[]数组(有关解释,请参阅此问题)和public枚举的任何成员。不会保留任何其他成员(例如私有字段方法)。



As noted by Jesse and myself in the comments - since you are processing a library, you must also add the -keepAttributesoption. From the reference guide:

正如 Jesse 和我自己在评论中指出的那样 - 由于您正在处理一个库,您还必须添加该-keepAttributes选项。从参考指南:

For example, you should at least keep the Exceptions, InnerClasses, and Signature attributes when processing a library.

例如,在处理库时,您至少应该保留 Exceptions、InnerClasses 和 Signature 属性。

回答by igork

to keep all internal enums try this:

要保留所有内部枚举,请尝试以下操作:

-keep class * {
    public enum **;
}

it saves me from writing every of 123 enum in proguard config.

它使我免于在 proguard 配置中编写 123 个枚举中的每一个。

Also, don't forget -keepAttributesif you are processing a library

另外,不要忘记-keepAttributes您是否正在处理库