-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlinx Serialization with GraalVM Native Images #1125
Comments
Thanks!
|
Thanks @qwwdfsad for quick response. I can confirm that the workaround works with GraalVM Native Image 👍 Looking forward for kotlin 1.4.30 though :) |
Just ran into this, and the workaround is mildly annoying -- can we keep this open until
this happens? Thanks! |
Just bumped into this while using ktor client, tried to add the serializer manually, but that still fails with 'serializer not found'. So I'm contextual serialization uses reflection as well. val serializer = Json {
serializersModule = SerializersModule {
contextual(MySerializableClass::class, MySerializableClass.serializer())
}
}
val client = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer(serializer)
}
}
val data = client.get<HttpResponse>("...").receive<MySerializableClass>() When using an explicit serializer it bumps into val body = client.get<HttpResponse>("...").receive<String>()
val data = Json.decodeFromString(MySerializableClass.serializer(), data) Though I guess I'll just postpone building a native image since it mostly creates unnecessary future refactor work |
It will be fixed with #1348, hopefully in Kotlin 1.8.0 |
I just used graalvms tracing agent which generated a config for me that works without this ugly workaround. I suspect this is the part that's making it work:
|
I can confirm that @Scui1's suggestion above works. When you add the above JSON snippet (wrapped in an array) to, e.g. Or, if you're using Spring, you can register your class RuntimeHints : RuntimeHintsRegistrar {
override fun registerHints(hints: RuntimeHints, classLoader: ClassLoader?) {
hints.reflection()
.registerField(YourSerializable::class.java.getField("Companion"))
hints.reflection()
.registerMethod(YourSerializable.Companion::class.java.getMethod("serializer"), ExecutableMode.INVOKE)
}
} |
This may not necessarily be bug, any reference in documentation would be helpful. When compiling the example as per the blog announcement
it works with Gradle
run
task (application
plugin applied). However When I convert application to a graalvm native image, the compilation succeeds (and binary native image is generated) but when I run the native image, I get the following error:To Reproduce
Just run compile the example using graalvm native image.
Expected behavior
Since I assume,
kotlinx.serialization
does not use reflection at runtime, the native image should have worked properly.If there is something needed in reflection config, please update the docs.
Environment
The text was updated successfully, but these errors were encountered: