Proguard vs Annotations

别来无恙 提交于 2019-12-18 12:09:30

问题


I have an app that uses ActiveAndroid, a database ORM library, that relies on annotations.

@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;

}

How do I get Proguard working nicely with this? Currently, I get errors about not finding a column name by ActiveAndroid when using Proguard. I guess it somehow mangles the annotation.

My relevant Proguard configuration:

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

回答1:


Column and Table aren't existing java class file attributes. You'll at least have to specify

-keepattributes *Annotation*

Cfr. the ProGuard manual.




回答2:


In March 2013, Proguard version 4.9 was released, one of the fixes were:

Fixed overly aggressive shrinking of class annotations. 

So make sure that your Proguard version is up to date and then use Eric Lafortune's solution:

-keepattributes *Annotation*

You can also use this configuration to store all class members that has a specific annotation:

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



回答3:


Solution was to keep all members of the library and the database classes

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



回答4:


For those only using Gradle, the solution is very similar (note the single quotes around the Annotation):

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

keepattributes '*Annotation*'

This is especially useful if you are using JSON serialization annotations (e.g., Jackson or the like) in a vanilla Gradle project.




回答5:


This what worked in my case:

-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 <fields>; }


来源:https://stackoverflow.com/questions/7378693/proguard-vs-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!