2

I am trying to create a gradle plugin that will generate files (serialized from data classes) from a gradle task that can run in another project. lets say that the classes that I am serializing are marked with some annotation @Annot and I find all the relevant classes with reflection in the gradle task (I made sure to depend on kotlin compile so that the binaries are created). The problem is that when I try to use

val clazz: Class<*>
clazz.kotlin.serializer()

I get a Serializer for class 'Type' is not found. (Type is the actual class that I found and is annotated with @Serializable and @Annot . I am using gradle version 7.2, kotlin 1.5.21 (tried with 1.5.31 too) The project that uses the plugin has a kotlinx serialization plugin enabled What am I missing? why can’t I access the class serializer with the gradle task?

Note* if I run the above code in the target project (and not in the plugin then the serializer() function doesn't throw an exception

YYJo
  • 1,064
  • 1
  • 9
  • 15
  • 1
    Interesting. I suspect this is related to the fact that `kotlinx.serialization` relies on a compiler plugin to generate the serializers and `serializer()` functionality. I'm guessing at this point in the Gradle execution life cycle that compiler plugin didn't run yet. – Steven Jeuris Dec 08 '21 at 14:58
  • so the plugin runs after the compile runs? do we know if there is a gradle task to run the plugin? just wondering – YYJo Dec 14 '21 at 10:14

1 Answers1

1

So This didn't work in a the way I wanted it to but I found a way to make it work. I defined a task that extends JavaExec task:

tasks.create(createFilesTaskName, JavaExec::class.java) {
        mainClass.set("package.of.file.SchemaKt")
        classpath = sourceSets.getByName("main").runtimeClasspath
        group = groupName
}

The code in SchemaKt is in the source set of my kotlin sources or alternatively in a package required by the current project. The serializer() is accessible and working from there and I can run the schemas creation from a gradle task which is exactly what I needed.

I hope this helps someone in the future.

YYJo
  • 1,064
  • 1
  • 9
  • 15