kotlinx.serialization
kotlinx.serialization copied to clipboard
Unable to enable serialization when applied from script plugin instead of directly in build.gradle
Describe the bug
When kotlin-serialisation is declared in script plugin, and then apply from
to project build.gradle. The compiler plugin does not get applied, even though the build is successful
The issue is visible by adding @Serializable
attribute to a class. The IDE complains with the following warning
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.
And the compile apk does not work, with the following error
kotlinx.serialization.SerializationException: Serializer for class '[ClassName]' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
at kotlinx.serialization.internal.PlatformKt.platformSpecificSerializerNotRegistered(Platform.kt:29)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:60)
.....
To Reproduce
Create a project, and a script plugin file
In the script plugin (let's call it - kotlin-serialisation.gradle
)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.0"
}
}
// can not apply plugin id in external gradle files. Known issue
// https://github.com/gradle/gradle/issues/1262
//
// apply plugin: 'kotlinx-serialization'
apply plugin: org.jetbrains.kotlinx.serialization.gradle.SerializationGradleSubplugin
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1"
}
In the project's build.gradle
[...other stuff]
apply from: "../plugins/kotlin-serialisation.gradle"
[...other stuff]
Expected behavior Kotlin serialization should work
Environment
- Kotlin version: 1.5.0
- Library version: 1.2.1
- Kotlin platforms: JVM
- Gradle version: 6.8.3
- IDE version (if bug is related to the IDE): Android Studio 4.2.1
- Other relevant context [e.g. OS version, JRE version, ... ]
I'm not sure what could go wrong here. Maybe the plugin wasn't applied to a particular project? Or it wasn't added because buildscript
section is evaluated only in root's build.gradle
. Try to use an approach that is recommended by Gradle
@sandwwraith Thanks for replying.
Replacing the apply from
line in build.gradle
[:app] with the content of kotlin-serialisation.gradle
works. (changing apply plugin with the id instead of implement-class. Known gradle issue [ https://github.com/gradle/gradle/issues/1262 ])
The issue is that it should work with this apply from plugin.gradle
approach.
We use this approach to enable plugins for different projects. So that each project does not need to duplicate the plugin's gradle file.
For example, Firebase Crashlytics also require a buildscript block in the it's plugin.gradle file
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.3'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
}
}
allprojects {
repositories {
// Check that you have Google's Maven repository (if not, add it).
google()
}
}
dependencies {
implementation 'com.google.firebase:firebase-crashlytics'
}
apply plugin: com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsPlugin
I've created a simple project that can be used to test this issue
https://github.com/ebhsgit/KXS_Test
The com.example.myapplication.Test
class contains the Serializable
annotation
@ebhsgit Have you managed to solve the issue and use apply from
approach with separate gradle file? I am running into the same problem now.
@zlmr Unfortunately not. If you find a solution, please let me know.
The problem with the org.jetbrains.kotlin.plugin.serialization
plugin could be related to specific versions of it and their compatibility issues with org.jetbrains.kotlin.android
plugin, I've just discovered this and posted a new issue here.
I hope this discovery will help someone who encounters a similar problem in the future since there seems to be absolutely no mention of this particular case on the internet.
For future viewers of this issue, the warning seems to be a bug from the IDE, and the library works perfectly fine. I'm using version catalogs with Gradle, here's my project-level build.gradle.kts
,
plugins {
alias(libs.plugins.com.android.application) apply false
alias(libs.plugins.org.jetbrains.kotlin.android) apply false
alias(libs.plugins.org.jetbrains.kotlin.serialization) apply false
}
and module-level build.gradle.kts
,
plugins {
alias(libs.plugins.com.android.application)
alias(libs.plugins.org.jetbrains.kotlin.android)
alias(libs.plugins.org.jetbrains.kotlin.serialization)
}
android {
...
}
dependencies {
...
implementation(libs.serialization)
...
}
and libs.versions.toml
,
[versions]
agp = "8.1.0"
org-jetbrains-kotlin-android = "1.8.10"
...
serialization-plugin = "1.9.20"
serialization-dep = "1.6.1"
[libraries]
...
serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json" , version.ref = "serialization-dep" }
[plugins]
com-android-application = { id = "com.android.application", version.ref = "agp" }
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "org-jetbrains-kotlin-android" }
org-jetbrains-kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization" , version.ref = "serialization-plugin" }
@shubham0204 Mine is almost as yours and working perfectly, but that warning is not aesthetically pleasing😂
The problem with the org.jetbrains.kotlin.plugin.serialization plugin could be related to specific versions of it and their compatibility issues with org.jetbrains.kotlin.android plugin, I've just discovered this and posted https://github.com/Kotlin/kotlinx.serialization/issues/2508.
#2508 is unrelated to this issue, and was resolved separately.
I think this issue is obsolete, as there are other recommended by Gradle ways to share build logic (e.g. convention plugins).