Process files before they are packed into an apk

I want to patch some of the files that are to be included in my apk before the apk is generated. How do I configure my gradle config to do that? Note that the code that patches the files will need access to all the files of the apk.