【问题标题】:annotations having no effect in proguard注释在 proguard 中无效
【发布时间】:2013-07-02 04:19:28
【问题描述】:

我无法让 proguard 根据注释来保存内容(在 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 导入中复制并粘贴了注释名称,因此我确信这不是拼写错误或不正确的导入。

谁能想到我可能会错过什么?

【问题讨论】:

    标签: java android proguard


    【解决方案1】:

    如果您的注释被作为程序代码处理,您应该明确地保留它们:

    -keep,allowobfuscation @interface *
    

    原因有点技术性:ProGuard 可能会在收缩步骤中删除它们,因此它们在优化步骤和混淆步骤中不再有效。

    参照。 ProGuard 手册 > 疑难解答 > Classes or class members not being kept.

    【讨论】:

    • 这主要对我有用,但它仍在删除注释的参数......所以@Hi(value="bye") 变成了@Hi
    • 另外,你能解释一下这个命令是如何工作的吗?在我看来,您只是告诉 Proguard 保留您定义的所有注释接口,而不是告诉它保留已放在类或方法上的注释。如果我错了,你能解释一下原因吗?
    猜你喜欢
    • 2012-07-22
    • 2017-04-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2016-01-21
    • 1970-01-01
    相关资源
    最近更新 更多