Processing layouts needs to be a Gradle task. But ...
# ksp
y
Processing layouts needs to be a Gradle task. But i don't think any of them are public API s such that you can define inputs outputs properly. Data binding does what you want, process files and provide inputs for the processor, yet all of setup is integrated in AGP. It is very very difficult to do it outside AGP due to different compilation modes (dynamic modules, namespaced resources, build variants etc). AGP also hooks those tasks' output as input to kapt. Don't get me wrong, what you want to do is totally reasonable but AGP just doesn't have the right public APIs to do that. If it is for your own project, you can make it work by hacking internals but for a public library, it is a losing battle😞
g
Yeah, that's exactly what I found after my current research. What I'm thinking is to yes use a gradle task that would first find all the annotated sources that have the layout id in them and it will output a list of the layout files (if I understand right, gradle can use that as an input) that will be used for the input of the generate task which means the task of finding the annotated sources will always run but the task of generating the code will only run when there is a change to one of the layouts or a new layout is being requested
The first challenge I need to solve is to intercept the R class generation and create a Similar class that will hold only the layouts and each layout will have two values. the id and the file path
y
You don't want file path in most situations because that will break remote cache. Also, finding annotated elements with a task does not make much sense, you can use KSP for that. Maybe you need 3 steps. 1) process resource inputs to extract what you need from layouts 2) pass its output into KSP task as input, that will find annotated elements and write it to some output 3) takes output of the two, generates code 4) make kotlin compilation depend on 3, and add 3s output as source inputs to kotlin compilation. It is possible to inject a task between KSP and kotlin compilation because KSP is an independent task
All of these can be incremental as a result
g
But I don't need to parse all the layout files, other than to solve the issue with non final values in annotations, just specific files that will be used for ViewHolders and I'll know what after processing the annotations
So there may be here 5 or 6 different steps
With a combination of both KSP and plug-in