Search code examples
androidkotlinretrofit

An annotation argument must be a compile-time constant


Here what I was trying to do

@FormUrlEncoded
@POST("retrofit/POST/signup.php")
abstract fun signUp(
    @Field(Constant().KEY_NAME) name: String?,
    @Field(Constant().KEY_CELL) cell: String?,
    @Field(Constant().KEY_PASSWORD) password: String?,
): Call<Contacts?>?

I am getting error on Constant().KEY_NAME, Constant().KEY_CELL, Constant().KEY_PASSWORD

Error: An annotation argument must be a compile-time constant

Constant class.

class Constant {
  val BASE_URL = "http://istiak.ga/app/"

  val KEY_NAME = "name"
  val KEY_PASSWORD = "password"
  val KEY_CELL = "cell"
}

enter image description here


Solution

  • What you need to do is convert your Constants class to an object like below

    object Constants {
     const val BASE_URL = "http://istiak.ga/app/"
    
     const val KEY_NAME = "name"
     const val KEY_PASSWORD = "password"
     const val KEY_CELL = "cell"
    }
    

    Then you can directly reference each value instead of creating an instance of the Constants class for each variable you need as below

    @FormUrlEncoded
    @POST("retrofit/POST/signup.php")
    abstract fun signUp(
        @Field(Constant.KEY_NAME) name: String?,
        @Field(Constant.KEY_CELL) cell: String?,
        @Field(Constant.KEY_PASSWORD) password: String?,
    ): Call<Contacts?>?