[英]Proguard won't keep a class member's enums

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,它只會讓所需要的接口暴露出來。我有一個帶有一些成員變量的配置類和一些enum定義。我的proguard腳本保留了成員變量,但是,enum定義被混淆了。我已經嘗試了所有我能想到的方法來強迫proguard保留這些內部定義的和公共的enums,但是我不能讓它工作。

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.myconfigobjectmyenum”。

Thanks for the help!


1 个解决方案



Try com.stuff.MyConfigObject$MyEnum instead. The Proguard class specification expects $ 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 question for an explanation) and any public members of the enum. Any other members (e.g. private fields methods) will not be kept.


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

正如Jesse和我在評論中提到的——既然你正在處理一個庫,你還必須添加- keepattributes選項。從參考指南:

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




粤ICP备14056181号  © 2014-2020