Gradle plugin with library module artifact

I’ve got my custom plugin that generates a java class which is like com.package.Class.

And this plugin applied to X library artifact, then applied to My real application’s one of the modules.

X Library artifact closure(i need only one task run with preBuild)
foo {
fooX {
params = blabla
}
}

App Library module closure(i need two tasks run with preBuild)
foo {
fooX {
params = blabla
}
fooY {
params = blabla
}
}

The problem starts after that point, when I run my App, prebuild task run for both X Library artifact and app and then fooY task runs for both aar module and library module so the app ends up with 2 similar files and that causes build to fail.

Any recommendations to fix this or workaround appreciated.

Thanks in advance.