Search code examples
buildgradle

Import a Gradle script from the root into subprojects


I want to do an 'apply from: ./gradle/script/common.gradle' in my root build.gradle and have it be available to all of my subprojects.

I've tried putting the apply in the 'subprojects' but because the path is relative it won't always resolve, (the subprojects are not flat). I've also put it outside of the subprojects at the root but then the targets aren't resolved by the subprojects.

I haven't been able to find a way to get the working directory in a way the 'apply from:' likes, or how to get the root directory or directory that the original gradle script is being executed from.


Solution

  • In your root build.gradle.:

    allprojects { // or: subprojects { ... }
        apply from: "gradle/script/common.gradle"
    }
    

    Should you ever need an absolute path to such a script, you can always do "$rootProject.projectDir/gradle/script/common.gradle".