Skip to content

Using java Custom Annotation in kotlin

I’ve created a custom annotation in java and I’m trying to use it in a Kotlin written class but in the compile time, I’m getting an error:

Annotation parameter must be a compile-time constant

Here is the code:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.CLASS)
public @interface EdsFieldInfo {
  int persianName();
  String columnName() default "";
  int domainVals() default -1;
}

This is the place where I’m using the annotation

@EdsFieldInfo(persianName = R.string.customer_file_id, columnName = "FileId")
@ColumnInfo(name = "FileId", typeAffinity = ColumnInfo.TEXT)
var fileId: String?,

and the error is shown here

persianName = R.string.customer_file_id

I try to find a solution for this but couldn’t please help me to resolve this matter? Thank you.

Answer

Since R is compiled during compile-time, I would highly suspect you cannot use such R.string.customer_file_id variable in an annotation. Maybe it gets evoluated before Android’s builder actually builds R.

So I’m afraid you should use a constant instead.