Kotlin Serialization is not applied to the module
Asked Answered
G

3

8

In my project I need to use Kotlin.Serialization, so by documenting myself I saw how to import the plugin.

In the build.gradle(project) here is the plugins block.

plugins {
    id 'com.android.application' version '8.0.1' apply false
    id 'com.android.library' version '8.0.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
    id 'com.google.dagger.hilt.android' version '2.48.1' apply false
    id 'androidx.navigation.safeargs' version '2.5.3' apply false
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.20'

}

In the build.gradle(app) here is the plugins block:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs.kotlin'
    id 'com.google.dagger.hilt.android'
    id 'org.jetbrains.kotlin.plugin.serialization'
}

And there I implement the following library:

implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0"

The problem is, annotating the following class with @Serializable like this:

@Serializable
data class User(
    val name: String = "",
    val surname: String = "",
    val phone: String = "",
    val email: String = "",
)

Android studio leaves me with this warning:

kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed.

I poked around StackOverflow and noticed that the code in the build.gradle is correct, so I really wouldn't know how to fix this warning.

Greengrocer answered 10/11, 2023 at 12:54 Comment(7)
Apart from the inspection warning, does your code work? Can you serialize/deserialize User?Slattery
No my code doesn't work because I need the function 'serializer()'Greengrocer
I have the same warning, but deserialization works fine for me. It's possible that this warning is a distraction and that your problem is somewhere else.Slattery
the object to which I affix the Serializable annotation does not generate a method called serializer() precisely because the annotation has that warning. So the annotation doesn't generate the necessary codeGreengrocer
When I remove either @Serializable or id 'org.jetbrains.kotlin.plugin.serialization', the code stops working. So the plugin is definitely getting applied. MySerializable.serializer() is missing, as you report, but I can write val serializer = serializer<MySerializable>() and that works. This function isn't generated, it's present in Serializers.kt. I think this is a reasonable workaround. Or you may try to downgrade the plugin, since this seems like a bug.Slattery
@MarkoTopolnik I have a similar situation, except not directly calling .serializer() in my code (instead, I'm using kotlinx.serialization.json.Json encode/decode methods). I just tried downgrading the plugin to 7 different older versions, with the exact same results. Maybe I just don't know which version to downgrade it to, or maybe something else is going on.Broz
...and by also downgrading kotlin-stdlib now I see the same situation as you, @MarkoTopolnik, where it shows the warning but everything works again.Broz
S
3

There seems to be a bug in this version of the serialization plugin. Even though the plugin is getting applied and the serializers get registered, the function MySerializable.serializer() is missing. A workaround is to use the function kotlinx.serialization.serializer<MySerializable>().

Slattery answered 12/11, 2023 at 8:14 Comment(3)
This doesn't help newcomers at allPermutation
I myself am a newcomer to Kotlin Serialization, and this was the finding that finally helped me solve my problem. You seem to think otherwise, but you haven't written any advice as to what exactly is missing. Please expand on your comment so I can improve the answer.Slattery
Could you give an example?Illene
C
0

Try this in your project level build.gradle

plugins {
     id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.20' apply false
}

That might be the problem.

Casanova answered 10/11, 2023 at 13:45 Comment(1)
unfortunately this does not solve the warningGreengrocer
U
0

I see you forgot to add this to build.gradle(app).

plugins {
    ...    
    id 'kotlinx-serialization'
    ...
}
Urine answered 10/11, 2023 at 15:6 Comment(5)
This advice seems to contradict the documentation. Can you add an official documentation link where this plugin ID is mentioned?Slattery
In fact, the documentation talks about this plugin id 'org.jetbrains.kotlin.plugin.serialization', But I've also tried the one recommended and it doesn't solveGreengrocer
Thank you for your return. I use it in my own project, additionally I reviewed the other answers given. https://mcmap.net/q/785202/-kotlin-serialization-quot-unresolved-reference-serializer-quotRosalindrosalinda
It seems to have been this way in an early version of the plugin (year is 2019).Slattery
Even the mentioned answer does not solve the problem (moreover, an answer I already consulted before asking the question.)Greengrocer

© 2022 - 2024 — McMap. All rights reserved.