2011-09-11 9 views
20

Tôi có một ứng dụng sử dụng ActiveAndroid, một thư viện ORM cơ sở dữ liệu, dựa trên chú thích.Proguard vs 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; 

} 

Làm cách nào để Proguard hoạt động tốt với tính năng này? Hiện tại, tôi gặp lỗi khi không tìm thấy tên cột của ActiveAndroid khi sử dụng Proguard. Tôi đoán nó bằng cách nào đó mangles chú thích.

liên quan cấu hình Proguard của tôi:

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

Trả lời

31

ColumnTable không hiện thuộc tính tệp lớp java. Bạn sẽ ít nhất phải chỉ định

-keepattributes *Annotation* 

Cfr. số ProGuard manual.

6

Giải pháp là để giữ tất cả các thành viên của thư viện và các lớp cơ sở dữ liệu

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

tôi đã sử dụng điều này, nhưng thay vì hai dòng "-keepattributes" Tôi từng dòng chỉ này: "-keepattributes \ * Chú \ *", như được chỉ ra bởi Eric Lafortune và Simon André Forsberg. Tất cả mọi thứ đã làm việc tốt ngay bây giờ! – PFROLIM

15

Vào tháng Ba năm 2013, Proguard version 4.9 was released, một trong các bản sửa lỗi là:

Fixed overly aggressive shrinking of class annotations. 

Vì vậy, hãy chắc chắn rằng phiên bản Proguard của bạn được cập nhật và sau đó sử dụng Eric Lafortune của giải pháp:

-keepattributes *Annotation* 

Bạn có thể cũng sử dụng cấu hình này để lưu trữ tất cả các thành viên của lớp có chú thích cụ thể:

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

Đối với những người chỉ sử dụng Gradle, giải pháp là rất giống nhau (chú ý dấu nháy đơn xung quanh Chú):

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

keepattributes '*Annotation*' 

Điều này đặc biệt hữu ích nếu bạn đang sử dụng JSON chú thích serialization (ví dụ, Jackson hoặc tương tự) trong một dự án vanilla Gradle.

0

này những gì làm việc trong trường hợp của tôi:

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