2017-02-28 100 views
0

我一直在使用“UI form validation library for Android”库来验证我的用户界面。而我也实现了一些自定义的验证规则,例如:progaurd不保留注释

public class CellRule extends AnnotationRule<Cell, String> { 
    protected CellRule(Cell cell) { 
     super(cell); 
    } 

    @Override 
    public boolean isValid(String text) { 
     if(text.isEmpty()) { 
      return false; 
     } 

     String cellNumber = StringUtils.toStandard(text); 

     Pattern cellPattern = Pattern.compile("^(0|\\+98)9(1[0-9]|3[1-9]|2[1-9])-?[0-9]{3}-?[0-9]{4}$"); 
     Matcher cellMatcher = cellPattern.matcher(cellNumber); 

     return cellMatcher.matches(); 
    } 
} 

@ValidateUsing(CellRule.class) 
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.FIELD) 
public @interface Cell { 
    int sequence()   default -1; 
    int messageResId()  default -1; 
    String message()  default "Invalid Cell!"; 
} 

,我用这个命令注册它们:

Validator.registerAnnotation(Cell.class); 

,并预期它一直工作。直到我试图生成签名apk(启用缩小),现在它给了我一些错误...当我检查了apk(使用反编译)看起来progaurd没有保留注释(@interface Cell)在apk中。

当我注释掉注册行都很好。

回答

0

我发现每个库都有它自己的progaurd设置,它可能记录在库的README文件中!

为saripaar的图书馆,这将这样的伎俩:

-keep class com.mobsandgeeks.saripaar.** {*;} 
-keep @com.mobsandgeeks.saripaar.annotation.ValidateUsing class * {*;}