Dynamically adding res/raw folder during the build

I like to add some raw files during the build
when I copy to build/intermediate//merged_res
it won’t get flatten nor packaged up for the Android app.

So I tryied adding custom task adding it during the task evaluation,

custom task does

  • copy file to build//raw
  • adding that folder by
    project.android.sourceSets.main.res.srcDirs +=[“build/<customFolder>”]

But flatten file raw_.flat still not included in

If I heard code in build.gradle file,
And the task just copy the files to build/,

sourceSets {
        main {
            res.srcDirs += ['build/<customFolder>']

Then it works.

I tried to have custom task depends on

  • mergeResource,
  • preBuild
  • compileKotlin

none of it works. Does anyone have any idea?

Not sure whether there are Android-specifics.

But anyway, copying to the output directory of another task is a big no-no-never.

And generating into a custom folder and manually configuring it as srcDir is sub-optimal, as then all tasks needing that resource are missing the task dependency.

Instead in a normal build you would make sure the task has its input and especially outputs properly defined - anyway always a good idea - and then configure the task itself as srcDir, that way its outputs are taken as the sources and every task requesting these files automatically has the necessary task dependency.

Thanks for the advice. Lets say we want to have some privacy sensitive files, depending on the build type.
And I like to have it included in the res/raw flatten process. In that case, it will not be in the code base
Nor we don’t want that extrapath to remain in the project file when developing locally.
In that case, copying that under build/ file and attempt to be included in the android.sourceSets.main.res.srcDirs

Or you have another suggestion?

Otherwise, I can put in the .gitignore, but I like to avoid it.

I did not say you should version the file.
I just said you should properly register the task that generates the file “as” srcDir instead of hard-coding a path where it is generated to.
At least if it works with Android the same as in normal builds.

I think you are suggesting what I’m attempting. May be putting in to the build path was wrong idea. But
in the custom task, I set

project.android.sourceSets.main.res.srcDirs +=[“<customFolder>”]

But it does not get processed.

Do you know which buildTask task should I depends on to enable "<customFolder>" to be counted?

I tried merge, preBuild, compileKotlin, but none includes this path.

I don’t think you understood what I said.
You should definitely not change the configuration from a task.
I said you should configure the task as source set, at least in a normal build it is like that.

def myGenerateTask = tasks.register(...)
sourceSets {
        main {