Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

java - 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.

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'"

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try com.stuff.MyConfigObject$MyEnum instead. The Proguard class specification expects $ as the separator for inner classes.

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...