Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

android - An Annotation argument must be a compile time constant

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...