Moshi: KotlinReflectionInternalError when attempting to create an adapter for a data class

Created on 16 Nov 2017  Â·  4Comments  Â·  Source: square/moshi

When attempting to create an adapter for the following data class, the code crashes with a KotlinReflectionInternalError.

data class AuthenticationResponse(
    @Json(name = "user_id") val userId: Int?,
    @Json(name = "access_token") val accessToken: String?,
    @Json(name = "expires_in") val expiresIn: Int?,
    @Json(name = "token_type") val tokenType: String?,
    @Json(name = "refresh_token") val refreshToken: String?,
    val scope: String?
)

I used the following code to create an adapter.

val json = "{\"user_id\": 000000," +
        "\"access_token\": \"testAccessToken\"," +
        "\"expires_in\": 3600," +
        "\"token_type\": \"Bearer\"," +
        "\"refresh_token\": \"testRefreshToken\"," +
        "\"scope\": \"test\"}"

val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()

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

val response = adapter.fromJson(json)

Timber.d("response=$response")

Here is the full error.

11-16 09:41:51.832 27067-27067/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app, PID: 27067
        kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Incorrect resolution sequence for Java constructor public constructor AuthenticationResponse() defined in com.example.app.data.auth.AuthenticationResponse[JavaClassConstructorDescriptor@16e84e0] (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 com.squareup.moshi.Moshi.adapter(Moshi.java:62)
            at com.example.app.ui.module.login.LoginActivity.login(LoginActivity.kt:201)
            at com.example.app.ui.module.login.LoginActivity.access$login(LoginActivity.kt:36)
            at com.example.app.ui.module.login.LoginActivity$onCreate$4.onEditorAction(LoginActivity.kt:100)
            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)

I'm not sure if it's an issue with how Moshi handles reflection, or if it's an issue within Kotlin reflection itself. I am using moshi-kotlin version 1.5.0 and Kotlin version 1.1.60.

Most helpful comment

ProGuard… it's always ProGuard, and it catches me off guard every time. I didn't include the ProGuard rule for the Kotlin artifact.

-keepclassmembers class kotlin.Metadata {
    public <methods>;
}

All 4 comments

Thanks for the runnable code, and thanks for filing https://youtrack.jetbrains.com/issue/KT-21295.

I couldn't reproduce this on Moshi 1.5.0 and Kotlin 1.1.60 with your code.
I am interested to see what comes out of that Kotlin issue.

(To be clear, this is most certainly an issue in kotlin-reflect, as noted by the exception name and the message.)

That is so strange! What could I be doing differently?

On Nov 16, 2017, at 4:51 PM, Eric Cochran notifications@github.com wrote:

Thanks for the runnable code, and thanks for filing https://youtrack.jetbrains.com/issue/KT-21295 https://youtrack.jetbrains.com/issue/KT-21295.

I couldn't reproduce this on Moshi 1.5.0 and Kotlin 1.1.60 with your code.
I am interested to see what comes out of that Kotlin issue.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub https://github.com/square/moshi/issues/394#issuecomment-345074369, or mute the thread https://github.com/notifications/unsubscribe-auth/AO8p45_lfcvuk3SCojf19n7T6xDuVrCJks5s3K59gaJpZM4QgmUV.

ProGuard… it's always ProGuard, and it catches me off guard every time. I didn't include the ProGuard rule for the Kotlin artifact.

-keepclassmembers class kotlin.Metadata {
    public <methods>;
}
Was this page helpful?
0 / 5 - 0 ratings