Proguard vs Annotations

我有一个使用ActiveAndroid的应用程序,它是一个依赖于注释的数据库ORM库。

@Table(name="test") public class DatabaseItem extends ActiveRecordBase { public DatabaseItem(Context context) { super(context); // TODO Auto-generated constructor stub } @Column(name="counter") public int counter; } 

我如何让Proguard与之合作得很好? 目前,我在使用Proguard时遇到ActiveAndroid没有找到列名的错误。 我想它以某种方式破坏了注释。

我的相关Proguard配置:

 #ActiveAndroid -keep public class com.activeandroid.** -keep public class * extends com.activeandroid.ActiveRecordBase -keepattributes Column -keepattributes Table 

ColumnTable不是现有的java类文件属性。 你至少要指定

 -keepattributes *Annotation* 

(CFR)。 ProGuard手册 。

2013年3月, Proguard 4.9版本发布 ,其中一个修复方案是:

 Fixed overly aggressive shrinking of class annotations. 

因此,请确保您的Proguard版本是最新的,然后使用Eric Lafortune的解决方案:

 -keepattributes *Annotation* 

您还可以使用此配置来存储具有特定注释的所有类成员:

 -keepclassmembers class * { @fully.qualified.package.AnnotationType *; } 

解决方案是保留库的所有成员和数据库类

 -keep class com.activeandroid.** { *; } -keep public class my.app.database.** { *; } -keepattributes Column -keepattributes Table 

对于那些只使用Gradle的人来说,解决方案非常相似(请注意Annotation周围的单引号):

 keep 'public class java.package.** { *; }' keepattributes '*Annotation*' 

如果您在vanilla Gradle项目中使用JSON序列化注释(例如,Jackson等),这将特别有用。

这对我的情况有用:

 -keep class com.activeandroid.** { *; } -keep class com.activeandroid.**.** { *; } -keep class * extends com.activeandroid.Model -keep class * extends com.activeandroid.serializer.TypeSerializer -keep public class * extends com.activeandroid.ActiveRecordBase -keepattributes Column -keepattributes Table -keepattributes *Annotation* -keepclasseswithmembers class * { @com.activeandroid.annotation.Column ; }