0

I received the following error when attempting to use a data class as a result type in a Retrofit service.

11-15 11:35:38.345 14693-14693/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app, PID: 14693
        kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Incorrect resolution sequence for Java constructor public constructor AuthenticationResponse() defined in com.example.app.data.auth.AuthenticationResponse[JavaClassConstructorDescriptor@6d57dc3] (kotlin.reflect.jvm.internal.impl.load.java.structure.reflect.ReflectJavaClass: class com.example.app.data.auth.AuthenticationResponse)
            at kotlin.reflect.jvm.internal.RuntimeTypeMapper.mapSignature(RuntimeTypeMapper.kt:202)
            at kotlin.reflect.jvm.internal.KFunctionImpl.<init>(KFunctionImpl.kt:46)
            at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:87)
            at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:39)
            at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
            at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
            at kotlin.reflect.jvm.internal.KClassImpl$Data.getConstructors(Unknown Source:7)
            at kotlin.reflect.jvm.internal.KClassImpl.getConstructors(KClassImpl.kt:192)
            at kotlin.reflect.full.KClasses.getPrimaryConstructor(KClasses.kt:40)
            at com.squareup.moshi.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:160)
            at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
            at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:90)
            at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:328)
            at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:311)
            at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:735)
            at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:168)
            at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:169)
            at retrofit2.Retrofit$1.invoke(Retrofit.java:146)
            at java.lang.reflect.Proxy.invoke(Proxy.java:913)
            at $Proxy4.credentialsLogin(Unknown Source)
            at com.example.app.data.rest.service.AuthenticationService$DefaultImpls.credentialsLogin$default(AuthenticationService.kt:37)
            at com.example.app.ui.module.auth.AuthenticatorActivity.login(AuthenticatorActivity.kt:204)
            at com.example.app.ui.module.auth.AuthenticatorActivity.access$login(AuthenticatorActivity.kt:37)
            at com.example.app.ui.module.auth.AuthenticatorActivity$onCreate$4.onEditorAction(AuthenticatorActivity.kt:107)
            at android.widget.TextView.onEditorAction(TextView.java:5909)
            at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
            at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
            at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
            at android.os.Handler.dispatchMessage(Handler.java:105)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6541)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

To be honest I'm not even sure what this error means and I haven't found many resources about it. The class description is fairly vague:

Signals that Kotlin reflection had reached an inconsistent state from which it cannot recover.

The only place that is using reflection is within Moshi, but I cannot figure out what is causing the issue.


Code

This is the data class:

data class AuthenticationResponse(
    @Json(name = "access_token) val accessToken: String,
    @Json(name = "refresh_token) val refreshToken: String,
    @Json(name = "user_id") val userId: String,
    val scope: String
)

The AuthenticationService:

interface AuthenticationService {

    @FormUrlEncoded
    @POST("oauth/token")
    fun credentials(
        @Field("username") username: String,
        @Field("password") password: String,
        @Field("scope") scope: String,
        @Field("grant_type") grantType: String = "password"
    ): Flowable<AuthenticationResponse>

    @FormUrlEncoded
    @POST("oauth/token")
    fun refreshToken(
        @Field("refresh_token") refreshToken: String,
        @Field("scope") scope: String,
        @Field("grant_type") grantType: String = "refresh_token"
    ): Flowable<AuthenticationResponse>

}

I am using Moshi as the JSON converter, and I have included the KotlinJsonAdapterFactory in the Moshi.Builder.


Update

I can reliably reproduce the error by attempting to create an AuthenticationResponse adapter using Moshi directly.

val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val json = "{\"user_id\": 000000," +
        "\"access_token\": \"testAccessToken\"," +
        "\"expires_in\": 3600," +
        "\"token_type\": \"Bearer\"," +
        "\"refresh_token\": \"testRefreshToken\"," +
        "\"scope\": \"test\"}"

val adapter = moshi.adapter<AuthenticationResponse>(AuthenticationResponse::class.java)

val response = adapter.fromJson(json)

Timber.d("response=$response")

The code above fails with the same KotlinReflectionInternalError when attempting to create the adapter value.

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • 1
    Can you reliably reproduce this error? If so, can you deserialization a response directly (without Retrofit and all) with a test case and file an issue on Moshi? – Eric Cochran Nov 16 '17 at 08:30
  • 1
    `KotlinReflectionInternalError` is, as may be deduced from the name and the description, a problem in the `kotlin-reflect` implementation. Please report this at http://kotl.in/issue – Alexander Udalov Nov 16 '17 at 10:45
  • @EricCochran Yes, I get the same error when deserializing directly (see the update above). I posted an issue [here](https://github.com/square/moshi/issues/394). – Bryan Nov 16 '17 at 14:50
  • 1
    @AlexanderUdalov I submitted an issue to YouTrack as well; [KT-21295](https://youtrack.jetbrains.com/issue/KT-21295). – Bryan Nov 16 '17 at 15:04
  • @Bryan You can answer this now and accept your own answer. :) Hopefully, it will help any future ProGuard users searching. – Eric Cochran Nov 17 '17 at 22:59

0 Answers0