In my project I'm using these library imports:
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-moshi:2.9.0")
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.3")
implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3")
to make rest api calls. the dto classes I use (using only these libraries) are of the type
import com.squareup.moshi.Json
data class myClassDto(
@field:Json(name = "api_wanted_name1") val myName1: String,
@field:Json(name = "api_wanted_name2") val myName2: Int,
)
and so on, while to create the retrofit object that will make the usage calls
val retrofit = Retrofit.Builder()
.baseUrl("https://my_apy_url")
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create()
When I run the code from the IDE everything works fine. When I compile and launch the .msi it gives me the following error:
Unable to create converter for class .. at retrofit2.Utils.methodError(Utils.java:54)
at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:126)
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:85)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:39)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
looking at the error I realized that it is as if this library had also been imported to me
com.squareup.moshi:moshi-kotlin:1.14.0
which I have to make some changes in my code:
- I have to add the builder to the retrofit object:
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
and therefore it becomes me
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
.create()
- all my dto classes I have to become
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class myClassDto(
val api_wanted_name1: String,
val api_wanted_name2: Int
)
so I have to add @JsonClass(generateAdapter = true) in front of the class and the variable names must be like the ones I used before inside @field:Json("") which I can no longer use.
That said, the problem is solvable, but I have to modify a lot of dto classes. Is there a way to solve this problem? can you tell me why this happens?