如何告诉 proguard 保持枚举常量和字段
2022-09-01 12:25:32
我尝试过混淆我们的Web应用程序,这些应用程序使用spring,jaxb并依赖于注释和反射。我应用了互联网上找到的许多食谱来保留一些类,属性,注释和枚举。但是枚举仍然存在问题。我已经能够保留枚举常量应用 http://proguard.sourceforge.net/manual/examples.html#enumerations 的配置:
-keepclassmembers,allowoptimization enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
乍一看,它看起来像工作解决方案和常量保留,所以(Class.getEnumConstants())返回正确的值列表。但是当我尝试按名称检索任何字段时,我得到了NoSuchFieldException。
问题来自jaxb反射导航器,请查看代码:
public Field[] getEnumConstants(Class clazz) {
try {
Object[] values = clazz.getEnumConstants();
Field[] fields = new Field[values.length];
for (int i = 0; i < values.length; i++) {
fields[i] = clazz.getField(((Enum) values[i]).name());
}
return fields;
} catch (NoSuchFieldException e) {
// impossible
throw new NoSuchFieldError(e.getMessage());
}
}
我完全落入“不可能”的分支。我认为查看调试会话屏幕截图(还有列出的常量)很容易理解:
如果我尝试获取字段,它们会被混淆为a,b,c,d,e,f:
我的 proguard 配置现在看起来像(去掉一些库列表和 kipp 关于 proguard 抱怨的特定类、字段和方法):
-injars core-3.15.rc5.6.jar
-outjars core-3.15.rc5.6.proguard.jar
-libraryjars <java.home>/lib/rt.jar
-libraryjars ... # Other libs listed, strip out for shortness
-printmapping core-3.15.rc5.6.proguard.map
-keep public class ru.rlh.egais.portal.backend.controller.rest.**
-keep public class ru.rlh.egais.portal.backend.integration.soap.service.**
# http://midgetontoes.com/blog/2015/06/26/tips-for-using-proguard-with-spring-framework
-optimizations !class/marking/final
-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF,META-INF/spring.*,spring/*
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
# Also tried:
# -keepattributes **
-allowaccessmodification
-dontshrink
-dontoptimize
-dontusemixedcaseclassnames
-keepdirectories
-keep @org.springframework.transaction.annotation.Transactional class *
-keep @org.springframework.stereotype.Service class *
-keep @org.springframework.stereotype.Repository class *
-keep @org.springframework.stereotype.Controller class *
-keep @org.springframework.stereotype.Component class *
-keep @org.springframework.beans.factory.annotation.Autowired class *
-keep @org.springframework.web.bind.annotation.ResponseBody class *
-keep @org.springframework.web.bind.annotation.RequestMapping class *
-keep @org.springframework.stereotype.Repository class *
-keep @javax.annotation.Resource class *
-keep @org.springframework.cache.annotation.EnableCaching class *
-keep @org.springframework.context.annotation.Configuration class *
-keepclassmembers class * {
@org.springframework.beans.factory.annotation.* *;
@org.springframework.beans.factory.annotation.Qualifier *;
@org.springframework.beans.factory.annotation.Value *;
@org.springframework.beans.factory.annotation.Required *;
@org.springframework.context.annotation.Bean *;
@javax.annotation.PostConstruct *;
@javax.annotation.PreDestroy *;
@org.aspectj.lang.annotation.AfterReturning *;
@org.aspectj.lang.annotation.Pointcut *;
@org.aspectj.lang.annotation.AfterThrowing *;
@org.aspectj.lang.annotation.Around *;
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
那么,我的问题是,如何完全防止公共枚举混淆?在这两种情况下 - 使用其常量(class.getEnumConstants())和字段(class.getFields())。