Java 如何阻止Proguard删除返回类型?

Java 如何阻止Proguard删除返回类型?,java,proguard,Java,Proguard,我最近第一次启用了Proguard的模糊处理功能,它似乎发现了my-keep规则中的所有漏洞 我的keep规则是使用注释定义的:注释的元素将被单独保留。随后的配置如下所示: # Keep the annotation. -keep @interface org.mozilla.gecko.mozglue.JNITarget # Keep classes tagged with the annotation. -keep @org.mozilla.gecko.mozglue.JNITarget

我最近第一次启用了Proguard的模糊处理功能,它似乎发现了my-keep规则中的所有漏洞

我的keep规则是使用注释定义的:注释的元素将被单独保留。随后的配置如下所示:

# Keep the annotation.
-keep @interface org.mozilla.gecko.mozglue.JNITarget

# Keep classes tagged with the annotation.
-keep @org.mozilla.gecko.mozglue.JNITarget class *

# Keep all members of an annotated class.
-keepclassmembers @org.mozilla.gecko.mozglue.JNITarget class * {
    *;
}

# Keep annotated members of any class.
-keepclassmembers class * {
    @org.mozilla.gecko.mozglue.JNITarget *;
}

# Keep classes which contain at least one annotated element. Split over two directives
# because, according to the developer of ProGuard, "the option -keepclasseswithmembers
# doesn't combine well with the '*' wildcard" (And, indeed, using it causes things to
# be deleted that we want to keep.)
-keepclasseswithmembers class * {
    @org.mozilla.gecko.mozglue.JNITarget <methods>;
}
-keepclasseswithmembers class * {
    @org.mozilla.gecko.mozglue.JNITarget <fields>;
}
从Proguard出来,看起来像:

 public mt loadUrl(java.lang.String);
尽管有注释


那么,保留依赖类的神秘保留语法是什么呢?奇怪的是,在告诉它我希望保留一个入口点后,它还是继续破坏它…

这是预期的行为。在早期版本中,ProGuard会自动保留所有的返回类型和参数类型,但对于许多开发人员来说,这是不需要的,也是令人困惑的

例如,对于类#getMethod的反射,返回类型不相关

ProGuard现在打印出注释(如果未保留此类类型)。然后,您仍然可以为它添加一个-保留行

请参阅ProGuard手册>故障排除>

更新:

ProGuard 5.0 beta3及更高版本的支持

-keep,includedescriptorclasses .......

也可以自动将类保留在指定的字段类型、返回类型和参数类型中。

通常不需要这样做。注意,描述符类被保留;只是他们的名字没有保留。对于大多数带有反射的代码,这不是问题。如果它们也被反射引用的话,这只是一个问题,在这种情况下,它们的名字确实需要保留-keep。JNI可能是一个不方便的例子。
-keep,includedescriptorclasses .......