我无法让混淆器根据注释保留内容(在 Android 下)。
我有一个注解,com.example.Keep:
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })
public @interface Keep {
}
我有一个类,它只能通过反射构造:
public class Bar extends Foo {
@com.example.Keep
Bar(String a, String b) { super(a, b); }
//...
}
它按预期工作(类保留其构造函数,尽管使用混淆的类名)当我的 proguard 配置包括以下内容时:
-keepattributes Signature,*Annotation*,SourceFile,LineNumberTable
-keepclassmembers class ** extends com.example.Bar {
<init>(...);
}
如果我尝试将构造函数更改为:
-keepattributes Signature,*Annotation*,SourceFile,LineNumberTable
-keepclassmembers class ** extends com.example.Bar {
@com.example.Keep <init>(...);
}
我看到我自己的注释和 proguard 的 annotation.jar 注释有相同的行为。我已经从 Foo 导入的 Keep
中复制并粘贴了注解名称,所以我确信这不是拼写错误或导入不正确。
谁能想到我可能遗漏了什么?
最佳答案
如果您的注解作为程序代码处理,您应该明确保留它们:
-keep,allowobfuscation @interface *
原因有点技术性:ProGuard 可能会在收缩步骤中删除它们,因此它们在优化步骤和混淆步骤中不再有效。
比较。 ProGuard 手册 > 故障排除 > Classes or class members not being kept .
关于java - 注释在混淆器中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17417323/