Hello everyone, I have a question about ksp. Is it...
# ksp
j
Hello everyone, I have a question about ksp. Is it possible to access a .json file from the code generation in the processor? example I have to generate certain classes from a json file that is in the parent
y
If you only need the JSON file, it is better as a gradle task. If you need both JSON and the KSP info, you need to pass that as an argument to KSP. I don't think gradle will realize it is a file, hence you also need to add that file to the ksp's task's inputs so that gradle knows it needs to rerun when the file contents change.
j
Thanks for your answer, if I need to generate classes according to the json, you will have an example of how I can pass the file to ksp
h
KSP is a Kotlin source processor, eg get all classes with this annotation. It does not support code generating. This is done by yourself, eg by using KotlinPoet. If you only want to generate code without looking at Kotlin code, create a Gradle task, read the JSON file and create the Kotlin code with KotlinPoet.
d
In addition to the other good answers in this thread, if you want an example of a Gradle task that does code gen from a resource (in this case XML) then maybe you could have a look at: https://github.com/flavioarfaria/Catalog/blob/main/catalog-gradle-plugin/src/main/[…]om/flaviofaria/catalog/gradle/GenerateResourceExtensionsTask.kt There are more complex examples that work this way too, like apollo-kotlin
j
thanks @David Rawson