Kotlinx.serialization: Android Studio compile of project with serializable class fails with Internal compiler error

Created on 5 Feb 2021  路  4Comments  路  Source: Kotlin/kotlinx.serialization

Describe the bug
I am trying to build a fresh "Simple Compose" project in Android Studio (2020.3.1 Canary 5) to which I have added a single @Serializable class ApodResponse. (See code below). I have modified the build.gradle file according to current setup recommendations, namely the additions:

id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.10'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"

After a warning that

w: Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies
w: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes

the build fails reporting:

Execution failed for task ':app:compileDebugKotlin'.
> Internal compiler error. See log for more details

resulting from:

java.lang.NoSuchMethodError: 'void org.jetbrains.kotlin.serialization.DescriptorSerializer$Companion.registerSerializerPlugin(org.jetbrains.kotlin.serialization.DescriptorSerializerPlugin)'

I'll note that a IntelliJ IDEA terminal application with the same @Serializable class file builds and executes successfully.
To Reproduce
Here's the class file that appears to induce the error:

import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.time.LocalDate

@Serializable
data class ApodResponse(
val date: String,
val explanation: String,
val hdurl: String,
@SerialName("media_type")
val mediaType: String,
val title: String,
val url: String,
@SerialName("copyright")
val copyrightInfo: String? = null,
@SerialName("service_version")
val serviceVersion: String? = null
) {
companion object {
private var json = Json {
encodeDefaults = true
ignoreUnknownKeys = true
isLenient = true
}

    fun fromJsonString(string: String): ApodResponse {
        return json.decodeFromString(string)
    }
}

@Contextual
val localDate: LocalDate = LocalDate.parse(date)

fun hasCopyrightInfo() = copyrightInfo != null

fun hasServiceVersion() = serviceVersion != null

}
Expected behavior
I expected a successful build.
Environment
As I mentioned, the context is Android Studio (2020.3.1 Canary 5). build.gradle (project) buildscript indicates ext.kotlin_version = '1.4.30' although I understand that the compiler version is determined by the current Kotlin plugin (1.4.21-release-Studio4.2-1) All tools (e.g. Gradle) are out-of-the-box for Canary 5. For what it's worth, I have tried using implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0-RC" with identical results.

compose

Most helpful comment

@sandwwraith I'm noticing that seems to be the case in one of my Jetpack Compose apps too. I'm seeing a circular error here though because when I update the buildscript to use compiler version 1.4.21, all my Compose elements (Text(), Scaffold(), etc..) throw errors about only being compatible with compiler version 1.4.3 and above. Is this a known issue? Are there workarounds. It seems Jetpack Compose is unusable in it's current state?

All 4 comments

If I remember correctly, compose and serialization are currently not compatible with each other (their compiler plugin parts that is).

As far as I know, latest builds of Compose are compatible only with 1.4.21 compiler and there were special version with a fix for serialization plugin. See more here: https://github.com/JetBrains/compose-jb/issues/301#issuecomment-765512624

@sandwwraith I'm noticing that seems to be the case in one of my Jetpack Compose apps too. I'm seeing a circular error here though because when I update the buildscript to use compiler version 1.4.21, all my Compose elements (Text(), Scaffold(), etc..) throw errors about only being compatible with compiler version 1.4.3 and above. Is this a known issue? Are there workarounds. It seems Jetpack Compose is unusable in it's current state?

In my case, the minimum version of compose that solved the issue is "0.3.0-build149".

id 'org.jetbrains.compose' version '0.3.0-build149'
Was this page helpful?
0 / 5 - 0 ratings