3

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.

1
  • Problem is in the first line & second : persianName = R.string.customer_file_id, culomnName = "FileId" Mostly the suspect: persianName = R.string.customer_file_id - culomnName = "FileId" & name = "FileId" Take a look: stackoverflow.com/a/50679302/4409113 Commented Aug 20, 2018 at 10:43

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

that's correct but I'm wondering how is was working in main project and it started throwing this error after I moved my code to a library project
Maybe your R.string.customer_file_id is still in your main project? If you moved your code to another module it need to be inside that module I believe.
No I'm pretty sure which I moved related strings too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.