An Annotation argument must be a compile time constant

an annotation argument must be a compile-time constant retrofit
an annotation argument must be a compile-time constant androidjunit4
an annotation argument must be a compile-time constant dagger
an annotation argument must be a compile-time constant epoxy
an annotation argument must be a compile-time constant room
an annotation argument must be a compile-time constant butterknife
unresolved reference androidjunit4 an annotation argument must be a compile time constant
an annotation argument must be a compile-time constant enum

i have seen this question. Similar error.But in my case it is different.

While working with Room i was creating table. it was working fine.

@Daointerface 
UserDao {
@Query("SELECT * FROM user")
fun getAll(): List<User>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(user: User)

@Delete
fun delete(user: User)}

but then i found all table Names must be stored in a different class. like table name "user" - > Stored in different class.

Eg.

class Table {
companion object {
    const val USER_TABLE = "user"
}}

But below code is not working . it is not picking up table name from Table class. Giving compile time error . "An Annotation argument must be a compile time constant" please help me out.What wrong in it

@Query("SELECT * FROM $Table.USER_TABLE")
fun getAll(): List<User>

The problem is the one stated in the error, you can't have dynamically defined arguments for your @Query annotation. If you want to define the name of the table somewhere else, use string concatenation. You can do it like this:

@Query("SELECT * FROM " + Table.USER_TABLE)
fun getAll(): List<User>

This is how they do it in this google sample.

"An annotation argument must be a compile-time constant" for Java, Misleading error "An annotation argument must be a compile-time constant" for Java "Class" object. 2. Subtask of 1. Subtask of 1 issue (1 unresolved). KT-3804  The question here is "is it possible to get around this in any way". I'm guessing the answer is no, but here is some background to why I ask: I have created a library that has many base functions

You need to escape the String concatenation when using @Value annotation with the dollar symbol in Kotlin (prepend \ to $):

@Query("SELECT * FROM \$Table.USER_TABLE")
fun getAll(): List<User>

Kotlin "An Annotation argument must be a compile time constant , I get "An Annotation argument must be a compile time constant" when I try to use compile time config with in Kotlin. I understand this is because  Getting "An annotation argument must be a compile-time constant Getting "An annotation argument must be a compile-time constant" #15. that action at this time.

You should define column name also in data class and access if you want to use columns in queries and access it via this method:

@Query("SELECT * FROM ${Table.USER_TABLE}")

Convert Java annotation non-constant field to Kotlin, Default value of annotation parameter must be a compile-time constant. @Target(​AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget. I get "An Annotation argument must be a compile time constant" when I try to use compile time config with in Kotlin. I understand this is because I use String resources.

Class::property.name should be compile-time constant, Class::property.name should be compile-time constant I was playing around with some annotations and I would like to annotate a field  By convention, annotations precede other modifiers. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants. Here is a method declaration with an annotation corresponding to the annotation type declared above:

Compile-time constants, There are, however, situations where we need to know the value at compile time Let's define a custom annotation, MyLogger , with a single parameter defining  The value for annotation attribute must be a constant expression What is the solution for this issue? java annotations | this question edited May 12 '13 at 15:46 Andrew Thompson 143k 20 141 278 asked May 12 '13 at 15:42 Cons 165 1 2 7 1 Use a constant expression.

Kotlin, Where Should I Keep My Constants in Kotlin? There has recently been a compile-time constant. Kotlin annotation parameter must be a compile-time constant  The documentation for compile-time constants lists three requirements that the property needs to fulfill in order to declare it as a const val. These are: Top-level or member of an object; Initialized with a value of type String or a primitive type; No custom getter

Comments
  • Why negative marking, i didn't understand. Whats illogical in it.
  • But in koltin , String concatenation is done using "$ symbol". when i convert your concat string into kotlin code . it showed me like this . @Query("""SELECT * FROM ${Table.USER_TABLE}""")
  • I've been doing with the + sign and it's been working. github.com/Levi-Moreira/TeamManagerApp/blob/master/app/src/main/… My guess is that the kotlin interpolation is evaluated at runtime and it might not work properly with annotation arguments that need to be evaluated at compile time.