Refactoring code using apply from '...' changes behavior compared to inline code in build.gradle

Should I expect side effects when putting code into additional gradle files?

For instance I’m running against an issue where my root project’s build.gradle contained something like this:

FileCollection getFoo() { … }

This function used to be accessible from the sub projects. Now I’m moving this in misc.gradle and doing

apply from ‘misc.gradle’

in my root project’s build.gradle file, but the function is not available anymore in my sub projects.

I would expect this to have no impact. This is with Gradle 1.9.


There are a few known limitations where it is necessary to adapt the code. To answer your particular question, in order to make a method declared in an applied build script available outside that script, you’ll have to refactor it into a closure property attached to the project (‘ = { … }’). The call sites remain unchanged.

Are the limitations documented somewhere? I’ve been refactoring a lot of our build code into files and I want to make sure I don’t miss anything. thanks!

I’m not aware of any such docs. One other limitation I can think of is that ‘buildscript’ blocks need to stay in the applying file, rather than being moved into the applied file.

ok, thanks.