Java 使用proguard保留特定注释

Java 使用proguard保留特定注释,java,android,annotations,proguard,Java,Android,Annotations,Proguard,我的项目中有两种注释类型:Annotation1和Annotation2。两者都是运行时注释。所以,我的问题是如何只保留Annotation1和剥离Annotation2?我找不到这是可能的。如果没有,我就得不到——为什么 例如: class Test { @Annotation1 @Annotation2 String name; } 我希望从所有字段中删除Annotation2,并将Annotation1保存在任何地方。模糊处理是编译时过程,理想情况下我建议不要模糊

我的项目中有两种注释类型:
Annotation1
Annotation2
。两者都是运行时注释。所以,我的问题是如何只保留
Annotation1
和剥离
Annotation2
?我找不到这是可能的。如果没有,我就得不到——为什么

例如:

class Test {
    @Annotation1
    @Annotation2
    String name;
}

我希望从所有字段中删除
Annotation2
,并将
Annotation1
保存在任何地方。

模糊处理是编译时过程,理想情况下我建议不要模糊任何注释,但如果您想这样做,应该可以解决您的问题

-keep class Annotation1
如果您想保留成员,可以使用以下代码

 -keep class Annotation1{*;}

如果在其他地方未使用(检索、转换、调用等)相应的注释类,ProGuard会自动从类/字段/方法中删除注释。您不能强制ProGuard删除注释,但可以告诉它保留看似未使用的注释:

-keep @interface com.example.MyAnnotation

它可以防止注释类被删除,而不是字段、方法和类上的注释。我修改了我的答案以帮助其他人,您可以使用-keep class Annotation1{*;}也许您应该再次阅读我的问题,您的答案不是关于注释的。是的,刚才看到了您的问题,您的编辑现在使它更清晰了。因此,您需要proguard执行后的Annotation2,但是我仍然不清楚通过删除Annotation2(它是运行时注释)可以实现什么效果。删除它将改变您的运行时行为。我相信您会在proguard删除死代码的优化阶段看到这种情况。我不认为注释被认为是死代码事实上,我不想这样做,我必须这样做,因为愚蠢的android行为。所以,问题不在于是否这样做,我只想检查是否可能,以及如何可能,或者为什么不可能。