for a task that generates code I’d prefer creating a standalone task in buildSrc. It would be possible with an adhoc task in a gradle.kts file, or to create a class, but a separate file helps with organisation and prevents accidentally using properties from the script within the task (which Gradle will let you do, but it can cause
weird issues)
Like
@ephemient said, downloading a file using a custom repository is best for performance
https://stackoverflow.com/a/34327202/4161471. It’s also fine to download it using a Java HTTP client, or to use another Gradle plugin (
gradle-download-task for example).
I’d recommend splitting up the downloading&unpacking task from the generation task. It helps separate concerns, and Gradle can better cache and/or re-use task outputs.
As for being able to use the generated code in the main module - that’s pretty easy! You can map the code generation task to a
Provider<File>
, and add that as source directory. Gradle will then understand that it needs to run the generation task in order to build the source set, so the generation task will be triggered automatically
Take a look at this answer for a more basic code generation example (it’s only missing the downloading step)
https://stackoverflow.com/questions/74743976/kotlin-multiplatform-accessing-build-variables-in-code/74771876#74771876