Proguard vs Annotations

Pet*_*rdk 24 java annotations proguard

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

@Table(name="test")
public class DatabaseItem extends ActiveRecordBase<DatabaseItem> {

    public DatabaseItem(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Column(name="counter")
    public int counter;

}
Run Code Online (Sandbox Code Playgroud)

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

我的相关Proguard配置:

#ActiveAndroid
-keep public class com.activeandroid.**
-keep public class * extends com.activeandroid.ActiveRecordBase
-keepattributes Column
-keepattributes Table
Run Code Online (Sandbox Code Playgroud)

Eri*_*une 35

Column并且Table不是现有的java类文件属性.你至少要指定

-keepattributes *Annotation*
Run Code Online (Sandbox Code Playgroud)

(CFR).在ProGuard的手册.


Sim*_*erg 16

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

Fixed overly aggressive shrinking of class annotations. 
Run Code Online (Sandbox Code Playgroud)

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

-keepattributes *Annotation*
Run Code Online (Sandbox Code Playgroud)

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

-keepclassmembers class * {
    @fully.qualified.package.AnnotationType *;
}
Run Code Online (Sandbox Code Playgroud)


Pet*_*rdk 6

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

-keep class com.activeandroid.**
{
     *;
}
-keep public class my.app.database.**
{
    *;
}
-keepattributes Column
-keepattributes Table
Run Code Online (Sandbox Code Playgroud)

  • 我使用了这个,但我没有使用两行“-keepattributes”,而是只使用了这一行:“-keepattributes \*Annotation\*”,如 Eric Lafortune 和 Simon André Forsberg 所示。现在一切都很好! (2认同)